Manage background tasks with PHP-Resque and Supervisor

Posted on Thu 03 July 2014 in Tech

PHP-Resque, the PHP equivalent to Ruby's really nice background process runner is a great way to execute long running PHP processes. Supervisor is a python-based process control system, essentially it's a convenient way to manage custom system processes you need to handle. Combining the two to manage long running PHP jobs makes a lot of sense but it takes a little bit of work to get them talking.

Once you've gotten PHP-Resque installed on your project, you've added items to your queue and written the workers to handle the background tasks. You'll need to plug PHP-Resque into supervisor. Here's a sample config file I used to manage php resque queues within Yii projects but it should work with anything that's using php-resque.

[program:my_resque]
 directory=/var/www/website
 command=php vendor/chrisboulton/php-resque/bin/resque
 numprocs=2
 environment=QUEUE='myresquequeue',BLOCKING=1,COUNT=1,APP_INCLUDE='models/UsefulClasses.php'
 autostart=true
 autorestart=true
 stopsignal=QUIT
 process_name = %(program_name)s-%(process_num)s

A few things to note.

  1. Firstly the APP_INCLUDE should include the set of classes you intent to use to handle the worker jobs. Meaning something that has a perform action that does something, as described here.
  2. You'll notice the COUNT=1 argument being set in the resque environment. This is essential, if you fork more than one job using php-resque it'll background both and supervisor will not be able to manage that process(and will continually try to spawn more processes till it gives up). If you wish to have multiple workers, use the numprocs argument.

And that's it really.

Happy forking!