Posted on

How to easily implement background processing in a WP website

Background processing can be a pretty handy feature on websites with huge traffic. Some WP plugins successfully use that to reduce the bloat of consuming tasks.

However, put up a totally new system for queuing tasks can be somewhat laborious. But no problem: fortunately, WooCommerce offers a ready-to-use library that you can rely on.

Action Scheduler

This is the name of the script that makes the background processing feature simple for WordPress. If you have WooCommerce installed, that should already working and you can just use it.

If you have a website that doesn’t run WooCommerce though, don’t worry: you can use it anyway.

You can install it using a plugin or,You can just require the library in a plugin of yours through Composer.

The library is pretty simple. It uses WP’s scheduled tasks in order to queue tasks that run eventually. That makes possible to run a task whenever it is possible or schedule it to a defined time. Also, you can set recurring tasks using cron.

WooCommerce processes a lot of tasks in the background. Of course you should use it just for some tasks, otherwise you incur in the risk of creating endless queues. However, some processes that don’t need to run immediately can be then scheduled to run later, causing no bloat.

Creating tasks

Using this library is something that involves the concept os task. Usually, in WP coding, the layer of tasks remains within the includes and is rarely exposed to the devs.

The wp-cron related functions, though, open a possibility to develop task-based coding, and using this library allows us to push new tasks to a queue. WooCommerce’s maintainers, who developed the library, explain how it works:

The scheduler will attempt to run every minute by attaching itself as a callback to the ‘action_scheduler_run_schedule’ hook, which is scheduled using WordPress’s built-in WP-Cron system. Once per minute on, it will also check on the ‘shutdown’ hook of WP Admin requests whether there are pending actions, and if there are, it will initiate a queue via an async loopback request.

Action Scheduler

So, the library provides functions to schedule tasks in four different ways:

Enqueue an action to run as soon as possibleEnqueue an action to run once, in a given timeEnqueue an action to run with a certain frequencyEnqueue an action to run in a cron-like schedule

Any of these ways has a particular function, which is used in a very similar way compared to wp_schedule_event() or any other WP cron related function. The short example provided by Action Scheduler developers shows that:

require_once( plugin_dir_path( __FILE__ ) . ‘/libraries/action-scheduler/action-scheduler.php’ );

/**
* Schedule an action with the hook ‘eg_midnight_log’ to run at midnight each day
* so that our callback is run then.
*/
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’ );
}
}
add_action( ‘init’, ‘eg_schedule_midnight_log’ );

/**
* A callback to run when the ‘eg_midnight_log’ scheduled action is run.
*/
function eg_log_action_data() {
error_log( ‘It is just after midnight on ‘ . date( ‘Y-m-d’ ) );
}
add_action( ‘eg_midnight_log’, ‘eg_log_action_data’ );

In the code before, the function as_has_scheduled_action checks whether the hook has been set or not. And if it didn’t, then it is set using as_schedule_recurring_action. If the schedule needs to run just once, then it needs to be set using as_schedule_single_action.

But maybe the best tool provided by Action Scheduler is this function: as_enqueue_async_action. Contrary to its siblings, this girl schedules an action to run “as soon as possible”, asynchronously.

The post How to easily implement background processing in a WP website appeared first on WordPress Tips, Tricks and Tools.

Leave a Reply

Your email address will not be published.