Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 2.4 KB

README.md

File metadata and controls

76 lines (53 loc) · 2.4 KB

WP Redis Job Queue

A simple Redis-based job queue for WordPress.

Basic usage

Install and activate this repo as a WP plugin. In a terminal, run the following:

wp queue process --delay=100

Note: You don't actually need the --delay=100 option. That's just to keep your fan from whirring too much when running this locally. :) Run wp queue process --help for more info.

Elsewhere, in your code, push a job onto it. A job can be any serializeable callable.

/* functions.php ...or wherever */
use WpRedisJobQueue as Q;

// imagine we need to sync user account info from an external system
function sync_user_account($id) {
  $externalId = get_user_meta($id, 'account_id', true);
  $info = expensive_api_call(['account_id' => $externalId]);
  update_user_meta($id, 'account_info', $info);
}

// sync on every successful login
add_action('wp_login', function($login, WP_User $user) {

  // don't make the user wait, fetch their account info asynchronously!
  Q\push([
    // callback is required
    'callback' => 'sync_user_account',

    // args are required for any callback that takes one or more params
    'args'     => [$user->ID],

    // you can define any other arbitrary parameters here, too:
    'additional_info' => [
      'ipv4_address'  => $_SERVER['REMOTE_ADDR'],
      'shot_first'    => 'Han',
    ],
    'initiated_by'    => 'wp_login',
  ]);
});

API

The WpRedisJobQueue namespace exposes a single function: push(). It takes an associative $job array as its sole argument.

Actions and Filters

TODO

Design

The Job Queue is implemented as a long-running WP-CLI command:

wp queue process --delay=1000 --debug
...
Debug: no tasks on the queue (445.908s)
Debug: no tasks on the queue (446.909s)
Debug: no tasks on the queue (447.909s)
...

The command runs an infinite loop, polling for jobs on a (FIFO) Redis queue. It pops any jobs it finds off the queue one a time, running each one, and then resuming the loop.

A job is just an array with a callable index and optional args. You can push an array with any serializeable callable onto the queue from your code (note that anonymous functions/closures are not serializeable, and cannot be pushed onto the queue as callbacks).

There is no CLI interface for pushing arbitrary tasks onto the queue. That is your code's job.

NOTE: To ensure reliability, use a monitoring tool to catch failures when the WP-CLI process