Filter uncanny-automator-pro

automator_async_retry_chunk_size

Filters the chunk size for processing asynchronous automator tasks to optimize performance.

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

Description

Controls the number of failed actions processed in a single batch during asynchronous retry operations. Developers can adjust this value to optimize performance or manage memory usage, especially for sites with a large number of failed automations.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Example of filtering the 'automator_async_retry_chunk_size' hook.
 *
 * This filter allows you to adjust the number of failed asynchronous actions
 * processed in a single chunk when retrying them. This can be useful for
 * managing server resources or performance during periods of high backlog.
 *
 * In this example, we're reducing the chunk size to 25, meaning only 25
 * failed actions will be processed at a time. This might be done to
 * prevent potential memory exhaustion on servers with limited resources.
 */
add_filter( 'automator_async_retry_chunk_size', 'my_custom_automator_retry_chunk_size', 10, 1 );

function my_custom_automator_retry_chunk_size( $chunk_size ) {
	// Reduce the chunk size for processing failed asynchronous actions to 25.
	// This helps to prevent potential memory or execution time issues
	// when dealing with a large number of failed actions.
	$new_chunk_size = 25;

	// You might also want to log that the filter is being applied,
	// especially during development or debugging.
	// error_log( "Automator async retry chunk size filtered from {$chunk_size} to {$new_chunk_size}." );

	return $new_chunk_size;
}

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/classes/async-actions.php:1513

public function maybe_retry_failed_actions() {

		if ( ! function_exists( 'as_get_scheduled_actions' ) ) {
			return;
		}

		$max_retries  = apply_filters( 'automator_async_max_retries', 3 );
		$chunk_size   = apply_filters( 'automator_async_retry_chunk_size', 50 );
		$memory_limit = function_exists( 'wp_convert_hr_to_bytes' )
			? (int) ( wp_convert_hr_to_bytes( ini_get( 'memory_limit' ) ) * 0.8 )
			: PHP_INT_MAX;

		// Process failed jobs in chunks to handle backlogs larger than a single page.
		// Track work done per iteration to avoid an infinite loop — the AS query
		// returns the same failed jobs every time (their status isn't mutated),
		// so once no job produces useful work we must exit.
		do {
			$failed_jobs = as_get_scheduled_actions(
				array(
					'hook'     => 'automator_async_run_with_hash',
					'status'   => ActionScheduler_Store::STATUS_FAILED,
					'per_page' => $chunk_size,
				)
			);

			if ( empty( $failed_jobs ) ) {
				break;
			}

			$processed = 0;

			foreach ( $failed_jobs as $job_id => $job ) {
				if ( $this->retry_failed_job( $job_id, $job, $max_retries ) ) {
					++$processed;
				}
			}

			// No jobs were retried or cleaned up — remaining jobs are stale
			// (hash already deleted, missing args, etc.). Exit to avoid infinite loop.
			if ( 0 === $processed ) {
				break;
			}

			// Bail if approaching memory limit — remaining jobs will be picked up next run.
			if ( memory_get_usage( true ) > $memory_limit ) {
				break;
			}
		} while ( count( $failed_jobs ) >= $chunk_size );
	}


Scroll to Top