Categories

(83)
(69)
(8)
(34)
(74)
(149)

Drupal 7's Queue API

10.01.2013
Drupal 7's Queue API
Author:

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 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 on-line 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 facilitate 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 substantial 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')
    ->execute();
  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();
    $queue->createQueue();
    // Sending array with nodes.
    $queue->createItem($nodes);
    // Set the interval 5 minutes.
    $queue->claimItem($start + (60 * 5));
    internetdevels_queue_select_content($count);
  }
  else {
    return;
  }
}

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'))
        ->execute();
      while ($node = $query->fetchAssoc()) {
        // Making something important here with node...
      }
    }
  }
} 

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

$queue = DrupalQueue::get('internetdevels_main_queue');
$queue->deleteQueue();

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 inconvenience 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 of 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.

12 votes, Rating: 5

Read also

1

This article is about hot quickly to configure the module Search API and Search API Solr search, and how to display the data from Solr server with the help of the module Views.

2

Our company often uses the Panels module for creating sites. Although this option adds a considerable amount of HTML structures, it provides us with a flexible, user-friendly, and easy-to-operate...

3

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 through your own requests. Inherent in such...

4

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

5

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

Subscribe to our blog updates