• This repository has been archived on 02/May/2024
  • Stars
    star
    231
  • Rank 173,434 (Top 4 %)
  • Language
    PHP
  • License
    GNU General Publi...
  • Created about 10 years ago
  • Updated 7 months ago

Reviews

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

Repository Details

ARCHIVED: Job Queue for WordPress

WP Minions

Provides a framework for using job queues with WordPress for asynchronous task running. Provides an integration with Gearman and RabbitMQ out of the box.

Support Level Build Status Release Version GPLv2 License

Background & Purpose

As WordPress becomes a more popular publishing platform for increasingly large publishers, with complex workflows, the need for increasingly complex and resource-intensive tasks has only increased. Things like generating reports, expensive API calls, syncing users to mail providers, or even ingesting content from feeds all take a lot of time or a lot of memory (or both), and commonly can't finish within common limitations of web servers, because things like timeouts and memory limits get in the way.

WP Minions provides a few helper functions that allow you to add tasks to a queue, and specify an action that should be called to trigger the task, just hook a callback into the action using add_action()

During configuration, a number of minions (workers) are specified. As minions are free, they will take the next task from the queue, call the action, and any callbacks hooked into the action will be run.

In the situation of needing more ram or higher timeouts, a separate server to process the tasks is ideal - Just set up WordPress on that server like the standard web servers, and up the resources. Make sure not to send any production traffic to the server, and it will exclusively handle tasks from the queue.

Installation

  1. Install the plugin in WordPress. If desired, you can download a zip and install via the WordPress plugin installer.

  2. Create a symlink at the site root (the same directory as wp-settings.php) that points to the wp-minions-runner.php file in the plugin (or copy the file, but a symlink will ensure it is updated if the plugin is updated)

  3. Define a unique salt in wp-config.php so that multiple installs don't conflict.

define( 'WP_ASYNC_TASK_SALT', 'my-unique-salt-1' );

Note: If you are using multisite, you'll also have to add the following to your wp-config.php file, after the block with the multisite definitions. This is due to the fact that multisite relies on HTTP_HOST to detect the site/blog it is running under. You'll also want to make sure you are actually defining DOMAIN_CURRENT_SITE in the multisite configuration.

if ( ! isset( $_SERVER['HTTP_HOST'] ) && defined( 'DOING_ASYNC' ) && DOING_ASYNC ) {
  $_SERVER['HTTP_HOST'] = DOMAIN_CURRENT_SITE;
}
  1. Next, you'll need to choose your job queue system. Gearman and RabbitMQ are supported out of the box.

Gearman

There are a few parts to get this all running. First, the Gearman backend needs to be setup - this part will vary depending on your OS. Once that is complete, we can install the WordPress plugin, and set the configuration options for WordPress.

Backend Setup - CentOS 6.x

  1. You'll need the EPEL repo for gearman, and the REMI repo for some of the php packages. Make sure to enable the appropriate remi repo for the version of php you are using.
    • wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm && rpm -Uvh epel-release-6*.rpm
    • wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm && rpm -Uvh remi-release-6*.rpm
    • rm *.rpm
  2. Make sure that remi is enabled, as well as any specific version of php you may want in /etc/yum.repos.d/remi.repo
  3. yum install gearmand php-pecl-gearman python-pip
  4. easy_install supervisor
  5. chkconfig supervisord on && chkconfig gearmand on
  6. If everything is running on one server, I would recommend limiting connections to localhost only. If not, you'll want to set up firewall rules to only allow certain clients to connect on the Gearman port (Default 4730)
  • edit /etc/sysconfig/gearmand - set OPTIONS="--listen=localhost"
  1. service gearmand start

Backend Setup - CentOS 7.x

  1. You'll need the EPEL repo for gearman, and the REMI repo for some of the php packages.
    • yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    • yum install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
  2. yum install gearmand php-pecl-gearman --enablerepo=remi-php<php version on your system>. For example, if you are using php 7.2 your command would look like this yum install gearmand php-pecl-gearman --enablerepo=remi-php72
  3. Optionally, install supervisord if you prefer it
    • yum install python-pip
    • easy_install supervisor
  4. If everything is running on one server, I would recommend limiting connections to localhost only. If not, you'll want to set up firewall rules to only allow certain clients to connect on the Gearman port (Default 4730)
    • edit /etc/sysconfig/gearmand - set OPTIONS="--listen=localhost"
  5. systemctl enable gearmand
  6. systemctl start gearmand

Backend Setup - Ubuntu

As you go through this, you may need to install additional packages, if you do not have them already, such as php-pear or a php*-dev package

  1. apt-get update
  2. apt-get install gearman python-pip libgearman-dev supervisor
  3. pecl install gearman
  4. Once pecl install is complete, it will tell you to place something like extension=gearman.so into your php.ini file - Do this.
  5. update-rc.d gearman-job-server defaults && update-rc.d supervisor defaults
  6. If everything is running on one server, I would recommend limiting connections to localhost only. If not, you'll want to set up firewall rules to only allow certain clients to connect on the Gearman port (Default 4730)
  • edit /etc/default/gearman-job-server - set PARAMS="--listen=localhost"
  1. service gearman-job-server restart

Supervisor Configuration

Filling in values in <brackets> as required, add the following config to either /etc/supervisord.conf (CentOS) or /etc/supervisor/supervisord.conf (Ubuntu)

[program:my_wp_minions_workers]
command=/usr/bin/env php <path_to_wordpress>/wp-minions-runner.php
process_name=%(program_name)s-%(process_num)02d
numprocs=<number_of_minions>
directory=<path_to_temp_directory>
autostart=true
autorestart=true
killasgroup=true
user=<user>
  • path_to_wordpress: Absolute path to the root of your WordPress install, ex: /var/www/html/wordpress
  • number_of_minions: How many minions should be spawned (How many jobs can be running at once).
  • path_to_temp_directory: probably should just be the same as path_to_wordpress.
  • user: The system user to run the processes under, probably apache (CentOS), nginx (CentOS), or www-data (Ubuntu).
  • You can optionally change the "my_wp_minions_workers" text to something more descriptive, if you'd like.

After updating the supervisor configuration, restart the service (CentOS or Ubuntu)

systemctl restart supervisord
service supervisor restart

Systemd Configuration (CentOS 7.x)

Filling in values in <brackets> as required, add the following to /etc/systemd/system/[email protected]

[Unit]
Description=WP-Minions Runner %i
After=network.target

[Service]
PIDFile=/var/run/wp-minions-runner.%i.pid
User=<user>
Type=simple
ExecStart=/usr/bin/env php <path_to_wordpress>/wp-minions-runner.php
Restart=always

[Install]
WantedBy=multi-user.target
  • path_to_wordpress: Absolute path to the root of your WordPress install, ex: /var/www/html/wordpress
  • user: The system user to run the processes under, probably apache (CentOS), nginx (CentOS), or www-data (Ubuntu).

Reload systemd:

systemctl daemon-reload

Enable and start as many runners as you'd like to have running:

systemctl enable wp-minions-runner@{1..n}
systemctl start wp-minions-runner@{1..n}

Where 'n' is the number of processes you want.

WordPress Configuration

Define the WP_MINIONS_BACKEND constant in your wp-config.php. Valid values are gearman or rabbitmq. If left blank, it will default to a cron client.

define( 'WP_MINIONS_BACKEND', 'gearman' );

If your job queue service not running locally or uses a non-standard port, you'll need define your servers in wp-config.php

# Gearman config
global $gearman_servers;
$gearman_servers = array(
  '127.0.0.1:4730',
);
# RabbitMQ config
global $rabbitmq_server;
$rabbitmq_server = array(
  'host'     => '127.0.0.1',
  'port'     => 5672,
  'username' => 'guest',
  'password' => 'guest',
);

Note: On RabbitMQ the guest/guest account is the default administrator account, RabbitMQ will only allow connections connections on that account from localhost. Connections to any non-loopback address will be denied. See the RabbitMQ manual on [user management](https://www.rabbitmq.com/rabbitmqctl.8.html#User_Management) and [Access Control](https://www.rabbitmq.com/rabbitmqctl.8.html#Access_Control) for information on adding users and allowing them access to RabbitMQ resources.

## MySQL Persistent Queue (Recommended)

By default, gearman will store the job queue in memory. If for whatever reason the gearman service goes away, so does the queue. For persistence, you can optionally use a MySQL database for the job queue:

#### CentOS

Edit the gearman config at ```/etc/sysconfig/gearmand```, adding the following to the OPTIONS line (or creating the line, if it doesn't exist yet), inserting database credentials as necessary:
```sh
OPTIONS="-q MySQL --mysql-host=localhost --mysql-port=3306 --mysql-user=<user> --mysql-password=<password> --mysql-db=gearman --mysql-table=gearman_queue"

Ubuntu

Edit the gearman config at /etc/default/gearman-job-server, adding the following to the PARAMS line (or creating the line, if it doesn't exist yet), inserting database credentials as necessary:

PARAMS="-q MySQL --mysql-host=localhost --mysql-port=3306 --mysql-user=<user> --mysql-password=<password> --mysql-db=gearman --mysql-table=gearman_queue"

Note: For some setups, the above will not work as /etc/default/gearman-job-server does not get read. If you don't see the persistent queue setup then:

  1. Create a gearman db in mysql (the database must be present in the database, but when gearmand is initialized the first time it will create the table).
  2. Create a file in /etc/gearmand.conf
  3. In the file paste the configuration all on one line:
-q MySQL --mysql-host=localhost --mysql-port=3306 --mysql-user=<user> --mysql-password=<password> --mysql-db=gearman --mysql-table=gearman_queue

Then restart the gearman-job-server: sudo service gearman-job-server restart.

Verification

Once everything is installed, you can quickly make sure your job queue is accepting jobs with the test-client.php and test-worker.php files located in the system-tests/YOURQUEUE directory. The worker is configured to reverse any text passed to it. In the client file, we pass "Hello World" to the worker.

In one window, run php test-worker.php - You'll now have one worker process running, waiting for jobs.

In another window, run php test-client.php "Hello World" - You should see "dlroW olleH" printed on your screen.

ctrl-c will stop the worker once you are done testing.

Usage

Once configured and activated, you'll have access to wp_async_task_add(). If you are at all familiar with wp_schedule_single_event(), the way wp_async_task_add() works should be very familiar to you.

The function takes up to three arguments, the first of which is required:

  1. $hook: This is the name of the action hook to execute when the job runs. Your callback function should hook into this with add_action( $hook, $callback )
  2. $args: This is optional, and defaults to an empty array. You can pass an array of arbitrary data to this, and it will be passed to your callback function.
  3. $priority: This is optional, and defaults to "normal". Other valid options are "high" or "low". High priority jobs will be run before normal priority jobs, even if they normal priority job has been in the queue longer.

Set an option in the database, when a worker becomes available:

// Add a task, that will call the "myplugin_update_option" action when it is run
wp_async_task_add( 'myplugin_update_option', array( 'mykey' => 'myvalue' ) );

function myplugin_update_option_callback( $args ) {
	// In reality, you are probably doing a lot of resource intensive work here
	update_option( 'my-option-name', $args['mykey'] );
}

// Add the action that links the task and the callback.
// Notice the hook below is the same as the hook provided to wp_async_task_add.
add_action( 'myplugin_update_option', 'myplugin_update_option_callback' );

Once a worker is free, and runs the above task, you'd have an option called "my-option-name" in the options table, with a value of "myvalue", since "myvalue" was passed in via the $args

Customization

The following constants can be used to customize the behaviour of WP Minions.

  1. WP_MINIONS_JOBS_PER_WORKER - The number of jobs to execute per Worker, default is 1. Running multiple jobs per worker will reduce the number workers spawned, and can significantly boost performance. However too large a value will cause issues if you have memory leaks. Use with caution.

  2. WP_MINIONS_CLIENT_CLASS - You can also alter the Client class used to send jobs to your job queue. It should match the interface of \WpMinions\Client.

  3. WP_MINIONS_WORKER_CLASS - Similarly you can alter the Worker class used to execute jobs. It should match the interface of \WpMinions\Worker.

Issues

If you identify any errors or have an idea for improving the plugin, please open an issue. We're excited to see what the community thinks of this project, and we would love your input!

Support Level

Stable: 10up is not planning to develop any new features for this, but will still respond to bug reports and security concerns. We welcome PRs, but any that include new features should be small and easy to integrate and should not include breaking changes. We otherwise intend to keep this tested up to the most recent version of WordPress.

Like what you see?

More Repositories

1

ElasticPress

A fast and flexible search and query engine for WordPress.
PHP
1,242
star
2

Engineering-Best-Practices

10up Engineering Best Practices
SCSS
757
star
3

wp-local-docker

****** WP Local Docker V2 is now available - https://github.com/10up/wp-local-docker-v2
Shell
748
star
4

wp_mock

WordPress API Mocking Framework
PHP
674
star
5

distributor

Share content between your websites.
PHP
628
star
6

classifai

Supercharge WordPress Content Workflows and Engagement with Artificial Intelligence.
PHP
567
star
7

action-wordpress-plugin-deploy

Deploy your plugin to the WordPress.org repository using GitHub Actions
Shell
521
star
8

wp-local-docker-v2

ARCHIVED: A simple Docker based development environment for WordPress.
JavaScript
484
star
9

actions-wordpress

GitHub Actions for WordPress!
Shell
419
star
10

MU-Migration

This WP-CLI plugin makes the process of moving sites from single WordPress sites to a Multisite instance (or vice-versa) much easier. It exports everything into a zip package which can be used to automatically import it within the desired Multisite installation.
PHP
324
star
11

safe-redirect-manager

A simple HTTP redirection plugin for WordPress.
PHP
311
star
12

wpcli-vulnerability-scanner

WP-CLI command for checking installed plugins and themes for vulnerabilities reported on wpvulndb.com
PHP
274
star
13

wp-component-library

A library of barebones front-end components built with WordPress and accessibility in mind.
HTML
270
star
14

safe-svg

Enable SVG uploads and sanitize them to stop XML/SVG vulnerabilities in your WordPress website.
PHP
263
star
15

grunt-wp-theme

WordPress Theme Project Templates
JavaScript
252
star
16

SketchPress

Sketch template for quickly creating awesome WordPress admin interface mockups and designs.
251
star
17

block-components

A collection of components built to be used in the block editor
TypeScript
251
star
18

restricted-site-access

Limit access to visitors who are logged in or allowed by IP addresses. Includes many options for handling blocked visitors.
PHP
228
star
19

wpsnapshots

(DEPRECATED) A project sharing tool for WordPress.
PHP
216
star
20

simple-local-avatars

Adds an avatar upload field to user profiles. Generates requested sizes on demand just like Gravatar!
PHP
213
star
21

wp-scaffold

10up WordPress project scaffold.
PHP
192
star
22

generator-wp-make

A Yeoman generator for making WordPress things
JavaScript
184
star
23

theme-scaffold

DEPRECATED. Use https://github.com/10up/wp-scaffold
PHP
184
star
24

grunt-wp-plugin

DEPRECATED: Please use https://github.com/10up/generator-wp-make instead.
JavaScript
183
star
25

simple-podcasting

A simple podcasting solution for WordPress.
PHP
179
star
26

nodeifywp

Powerful framework plugin for turning your WordPress theme into an isomorphic JavaScript application.
PHP
171
star
27

headstartwp

Build a headless website fast with WordPress, the world’s most popular CMS, and Next.js, the most popular React framework. A free and open source solution by the experts at 10up.
TypeScript
160
star
28

wp-hammer

ARCHIVED: Please see https://github.com/10up/wp-scrubber as replacement.
PHP
150
star
29

wpacceptance

ARCHIVED: A team scalable solution for reliable WordPress acceptance testing.
PHP
148
star
30

convert-to-blocks

Convert to Blocks is a WordPress plugin that transforms classic editor content to blocks on the fly.
PHP
147
star
31

simple-page-ordering

Order your pages and other hierarchical post types with simple drag and drop right from the standard page list.
PHP
146
star
32

plugin-scaffold

DEPRECATED. Use https://github.com/10up/wp-scaffold
PHP
133
star
33

10up-experience

The 10up Experience plugin configures WordPress to better protect and inform clients, aligned to 10up’s best practices.
PHP
129
star
34

Async-Transients

ARCHIVED: Transients that serve stale data while regenerating the new transients in the background.
PHP
124
star
35

action-wordpress-plugin-asset-update

Update your plugin readme and assets in the WordPress.org repository outside of new releases
Shell
117
star
36

autoshare-for-twitter

Automatically shares the post title or custom message and a link to the post to X/Twitter.
PHP
117
star
37

10up-toolkit

Official 10up asset building toolkit.
JavaScript
108
star
38

wp-codeception

Integrates Codeception framework into WordPress and allows run tests using WP CLI command.
PHP
103
star
39

twentysixteenreact

Twenty Sixteen theme as an isomorphic React.js application using NodeifyWP.
CSS
93
star
40

slotfill-and-filter-demos

This repo can be used as reference or can be installed as a plugin in any WordPress install to make code changes as needed. Each SlotFill or filter is explained with examples. This is meant to be a working document and will change as Gutenberg does.
JavaScript
93
star
41

gutenberg-best-practices

Welcome to the 10up Gutenberg Best Practices!
JavaScript
88
star
42

eight-day-week

Optimize print publication workflows by using WordPress as your print CMS.
PHP
86
star
43

wp-content-connect

WordPress library that enables direct relationships for posts to posts and posts to users.
JavaScript
83
star
44

nginx_configs

ARCHIVED: Nginx Configuration Template for WordPress Sites
C++
77
star
45

project-scaffold

DEPRECATED. Use https://github.com/10up/wp-scaffold
JavaScript
76
star
46

insert-special-characters

A Special Character inserter for the WordPress block editor (Gutenberg).
JavaScript
71
star
47

maps-block-apple

An Apple Maps block for the WordPress block editor (Gutenberg).
JavaScript
70
star
48

secured-advanced-custom-fields

ARCHIVED: Secured version of the Advanced Custom Fields plugin
PHP
63
star
49

windows-azure-storage

Use the Microsoft Azure Storage service to host your website's media files.
PHP
61
star
50

wp-newrelic

New Relic APM reports for WordPress.
PHP
59
star
51

component-library

A library of barebones front-end components built with WordPress and accessibility in mind.
JavaScript
58
star
52

ads-txt

Ads.txt Manager for WordPress: Manage your ads.txt and app-ads.txt files in the WordPress dashboard
PHP
56
star
53

Post-Customizer

A WordPress plugin to enhance the post editor preview
JavaScript
51
star
54

phpcs-composer

Official 10up PHPCS rules.
PHP
48
star
55

Open-Source-Best-Practices

Start reading: https://10up.github.io/Open-Source-Best-Practices/
SCSS
46
star
56

WordPress-Server-Configs

Configurations for the common Linux software stack 10up uses for WordPress
Nginx
44
star
57

10up-code-review

Custom PHP_CodeSniffer rules to help flag common issues during code review
PHP
43
star
58

publisher-media-kit

Pre-configured Media Kit Page using Gutenberg Block Patterns.
PHP
42
star
59

block-catalog

Easily keep track of which Gutenberg Blocks are used across your WordPress site.
PHP
40
star
60

curator

Select specific posts from across multiple post types to combine together and control the ordering.
PHP
38
star
61

insecure-content-warning

Prevent editors from adding insecure content in the editor.
JavaScript
37
star
62

retro-winamp-block

A Winamp-styled audio block for all your retro music player needs.
JavaScript
35
star
63

wordpress-ci-container

WordPress continuous integration Docker container with composer, NPM, and other common build tools for PHP projects
Shell
35
star
64

vagrant-ghost

Vagrant Hosts Updater Plugin
Ruby
31
star
65

elasticpress-react

Use ElasticPress with React.
JavaScript
30
star
66

10up-sitemaps

Simple sitemaps plugin that performs at scale.
PHP
29
star
67

debug-bar-elasticpress

Extends the Debug Bar plugin for usage with ElasticPress
PHP
27
star
68

simple-new-post-emails

Allow site members to check a box and get new posts via email. Includes a widget.
PHP
27
star
69

elasticpress-autosuggest

DEPRECATED - Functionality merged into core ElasticPress plugin
JavaScript
26
star
70

nodeifywp-environment

Dockerized environment for NodeifyWP applications.
26
star
71

action-wordpress-plugin-build-zip

Build a zip archive of your WordPress.org plugin using GitHub Actions
Shell
25
star
72

Docker-Images

Docker image repository
Shell
24
star
73

Ad-Refresh-Control

Enable Active View refresh for Google Ad Manager ads without needing to modify any code.
PHP
24
star
74

cypress-wp-utils

Utilities library for WordPress E2E testing in the Cypress environment
TypeScript
23
star
75

secure-media

ARCHIVED: Store private media securely in WordPress.
PHP
23
star
76

elasticpress-woocommerce-deprecated

DEPRECATED - Bundled in ElasticPress 2.1
PHP
22
star
77

brightcove-video-connect

A plugin to integrate your Brightcove video library or libraries with WordPress
PHP
22
star
78

Brute-Force-Login-Prevention

Assists in preventing common brute force login attempts by modifying the default login URL for WordPress.
PHP
20
star
79

snapshots

(BETA) A project sharing tool for WordPress. Previously known as WP Snapshots.
PHP
20
star
80

wp-safe-edit

ARCHIVED: Safely edit published posts behind the scenes without affecting the live site.
PHP
18
star
81

elasticpress-proxy

A custom PHP Proxy to handle Instant Results requests.
PHP
18
star
82

ElasticPressLabs

A developer-focused interface to enabling experimental ElasticPress plugin features.
PHP
18
star
83

wp-local-docker-docs

Documentation for WP Local Docker
SCSS
18
star
84

cypress-wp-setup

NPM package to set up boilerplate for Cypress testing.
JavaScript
17
star
85

gutenberg-lessons

10up Gutenberg Training WordPress Starter Files
PHP
17
star
86

image-generator

Generates missed thumbnails for images on the fly.
PHP
17
star
87

SwiftStream

Image lazy-loader for WordPress.
PHP
16
star
88

grunt-for-wordpress

Install Grunt, Git, and 10up WordPress Grunt scripts.
Shell
16
star
89

eslint-config

A shareable ESLint config.
JavaScript
16
star
90

elasticpress-stream

ARCHIVED: Use ElasticPress to power Stream with Elasticsearch.
PHP
16
star
91

wp-scrubber

BETA: This plugin provides a command-line interface for scrubbing sensitive user and comment data from a WordPress installation.
PHP
15
star
92

action-repo-automator

GitHub Action to automate common repo operations like validate PR description, changelog, and credits.
JavaScript
15
star
93

Gitlab-Registry-Cleaner

Bash script using the GitLab API to delete images from a GitLab container registry. Supports regex and deleting images older than a specific age.
Shell
15
star
94

frontity-packages

10up's collection of frontity packages.
JavaScript
15
star
95

CSS_Customizer

Drop in class for implementing a custom CSS editor either as a standalone settings page or a meta box.
PHP
14
star
96

ElasticPress-Admin-Deprecated

THIS FEATURE HAS BEEN MOVED TO THE CORE ELASTICPRESS PLUGIN.
PHP
13
star
97

composer-scan

ARCHIVED: Scans your composer.lock file to find vulnerable WordPress plugins and themes using the https://wpvulndb.com API
Python
12
star
98

simple-google-news-sitemap

A simple Google News sitemap is generated on-the-fly for articles that were published in the last two days. Output is saved in cache or as a transient for fast reading and displaying on the front end.
PHP
12
star
99

Publish-to-Twitter

Publish to Twitter based on WordPress taxonomies.
JavaScript
11
star
100

animation-best-practices

Welcome to the 10up Animation Best Practices!
HTML
11
star