Download the PHP package morningtrain/wp-queue without Composer
On this page you can find all versions of the php package morningtrain/wp-queue. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package wp-queue
Morningtrain\WP\Queue
Queue system for WordPress.
Table of Contents
- Introduction
- Getting Started
- Installation
- Usage
- Register Worker
- Create a Job
- Running the Queue Worker
- Credits
- License
Introduction
This package is made to dispatch jobs to a queue system.
It is implementet with WP CLI so it can be runned from the command line.
Getting started
To get started install the package as described below in Installation.
To use the package have a look at Usage
Installation
Install with composer.
Usage
Register Worker
To get started with the module simply register a job queue \Morningtrain\WP\Queue\Queue::registerWorker()
.
You can change name of the job queue from default job_queue
.
You can change the version of the job queue database table from default 1.0.0
.
Create a Job
Jobs can be created by extending Morningtrain\WP\Queue\Abstracts\AbstractJob
and create a handle
method.
You can define which job_queue the job should use by setting the $worker
parameter to the job queue slug.
Now you can put a job in the queue by calling the static method dispatch
on your Job class.
Alternatively Job Creation
Jobs can alternativley be created directly from the worker, by passing a callback to the createJob
method.
Arguments
If you use arguments in your jobs, you have to be aware, that we are using call_user_func_array
.
So if you pass an array, you need to have same number of arguments in your method as you have in your array. And if keyed, the arguments in the method must be called the same as the keys.
If you do not know the number of arguments in your array or you just need to work on an data array, you can use ...$args
in your method.
Or you can wrap your array in an extra array containing only the one argument.
See more about call_user_func_arrey
on the PHP documentation
Running the Queue Worker
Use WP CLI to run the job queue. See WP CLI documentation.
Start Worker
Start job queue with the wp queue start
command and the worker slug.
Note
The worker will run until it is manually stopped or the terminal is closed. You should use a process monitor such as Supervisor to make sure the process does not stop unintended.
You can run multiple workers simultaneously.
Stop Worker
To stop a worker that you can not stop by closing you terminal, you can use the stop
command with the worker slug.
To stop all workers use all
instead of worker slug.
List available workers
To list available workers and their status, use the list
command
Process a Specific Job
Call wp queue run
with the worker slug and the job ID to process the specific job.
Note
You can use --untouched to not set run_date and result
You can use --force to force the job to run even though it has run before
Credits
- Martin Schadegg Brønniche
- All Contributors
License
The MIT License (MIT). Please see License File for more information.