Drupal 7's Queue API

Drupal 7's Queue API

Queue API is a particular functional in Drupal that allows web developer to line up all labor-intensive operations and further check up their implementation on the site. Unlike Batch API, Queue API fully automates the procedures run on your site.

Queue API performs the so-called atomic operations, e.i. the ones that are either performed as consecutive actions within a single unit, or otherwise get aborted.

Let us consider a scenario with an on-line store that has an intensive customer traffic and, consequently, permanent on-line order processing. Incidentally, the processed order statistics gets forwarded to some external server. We can as well have the statistics handled for each and every order and then have it sent out to the external server; in which case the resulting overpressure may affect both servers.

Queue API is just the thing to help us ease up on that kind of a situation. With it we can "instruct" our server that the order processing be "rescheduled", say, for night hours, when visits to the site are few and the sever's capabilities aren't compromised. After we've rung this tool the server that hosts the online shop stays relieved of the excessive burden. Thus, we will have our site capacity 'automated' and 'optimized' (!!!) enough to ensure efficient report processing.

First, we have to have a line formed. That is actually some list of operations to fascilitate thereby. A certain function should be written to look after the whole thing, when a cron is getting started. The reason for using this cron is simple: a database request operation doesn’t require substancial resources. With the code I will run a simple data processing. The selection of materials that have rested more than a week will then be drawn out of the database and the Queue operation date will be 'fixed' to the current date.

In the example below you see how hook_cronapi looks like, after elysia_cron module has been installed. Now I am selecting the content of the site and carrying materials (in 20pcs lots) over into the Queue. Please, note that the Elysia cron module will get an operation \running every hour by default. This option may be easily removed off the module settings.

Here's the hook:

 * Implements hook_cronapi().
function internetdevels_queue_cronapi($op, $job = NULL) { 
  $items = array(); 
  $items['internetdevels_queue_main'] = array( 
    'description' => 'Send nid to Queue', 
    'rule' => '0 23 * * *', 
    'arguments' => array(20), 
    'callback' => 'internetdevels_queue_select_content', 
  return $items; 

here's the function for running the cron:

 * Cron function.
function internetdevels_queue_select_content($count) {
  static $start;
  if (empty($start)) {
    $start = 0;
  $nodes = array();
  // Choosing nodes older than 1 week.
  $query = db_select('node', 'n')
    ->condition('n.changed', REQUEST_TIME - ((3600 * 24) * 7), '<')
    ->fields('n', array('nid'))
    ->range($start, $count)
    ->orderBy('n.changed', 'ASC')
  while ($value = $query->fetchAssoc()) {
    $nodes[] = $value['nid'];
  // If we have such material then send the nodes to the Queue.
  if (count($nodes) !== 0) {
    $start += $count;
    $queue = DrupalQueue::get('internetdevels_main_queue');
    // Return number of queues.
    $count = $queue->numberOfItems();
    // Sending array with nodes.
    // Set the interval 5 minutes.
    $queue->claimItem($start + (60 * 5));
  else {

Having chosen the materials and recorded them into the Queue, we have to configure the way the queue will be processed, and ‘hook_cron_queue_info’ is to be responsible for this.

The example of how the hook is being realized:

 * Implements hook_cron_queue_info().
function internetdevels_queue_cron_queue_info() {
  $queues = array();
  $queues['internetdevels_main_queue'] = array(
    // Queue worker function.
    'worker callback' => 'internetdevels_queue_function',
    // The function will be work 10 seconds.
    'time' => 10,
  return $queues;

Besides, let us discuss a function that performs a Queue operation.

 * Queue function.
function internetdevels_queue_function($data) {
  if (!empty($data)) {
    foreach ($data as $key => $value) {
      // Choosing nodes.
      $query = db_select('node', 'n')
        ->condition('n.nid', $value)
        ->condition('n.changed', REQUEST_TIME - ((3600 * 24) * 7), '<')
        ->fields('n', array('nid'))
      while ($node = $query->fetchAssoc()) {
        // Making something important here with node...

While working with this functional one has to get to dnow the method for the queue deletion. To delete the list you should write the following 2 lines:

$queue = DrupalQueue::get('internetdevels_main_queue');

Finally, we have got a functional for data updating based upon the Queue API operations.
Note, that the operation has a 10 second time span. If the function's failed to perform within the given time-limit (10 seconds), Queue will get it started again.
Besides, there is a certain module available for getting the Queue visual interface exposed. The Queue UI Module promptly spares the enconvenience of constant database checking and that's another reason for you to enjoy the drive with the Queue.

Note that a systematic cron (aka a system_cron) is also a part to the operation. If the Queue operation has been a success (within the required time frame), it will automatically be delisted after the cron’s started.

this document contains a comparative description of Batch API and Queue API which you may find helpful.

7 votes, Rating: 5

Read also


This article is about hot quickly to configure the module Search API and...


Our company often uses the Panels module for creating sites. Although this option adds a considerable amount of HTML structures, it...


You sometimes find yourself trying to handle the tasks that require creating a custom table in the database so as to later interact with this table...


In my previous article we discussed capabilities of PHP code profiler...



Nowadays everyone knows about such Internet shops like eBayAmazon,...

Need a quote? Let's discuss the project

Are you looking for someone to help you with your Drupal Web Development needs? Let’s get in touch and discuss the requirements of your project. We would love to hear from you.

Join the people who have already subscribed!

Want to be aware of important and interesting things happening? We will inform you about new blog posts on Drupal development, design, QA testing and more, as well news about Drupal events.

No charge. Unsubscribe anytime