Filter uncanny-automator-pro

automator_pro_throttle_background_task_execution

Filters the background task execution throttle, allowing customization of how frequently tasks are processed.

add_filter( 'automator_pro_throttle_background_task_execution', $callback, 10, 1 );

Description

This filter hook controls the delay between executions of background tasks. Developers can use it to introduce a custom sleep duration in seconds, measured by the returned value, to manage CPU usage and prevent overwhelming the system. It fires before a background task is executed.


Usage

add_filter( 'automator_pro_throttle_background_task_execution', 'your_function_name', 10, 1 );

Parameters

$item (mixed)
This parameter is used to specify the number of seconds to pause the background task execution, allowing other CPU processes to run.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the automator_pro_throttle_background_task_execution filter.
 *
 * This example demonstrates how to dynamically adjust the sleep duration
 * for background task execution based on certain conditions related to the task item.
 * In this specific scenario, we're introducing an additional 2-second delay
 * if the task item's 'type' is 'send_email'.
 *
 * @param int $throttle_seconds The current number of seconds to throttle execution.
 * @param array $item           The current background task item being processed.
 * @return int                  The updated number of seconds to throttle execution.
 */
add_filter( 'automator_pro_throttle_background_task_execution', function( $throttle_seconds, $item ) {

	// Check if the task item is an array and has the 'type' key.
	if ( is_array( $item ) && isset( $item['type'] ) ) {
		// If the task type is 'send_email', add an additional 2 seconds to the throttle.
		if ( $item['type'] === 'send_email' ) {
			$throttle_seconds += 2;
		}
	}

	// Return the potentially modified throttle seconds.
	return $throttle_seconds;

}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

uncanny-automator-pro/src/core/loops/loop/background-process/entity-actions.php:150

*/
	protected function task( $item ) {

		// Start of with null user id.
		$user_id = null;

		// The idea here is to let CPU do other tasks while we sleep the process.
		$throttle_seconds = apply_filters( 'automator_pro_throttle_background_task_execution', 0, $item );

		if ( $throttle_seconds >= 1 ) {
			sleep( $throttle_seconds );
		}

		$transient = Registry::get_process_transient( $item['process_transient'] );

Scroll to Top