Saturday, May 21st, 2022

How to easily implement background processing in WP website

Background processing can be a very handy feature on websites with heavy traffic. Some WP plugins successfully use this to reduce the swelling of consuming tasks.

However, setting up an entirely new system for queuing tasks can be somewhat laborious. But no problem: Fortunately, WooCommerce offers a ready-to-use library you can rely on.

action scheduler

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

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

  • You can install it using a plugin or,
  • You can simply require the library in your plugin via composer.

The library is quite simple. It uses WP’s scheduled tasks to queue up the tasks that will eventually run. This makes it possible to run a task whenever possible or to schedule it at a specified time. In addition, you can set up recurring tasks using cron.

WooCommerce processes a lot of tasks in the background. Of course you should only use it for certain tasks, otherwise you risk creating endless queues. However, some processes that do not need to be run immediately can be scheduled to run later, thereby not causing any bloat.

create task

The use of this library is something that includes the concept of os Work, Usually, in WP coding, the functions layer resides within the include and is rarely exposed to devs.

However, wp-cron related tasks open up the possibility of developing task-based coding, and using this library allows us to push new tasks to a queue. The maintainers of WooCommerce, who developed the library, explain how it works:

The scheduler will attempt to run every minute by attaching itself as a callback 'action_scheduler_run_schedule' Hooks, which are scheduled using WordPress’ built-in WP-Cron system. once per minute, it will also check 'shutdown' The WP admin hook requests whether there are pending tasks, and if there are, it will start a queue via an async loopback request.

action scheduler

Therefore, the library provides functions for scheduling tasks in four different ways:

  1. Queue the action to run as fast as possible
  2. Attach an action to run once in a certain amount of time
  3. Enqueue an action to run with a certain frequency
  4. Enqueue an action to run in a cron-like schedule

Either of these methods has a special function, which is used in a very similar way than wp_schedule_event() or any other WP cron related function. The short example provided by the 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 first code, the function as_has_scheduled_action Checks whether the hook has been set. and if it doesn’t, it is set using as_schedule_recurring_action, If the schedule needs to be run only once, it needs to be set using as_schedule_single_action,

But probably the best tool provided by Action Scheduler is this task: as_enqueue_async_action, Unlike her siblings, this girl sets an action to run “as fast as possible” asynchronously.

Source link