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') ->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; } }