Drupal 7 queues using beanstalkd

Queues are a great way to process large amounts of data without holding a users browser hostage, and even better if you can process that data in real-time using a queue worker like beanstalkd instead of cron, which Drupal uses by default.

Installing Dependencies

Before we can start using beanstalkd for our queues, we first need to install a few dependencies

beanstalkd is a system package, so installation will depend on which system you’re on. Using ubuntu, the command is

Note that the ‘d’ on the end is required. There is also a ‘beanstalk’ package in Ubuntu which is not what we need.

Once installed, make sure it’s running

Installing the drupal module is straight forward with Drush

We’re using the 7.2 dev branch here because I was not able to get the 7.1 branch working due to it needing old versions of libraries.

At the time of writing, the 7.2 branch also required the composer-manager module for installing pheanstalk. I had a few problems getting this all working, but once I got everything installed my beanstalkd module directory looking like this

Once all of that’s installed, it’s time to move on to the module.

The Module

There are three main functions that your module needs

  1. A function to queue an item
  2. A function to claim an item
  3. A hook to tell Drupal how to handle queued items

Your item queueing function needs to do two things, first is to make sure that the queue exists and if it does not, create it

And then we can add an item to the queue. For example, you could add a node ID and title

You could also store the entire node in the queue if you wanted. When the item comes off of the queue, Drupal would convert it back to a node object automatically.

And that’s all we need to do. Here’s the full code

And to add an item to the queue, all you need to do is call the function from somewhere else

Now you can create a function to claim the item

If there are errors thrown within the claiming function, the item will be placed back on the queue for reprocessing, otherwise it will be pulled off the queue and then the next item will be processed.

Now we use hook_cron_queue_info to tell Drupal how to process an item

Then all that’s left is getting up and running with the queue itself.

The Queue

Now that we have the framework laid out we can tell Drupal to use beanstalkd for our queue, and then finally run the queue.

In  sites/default/settings.php add the following line

This will tell Drupal that the “custom_queue” queue should be processed by the BeanstalkdQueue class from the beanstalkd module.

To run the queue itself, we’ll use the runqueue.sh file included with the beanstalkd module

The -q argument specifies which queue we’re interested in processing, the -r argument tells the script where the root of the Drupal install is located, and the -s argument is the site (i.e.  /sites/all/sites/default, etc).

Once you confirm the queue is working, you can set that up to run in a screen or supervisor so that it’s always running.

Wrap Up

Getting set up with beanstalkd queues isn’t very intuitive, but then the default DB queue isn’t either. But once you upgrade to real-time queue I don’t think you’ll ever want to go back.

Leave a Reply

Your email address will not be published. Required fields are marked *