PHP Queue Platform provides an easy way to build queue system in PHP.
The easiest path to use queue platform is to use QueuePlatformExample repository.
QueuePlatformExample provides a complete working example with sample workers.
To run and test it, it comes with configured vagrant setup. To use it just boot up vagrant with vagrant up
.
Vagrant setup will set up a complete environment. It will start 5 working processes, setup queue manager URL and server testing GUI.
URL to testing GUI: http://192.168.29.6/index.html
How to modify it, see docs in QueuePlatformExample repository.
Require QueuePlatform package with composer: composer require bostjanob/queue-platform
.
Workers must extend BostjanOb\QueuePlatform\Worker
interface.
The only method to implement is run($params = null)
.
Create a new BostjanOb\QueuePlatform\QueueManager
object and register your workers with it.
For constructor, you must provide storage object (object that implements \BostjanOb\QueuePlatform\Storage\Storage
)
<?php
// queuemanager.php
$storage = new \BostjanOb\QueuePlatform\Storage\SqlLiteStorage('db.sqlite3');
$qm = new \BostjanOb\QueuePlatform\QueueManager($storage);
$qm->registerWorker('task-name', new Worker());
To push task, get task status and to make communication with working processes QueueManager uses JSON-RPC. So it must be accessible by a web server (over URL).
To listen for json-rpc request run listen()
on QueueManager.
<?php
// queue.php - server over web server
require 'queuemanager.php';
echo $qm->listen();
To start long running working process call work()
on QueueManager. Run it from CLI and pass URL to QueueManager (in step 4) as a parameter.
<?php
// process.php
require 'queuemanager.php';
$qm->work();
Run from CLI:
php process.php http://example.com/queue.php
QueueManager JSON-RPC provides two methods to interact with.
Queue task to be then later pulled by the processor. It accepts one or two parameters.
the first parameter is the name of a worker registered with a queue manager
the second parameter is a parameter for the worker. It accepts a single value. If you want to pass multiple values use an array.
returned result is a task object as a JSON-RPC result
getTask method return task object. The method requires id of a task as a first parameter.
To run long running process run PHP script from CLI (step 5.).
Command to start process:
php file.php [OPTIONS] URL_TO_QUEUEMANAGER
Available options:
- workers - List workers to work with. Default to all registered in QueueManage. (split by comma)
- sleep - How many seconds to wait if there is no task. The process will check if there is some task and if there is none it will wait before checking again.
Example with options:
php file.php --workers=foo,bar --sleep=3 http://example.com/queue.php