Created by Jurian Sluiman and Michaël Gallego
First, install SlmQueue (instructions here). Then,
add the following line into your composer.json
file:
"require": {
"slm/queue-beanstalkd": "0.4.*"
}
Then, enable the module by adding SlmQueueBeanstalkd
in your application.config.php file. You may also want to
configure the module: just copy the slm_queue_beanstalkd.local.php.dist
(you can find this file in the config
folder of SlmQueueBeanstalkd) into your config/autoload folder, and override what you want.
Before reading SlmQueueBeanstalkd documentation, please read SlmQueue documentation.
(Don't forget to first install Beanstalkd, and to run the daemon program on the server)
Copy the slm_queue_beanstalkd.local.php.dist
file to your config/autoload
folder, and follow the instructions.
SlmQueueBeanstalkd provides an interface for a queue that implements SlmQueue\Queue\QueueInterface
and provides in
addition the following methods:
A concrete class that implements this interface is included: SlmQueueBeanstalkd\Queue\BeanstalkdQueue
and a factory is available to
create the queue. Therefore, if you want to have a queue called "email", just add the following line in your
module.config.php
file:
return array(
'slm_queue' => array(
'queue_manager' => array(
'factories' => array(
'email' => 'SlmQueueBeanstalkd\Factory\BeanstalkdQueueFactory'
)
)
)
);
This queue can therefore be pulled from the QueuePluginManager class.
Valid options are:
Example:
$queue->push($job, array(
'priority' => 20,
'delay' => 23,
'ttr' => 50
));
Valid option is:
Valid options are:
Valid option is:
Beanstalkd offers a nice bury/kick/release mechanism, so that jobs that fail can have a second chance to be executed.
SlmQueueBeanstalkd provides a nice way to easily bury/release a job. In fact, you just need to throw either
the SlmQueueBeanstalkd\Job\Exception\BuryableException
or SlmQueueBeanstalkd\Job\Exception\ReleasableException
in
the execute
method of your job:
use SlmQueue\Job\AbstractJob;
use SlmQueueBeanstalkd\Job\Exception;
class SimpleJob extends AbstractJob
{
public function execute()
{
// Bury the job, with a priority of 10
throw new Exception\BuryableException(array('priority' => 10));
// Release the job, with a priority of 10 and delay of 5 seconds
throw new Exception\ReleasableException(array('priority' => 10, 'delay' => 5));
}
}
SlmQueueBeanstalkd provides a command-line tool that can be used to pop and execute jobs. You can type the following command within the public folder of your Zend Framework 2 application:
php index.php queue beanstalkd <queue> [--timeout=]
The queue is a mandatory parameter, while the timeout is an optional flag that specifies the duration in seconds for which the call will wait for a job to arrive in the queue before returning (because the script can wait forever if no job come).