Filter uncanny-automator-pro

automator_pro_auto_purge_chunk_size

Filters the chunk size used for batch processing in the automation purging system.

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

Description

This filter allows developers to modify the number of database rows processed per chunk during automatic activity log purging. Adjust this value to optimize performance or manage memory usage during large-scale log cleanup operations. The default is 100.


Usage

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

Return Value

The filtered value.


Examples

<?php

/**
 * Example of how to filter the 'automator_pro_auto_purge_chunk_size' hook.
 *
 * This filter allows you to modify the number of log entries that are
 * processed in each chunk during the automatic log purging process.
 *
 * In this example, we're increasing the chunk size from the default
 * (self::PURGE_CHUNK_SIZE) to 500 to potentially speed up purging
 * on systems with ample memory.
 *
 * @param int $chunk_size The current chunk size.
 * @return int The modified chunk size.
 */
add_filter( 'automator_pro_auto_purge_chunk_size', function ( $chunk_size ) {
	// Increase the chunk size to 500 for faster purging if the server has enough memory.
	// You might want to adjust this value based on your server's performance.
	$new_chunk_size = 500;

	// It's good practice to ensure the new chunk size is at least 1.
	return max( 1, $new_chunk_size );
}, 10, 1 );

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/extensions/activity-log-settings.php:190

public function delete_records( $purge_value = 1, $unit = 'days' ) {

		global $wpdb;

		$finished_statuses = apply_filters( 'automator_pro_auto_purge_logs_statuses_to_remove', Automator_Status::get_finished_statuses() );

		// Optionally not remove "Completed with errors".
		if ( true === apply_filters( 'automator_pro_auto_purge_logs_exclude_completed_with_errors', true ) ) {
			foreach ( $finished_statuses as $k => $v ) {
				if ( 2 === (int) $v ) {
					unset( $finished_statuses[ $k ] );
				}
			}
		}

		$datetime_base = $this->get_datetime_base( $purge_value, $unit );
		$previous_time = apply_filters( 'automator_pro_auto_purge_logs_previous_time', $datetime_base, $purge_value );
		$chunk_size    = apply_filters( 'automator_pro_auto_purge_chunk_size', self::PURGE_CHUNK_SIZE );
		$memory_limit  = $this->get_memory_limit_bytes() * self::MEMORY_THRESHOLD;

		$placeholders = implode( ',', array_fill( 0, count( $finished_statuses ), '%d' ) );

		do {
			// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
			$recipes = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT `ID`, `automator_recipe_id`, `run_number`
					FROM {$wpdb->prefix}uap_recipe_log
					WHERE `date_time` < %s
						AND `completed` IN ({$placeholders})
					LIMIT %d",
					array_merge( array( $previous_time ), $finished_statuses, array( $chunk_size ) )
				)
			);

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

			foreach ( $recipes as $recipe ) {

				$recipe_id               = absint( $recipe->automator_recipe_id );
				$automator_recipe_log_id = absint( $recipe->ID );
				$run_number              = absint( $recipe->run_number );

				// Purge recipe logs.
				automator_purge_recipe_logs( $recipe_id, $automator_recipe_log_id );
				// Purge trigger logs.
				automator_purge_trigger_logs( $recipe_id, $automator_recipe_log_id );
				// Purge action logs.
				automator_purge_action_logs( $recipe_id, $automator_recipe_log_id );
				// Purge closure logs.
				automator_purge_closure_logs( $recipe_id, $automator_recipe_log_id );

				do_action( 'automator_recipe_log_deleted', $recipe_id, $automator_recipe_log_id, $run_number );
			}

			// If approaching memory limit, reschedule remaining work and bail.
			// Guard against duplicate scheduling — only queue if no pending run exists.
			if ( memory_get_usage( true ) > $memory_limit ) {
				if ( false === as_next_scheduled_action( self::$cron_schedule, array(), 'uncanny-automator' ) ) {
					as_schedule_single_action( time() + 60, self::$cron_schedule, array(), 'uncanny-automator' );
				}
				return;
			}
		} while ( count( $recipes ) >= $chunk_size );
	}


Scroll to Top