Filter uncanny-automator-pro

automator_async_max_retries

Filters the maximum number of retries for asynchronous operations.

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

Description

Filters the maximum number of times a failed asynchronous action can be retried. Developers can use this hook to adjust the retry limit for asynchronous tasks within Uncanny Automator Pro, for example, to increase retries for less critical operations. The default is 3.


Usage

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

Return Value

The filtered value.


Examples

/**
 * Increase the maximum number of retries for failed async actions.
 *
 * This filter allows developers to customize how many times Uncanny Automator
 * will attempt to retry a failed asynchronous action before marking it as permanently failed.
 * The default is 3 retries.
 *
 * @param int The maximum number of retries allowed.
 */
add_filter(
	'automator_async_max_retries',
	function( $max_retries ) {
		// Increase the number of retries to 5 for critical automations.
		// This might be useful for automations that are essential for user experience
		// or system integrity, where a few temporary hiccups shouldn't lead to permanent failure.
		$new_max_retries = 5;

		// It's good practice to ensure the new value is at least as much as the default,
		// or to implement more complex logic based on other factors if needed.
		// For this example, we're simply setting it to a higher fixed value.
		return $new_max_retries;
	},
	10, // Priority: Run this filter relatively early.
	1   // Accepted args: The callback function accepts only one argument ($max_retries).
);

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:1512

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