• Stars
    star
    1,025
  • Rank 44,763 (Top 0.9 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created almost 9 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

WordPress background processing class

WP Background Processing

WP Background Processing can be used to fire off non-blocking asynchronous requests or as a background processing tool, allowing you to queue tasks. Check out the example plugin or read the accompanying article.

Inspired by TechCrunch WP Asynchronous Tasks.

Requires PHP 5.6+

Install

The recommended way to install this library in your project is by loading it through Composer:

composer require deliciousbrains/wp-background-processing

It is highly recommended to prefix wrap the library class files using the Mozart package, to prevent collisions with other projects using this same library.

Usage

Async Request

Async requests are useful for pushing slow one-off tasks such as sending emails to a background process. Once the request has been dispatched it will process in the background instantly.

Extend the WP_Async_Request class:

class WP_Example_Request extends WP_Async_Request {

	/**
	 * @var string
	 */
	protected $prefix = 'my_plugin';

	/**
	 * @var string
	 */
	protected $action = 'example_request';

	/**
	 * Handle a dispatched request.
	 *
	 * Override this method to perform any actions required
	 * during the async request.
	 */
	protected function handle() {
		// Actions to perform.
	}

}

protected $prefix

Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.

protected $action

Should be set to a unique name.

protected function handle()

Should contain any logic to perform during the non-blocking request. The data passed to the request will be accessible via $_POST.

Dispatching Requests

Instantiate your request:

$this->example_request = new WP_Example_Request();

Add data to the request if required:

$this->example_request->data( array( 'value1' => $value1, 'value2' => $value2 ) );

Fire off the request:

$this->example_request->dispatch();

Chaining is also supported:

$this->example_request->data( array( 'data' => $data ) )->dispatch();

Background Process

Background processes work in a similar fashion to async requests, but they allow you to queue tasks. Items pushed onto the queue will be processed in the background once the queue has been saved and dispatched. Queues will also scale based on available server resources, so higher end servers will process more items per batch. Once a batch has completed, the next batch will start instantly.

Health checks run by default every 5 minutes to ensure the queue is running when queued items exist. If the queue has failed it will be restarted.

Queues work on a first in first out basis, which allows additional items to be pushed to the queue even if it’s already processing. Saving a new batch of queued items and dispatching while another background processing instance is already running will result in the dispatch shortcutting out and the existing instance eventually picking up the new items and processing them when it is their turn.

Extend the WP_Background_Process class:

class WP_Example_Process extends WP_Background_Process {

	/**
	 * @var string
	 */
	protected $prefix = 'my_plugin';

	/**
	 * @var string
	 */
	protected $action = 'example_process';

	/**
	 * Perform task with queued item.
	 *
	 * Override this method to perform any actions required on each
	 * queue item. Return the modified item for further processing
	 * in the next pass through. Or, return false to remove the
	 * item from the queue.
	 *
	 * @param mixed $item Queue item to iterate over.
	 *
	 * @return mixed
	 */
	protected function task( $item ) {
		// Actions to perform.

		return false;
	}

	/**
	 * Complete processing.
	 *
	 * Override if applicable, but ensure that the below actions are
	 * performed, or, call parent::complete().
	 */
	protected function complete() {
		parent::complete();

		// Show notice to user or perform some other arbitrary task...
	}

}

protected $prefix

Should be set to a unique prefix associated with your plugin, theme, or site's custom function prefix.

protected $action

Should be set to a unique name.

protected function task( $item )

Should contain any logic to perform on the queued item. Return false to remove the item from the queue or return $item to push it back onto the queue for further processing. If the item has been modified and is pushed back onto the queue the current state will be saved before the batch is exited.

protected function complete()

Optionally contain any logic to perform once the queue has completed.

Dispatching Processes

Instantiate your process:

$this->example_process = new WP_Example_Process();

Note: You must instantiate your process unconditionally. All requests should do this, even if nothing is pushed to the queue.

Push items to the queue:

foreach ( $items as $item ) {
    $this->example_process->push_to_queue( $item );
}

Save and dispatch the queue:

$this->example_process->save()->dispatch();

Background Process Status

A background process can be queued, processing, paused, cancelled, or none of the above (not started or has completed).

Queued

To check whether a background process has queued items use is_queued().

if ( $this->example_process->is_queued() ) {
    // Do something because background process has queued items, e.g. add notice in admin UI.
}
Processing

To check whether a background process is currently handling a queue of items use is_processing().

if ( $this->example_process->is_processing() ) {
    // Do something because background process is running, e.g. add notice in admin UI.
}
Paused

You can pause a background process with pause().

$this->example_process->pause();

The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron and firing a "paused" action.

To check whether a background process is currently paused use is_paused().

if ( $this->example_process->is_paused() ) {
    // Do something because background process is paused, e.g. add notice in admin UI.
}

You can perform an action in response to background processing being paused by handling the "paused" action for the background process's identifier ($prefix + $action).

add_action( 'my_plugin_example_process_paused', function() {
    // Do something because background process is paused, e.g. add notice in admin UI.
});

You can resume a background process with resume().

$this->example_process->resume();

You can perform an action in response to background processing being resumed by handling the "resumed" action for the background process's identifier ($prefix + $action).

add_action( 'my_plugin_example_process_resumed', function() {
    // Do something because background process is resumed, e.g. add notice in admin UI.
});
Cancelled

You can cancel a background process with cancel().

$this->example_process->cancel();

The currently processing batch will continue until it either completes or reaches the time or memory limit. At that point it'll unlock the process and either complete the batch if the queue is empty, or perform a dispatch that will result in the handler removing the healthcheck cron, deleting all batches of queued items and firing a "cancelled" action.

To check whether a background process is currently cancelled use is_cancelled().

if ( $this->example_process->is_cancelled() ) {
    // Do something because background process is cancelled, e.g. add notice in admin UI.
}

You can perform an action in response to background processing being cancelled by handling the "cancelled" action for the background process's identifier ($prefix + $action).

add_action( 'my_plugin_example_process_cancelled', function() {
    // Do something because background process is paused, e.g. add notice in admin UI.
});

The "cancelled" action fires once the queue has been cleared down and cancelled status removed. After which is_cancelled() will no longer be true as the background process is now dormant.

Active

To check whether a background process has queued items, is processing, is paused, or is cancelling, use is_active().

if ( $this->example_process->is_active() ) {
    // Do something because background process is active, e.g. add notice in admin UI.
}

If a background process is not active, then it either has not had anything queued yet and not started, or has finished processing all queued items.

BasicAuth

If your site is behind BasicAuth, both async requests and background processes will fail to complete. This is because WP Background Processing relies on the WordPress HTTP API, which requires you to attach your BasicAuth credentials to requests. The easiest way to do this is using the following filter:

function wpbp_http_request_args( $r, $url ) {
	$r['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );

	return $r;
}
add_filter( 'http_request_args', 'wpbp_http_request_args', 10, 2);

Contributing

Contributions are welcome via Pull Requests, but please do raise an issue before working on anything to discuss the change if there isn't already an issue. If there is an approved issue you'd like to tackle, please post a comment on it to let people know you're going to have a go at it so that effort isn't wasted through duplicated work.

Unit & Style Tests

When working on the library, please add unit tests to the appropriate file in the tests directory that cover your changes.

Setting Up

We use the standard WordPress test libraries for running unit tests.

Please run the following command to set up the libraries:

bin/install-wp-tests.sh db_name db_user db_pass

Substitute db_name, db_user and db_pass as appropriate.

Please be aware that running the unit tests is a destructive operation, database tables will be cleared, so please use a database name dedicated to running unit tests. The standard database name usually used by the WordPress community is wordpress_test, e.g.

bin/install-wp-tests.sh wordpress_test root root

Please refer to the Initialize the testing environment locally section of the WordPress Handbook's Plugin Integration Tests entry should you run into any issues.

Running Unit Tests

To run the unit tests, simply run:

make test-unit

If the composer dependencies aren't in place, they'll be automatically installed first.

Running Style Tests

It's important that the code in the library use a consistent style to aid in quickly understanding it, and to avoid some common issues. PHP_Code_Sniffer is used with mostly standard WordPress rules to help check for consistency.

To run the style tests, simply run:

make test-style

If the composer dependencies aren't in place, they'll be automatically installed first.

Running All Tests

To make things super simple, just run the following to run all tests:

make

If the composer dependencies aren't in place, they'll be automatically installed first.

License

GPLv2+

More Repositories

1

wordpress-nginx

Nginx server configurations for WordPress
DIGITAL Command Language
542
star
2

wp-migrate-db

WordPress plugin that exports your database, does a find and replace on URLs and file paths, then allows you to save it to your computer.
PHP
341
star
3

wp-amazon-s3-and-cloudfront

Automatically copies media uploads to Amazon S3 for delivery. Optionally configure Amazon CloudFront for even faster delivery.
JavaScript
304
star
4

wp-image-processing-queue

Resize WordPress images in the background
PHP
203
star
5

wp-migrations

WordPress library for managing database table schema upgrades and data seeding
PHP
190
star
6

sqlbuddy

phpMyAdmin alternative with a focus on usability
PHP
188
star
7

wp-queue

Job queues for WordPress
PHP
154
star
8

wp-migrate-db-pro-tweaks

Examples of using WP Migrate DB Pro's filters
PHP
98
star
9

spinupwp-composer-site

A WordPress site setup using Composer that is primed and ready to be hosted using SpinupWP.
PHP
82
star
10

better-search-replace

A simple plugin for updating URLs or other text in a database.
PHP
82
star
11

wp-plugin-build

Shell script we use to create builds of our WordPress plugins.
PHP
68
star
12

serialized-editor

A Vue.js component for editing data that has been serialized in PHP
Vue
55
star
13

wp-amazon-s3-and-cloudfront-tweaks

Examples of using Amazon S3 and CloudFront's filters
PHP
53
star
14

wpfbbotkit

A messenger bot developer framework for WordPress
PHP
51
star
15

wp-migrate-db-anonymization

An addon for WP Migrate DB Pro that anonymizes user data
PHP
46
star
16

wp-dbi-file-uploader

A basic JavaScript file uploader plugin that can upload large files
PHP
45
star
17

wp-amazon-web-services

Houses the Amazon Web Services (AWS) PHP libraries and manages access keys. Required by other AWS plugins.
PHP
42
star
18

wp-phpstorm-code-styles

Delicious Brains WordPress coding styles for PhpStorm
25
star
19

npm-scripts

SCSS
23
star
20

spinupwp-plugin

PHP
20
star
21

wp-auto-login

WordPress library for generating automatic login URLs for users
PHP
16
star
22

wp-post-types

WordPress library for registering, reading and writing custom post types.
PHP
13
star
23

spinupwp-mu-plugin

PHP
13
star
24

wp-offload-ses-lite

WP Offload SES Lite sends all outgoing WordPress emails through Amazon Simple Email Service (SES) instead of the local wp_mail() function.
PHP
13
star
25

wp-filesystem

A wrapper for the WP Filesystem
PHP
11
star
26

wp-db-backup

PHP
11
star
27

wp-post-series

WordPress library for adding a series support to posts
PHP
9
star
28

mergebot-schemas

Contains all the WordPress core, plugin and theme schemas for the Mergebot application.
PHP
9
star
29

wp-testimonials

WordPress must-use plugin for managing testimonials, importing from tweets and displaying them.
PHP
7
star
30

spinupwp-cli

The official command-line tool for SpinupWP.
PHP
6
star
31

wp-post-promoter

WordPress must-use plugin for promoting posts via email and social media.
PHP
6
star
32

spinupwp-php-sdk

The official SpinupWP PHP SDK.
PHP
5
star
33

github-gardener

Gardening of GitHub issues and pull requests
PHP
5
star
34

finance-app-trial-project

PHP
4
star
35

wordpress-ansible

Ansible playbook for provisioning WordPress servers
4
star
36

wp-cli-up

wp-cli-up
PHP
4
star
37

wp-mergebot

PHP
3
star
38

testdriiive

PHP
3
star
39

mergebot-schema-generator

Generate Mergebot schema files for WordPress plugins, themes, or core
PHP
2
star
40

woocommerce-subscriptions-renew-active

Abandoned (see below). Extends WooCommerce Subscriptions plugin to enable manual renewals of active subscriptions
PHP
2
star
41

wp-migrate-db-pro-bugs

Bug tracking for WP Migrate DB Pro beta release
2
star
42

wp-markdown

PHP
1
star
43

gp-login-with-email

GlotPress plugin to allow login with the user's email
PHP
1
star