Resque is a Redis-backed library for creating background jobs, placing those jobs on one or more queues, and processing them later.
Features:
setUp
and tearDown
methods, called pre and post jobsQueueing
Resque::enqueue('default', 'My_Job', $args);
Defining Job
class My_Job { public function perform() { // Work work work echo $this->args['name']; } }
Dequeueing Jobs
This method can be used to conveniently remove a job from a queue.
// Removes job class 'My_Job' of queue 'default' Resque::dequeue('default', ['My_Job']);
Tracking Job Statuses
php-resque has the ability to perform basic status tracking of a queued job. The status information will allow you to check if a job is in the queue, is currently being run, has finished, or has failed.
$token = Resque::enqueue('default', 'My_Job', $args, true); $status = new Resque_Job_Status($token);
$ QUEUE=file_serve php bin/resque
Priorities $ QUEUE=file_serve,warm_cache bin/resque
Running all queues $ QUEUE='*' bin/resque
Running multiple workers $ COUNT=5 bin/resque
Similarly to the Ruby versions, supported platforms will immediately fork after picking up a job. The forked child will exit as soon as the job finishes.
The difference with php-resque is that if a forked child does not exit nicely (PHP error or such), php-resque will automatically fail the job.
Signals also work on supported platforms exactly as in the Ruby version of Resque:
QUIT
- Wait for job to finish processing then exitTERM
/ INT
- Immediately kill job then exitUSR1
- Immediately kill job but don't exitUSR2
- Pause worker, no new jobs will be processedCONT
- Resume worker.Worker: PHP process that will run indefinitely, always monitoring for new jobs to execute. Pseudocode
while (true) { $jobs = pullData(); # Pull jobs from the queues foreach ($jobs as $class => $args) { # For each jobs found $job = new $class(); $job->perform($args); # Execute them } sleep(300); # Then Sleep for 5 minutes (300 seconds), and retry }
Workers have to be started in the CLI. You can’t ceate worker through a browser, because :
QUEUE=notification VVERBOSE=1 php resque.php
nohup QUEUE=notification php resque.php & &: Daemon-ify the worker nohup: enables the commnad to keep running even if the user logged out
nohup QUEUE=notification php resque.php >> /path/to/your/logfile.log 2>&1 &
tail -F /path/to/your/logfile.log
when a Resque worker reserves a job it immediately forks a child process. The child processes the job then exits. When the child has exited successfully, the worker reserves another job and repeats the process. Resque assumes chaos => a job uses too much memory, a job delays
Thanks to Resque’s parent / child architecture, jobs that use too much memory release that memory upon completion. No unwanted growth.
With Resque’s parent / child architecture you can tell the parent to forcefully kill the child then immediately start processing more jobs.
A job is a written order to tell the workers to execute a particular task.
Resque::enqueue('default', 'Mail', array('dest@mail.com', 'hi!', 'this is a test content')); // queue name, class name, arguments // ------- class Mail { public function perform() {} }
All your jobs classes must be discoverable by the workers. There’s more than one way to do that
Using APP_INCLUDE
QUEUE=default APP_INCLUDE=/path/file.php php resque
All workers activities are logged. You’ll find in the logs:
Resque::enqueue
Depending on your verbose mode, they come with more or less details.
TODOs:
Running worker on a queueu
default
queue : app/console bcc:resque:worker-start default
q1
and q2
queue : app/console bcc:resque:worker-start q1,q2
Supervisor
[program:myapp_phpresque_default] command = /usr/bin/php /home/sites/myapp/prod/current/vendor/bcc/resque-bundle/BCC/ResqueBundle/bin/resque user = myusername environment = APP_INCLUDE='/home/sites/myapp/prod/current/vendor/autoload.php',VERBOSE='1',QUEUE='default' stopsignal=QUIT