Action Scheduler is a library for triggering WordPress hooks at some time in the future (or as soon as possible in the case of asynchronous operations). Each hook can be scheduled with unique data so that callbacks can perform actions on that data. Hooks can also be scheduled to run on one or more occasions.
We can think of this as an extension to do_action(), adding the ability to delay and repeat hooks.
As it happens, this feature also provides a powerful backend processing tool for handling large task queues in WordPress. It also provides traceability of tasks processed in the background by adding logging and a management interface.
Proven backend processing
Every month, Action Scheduler handles millions of subscription payments, WooCommerce webhooks, and a host of other plugin emails and other events.
On real websites, it has been proven to handle queues of more than 50,000 tasks and perform resource-intensive operations such as processing payments and creating orders in 10 concurrent queues at a rate of more than 10,000 operations per hour without negatively impacting normal website operations.
All of this can be done on infrastructure and WordPress sites outside of the plugin author's control.
Action Scheduler is designed for distribution within WordPress plugins (and themes) - no server access required. If your plugin requires background processing, especially for high-volume tasks, Action Scheduler can help.
How Action Scheduler works
Action Scheduler Stores the hook name, parameters, and scheduled date of an action that should be triggered at some time in the future.
The scheduler will attempt to run every minute by attaching itself as a callback to the action_scheduler_run_schedule
This is accomplished with a hook that uses WordPress' built-in WP-Cron system for scheduling. Every minute it will also add a new hook to the WP Admin request for the shutdown
The hook checks to see if there is a pending action on the hook, and if there is, it will start the queue with an asynchronous circular request.
When triggered, the Action Scheduler checks for scheduled actions that are scheduled to run at or before the current time, i.e., actions that are scheduled to run now or sometime in the past. Asynchronously scheduled actions, i.e., unscheduled actions, have a zero date, meaning that they are always due regardless of when they are checked.
Batch background jobs
If there are pending actions, the Action Scheduler will uniquely declare a batch of 25 actions and begin processing the batch. The spawned PHP process will continue to process the batch of 25 actions until the available memory of 90% has been used or until 30 seconds have elapsed.
At this point, if there are other actions to be processed, an asynchronous circular request is issued to the site to continue processing the action in a new request.
This process and recurring requests will continue until all actions have been processed.
routine maintenance
Before processing the batch, the scheduler removes any action statement that exists for more than five minutes (or more specifically, 10 times the allowed time limit, which defaults to 30 seconds).
The Action Scheduler also removes any actions that have been completed or canceled for more than a month.
If an action runs for more than 5 minutes, the Action Scheduler will assume that the action has timed out and mark it as failed. However, if all callbacks attached to the action successfully complete after the timeout, its status will be updated to complete later.
Usage
We can change the library asPluginsinstalled, or installed directly as a library in our theme or plugin. Once installed, we can schedule and execute the task plan.
Plan for the organization of the mission
In the following code, we use the hookeg_midnight_log
Schedule the operation to run at midnight every day. When this operation is run, the functions mounted to the hook eg_midnight_log are executed.
function eg_schedule_midnight_log() {
if ( false === as_has_scheduled_action( 'eg_midnight_log' ) )
as_schedule_recurring_action( strtotime( 'tomorrow' ), DAY_IN_SECONDS, 'eg_midnight_log', array(), '', true );
}
}
add_action( 'init', 'eg_schedule_midnight_log' );
function eg_log_action_data() {
error_log( 'It is just after midnight on ' . date( 'Y-m-d' ) ); function eg_log_action_data( ); error_log( 'It is just after midnight on ' .
}
add_action( 'eg_midnight_log', 'eg_log_action_data' ); }
Transfer parameters
If we need to pass parameters to a function that performs an operation to add a task plan, we need to pass the parameters to the task plan in the form of an array, and at the same time, the function that performs this operation must also accept the same number of arrays.
add_action( 'purchase_notification', 'send_purchase_notification', 10, 2 );
as_schedule_single_action( time(), 'purchase_notification', array(
'bob@foo.bar'.
'Learning Action Scheduler (e-book)', .
) ).
function send_purchase_notification( $customer_email, $purchased_item ) {
wp_mail(
$customer_email,
'Thank you!
"You purchased $purchased_item successfully."
);
}
Traceable back-office processing
Are our background jobs running? Take the guesswork out of it with Action Scheduler's built-in logging capabilities. All events for each action are logged in the actionscheduler_logs table and displayed in the admin interface.
Events logged by default include:
- When the action is created
- When the action starts (including details on how to run it, e.g. via WP CLI or WP Cron)
- perfect tense (grammar)
- When the action fails
If it fails due to an error, we can view the logs recorded in the error log in the administration interface, allowing us to track down errors that have occurred in the past on sites that we do not have access to.