PHP, work queues

PHP, work queues

Biggest pain point in PHP is lack of asynchronous calls. The only reasonable way is to use some kind of “backend” processing and task list to execute. Some people use cron to run given script each n-minutes and execute tasks. But there is a little bit more “professional” approach.  Beanstalk is a simple, fast work queue. Idea is pretty simple – you can just throw tasks into queue and then have background worker which is pulling data from pipe and executes task.

Couple ideas: sending emails, processing images, making heavy queries into database. But the best part of it – you don’t have to use PHP for back processing. It could be anything, because protocol us universal and worker and client doesn’t have to be in the same language. So you can schedule task in PHP, and run it in Go. I’m not going too much into details. Just run some examples.

You can install it on probably any Linux instance just pulling from repositories: yum install beanstalkd. Don’t forget to check port 11300 if you are connecting remotely. You just call telnet hostname 113000 – you can run simple command like stats to see if you got anything in queue.

For PHP Client i’m using pheanstalk which can be installed from composer.

Code sample for creating task in PHP

<?php
   require_once('./vendor/pda/pheanstalk/pheanstalk_init.php'); // use it if you don't have autolader
   $pheanstalk = new Pheanstalk_Pheanstalk('127.0.0.1');
   $pheanstalk->useTube('core')->put('task name');

And worker:

<?php   
  $pheanstalk = new Pheanstalk_Pheanstalk("127.0.0.1");

  while ($job = $pheanstalk->watch('core')->ignore('default')->reserve()) {
    $data = json_decode($job->getData(), true);
    $pheanstalk->delete($job);
  }

Most important thinks to now:

  1. Worker needs to be running all time, so that’s why is looped – so run it in screen or as a bg process on your server: php worker.php &
  2. Don’t forget to remove job when it’s finished.