I have been using Views Bulk Operations (VBO) module from long time. I always found an option appear in list after enabling the module Enqueue the operation instead of executing it directly , this make me appealing to learn more about Drupal Queue API and Batch processing.
Queue is actually new to Drupal 7. The Queue API is designed to manage tasks exactly like the way we make to-do lists for day-to-today life in Drupal term we call data processing, chore Queue always have room for one more the exception is if you ran out of memory or you ran out of storage space.
So why we would be using Drupal Queue Api, its simple to
- overcome memory/time limit problems
- To simplify our development by dealing with single items instead of huge catalog.
- Processding of large amounts of data/item
- Delay Processing of complex calculation so system load in more reliable.
Drupals Queue Interface
After creating a Queue with createQueue() you need to create an Item with createItem($data) which will place your item in a Queue and wrapped it with some meta data such as Id and on retrieving data from Queue we will receive this meta-data as well. numberOfItems() method lets you see how many items are present in the queue. claimItem($seconds) helps to read item from the Queue, there could be number of users claiming stuff from the Queue therefore user can mentioned the seconds he want to claim the item, which essentially means requesting the item from the queue and holding a lock on that item for a specified period of time. releaseItem() release an item for others to get hold on it, which means giving up a claim but leaving the item in the queue. To permanently delete an item from the Queue use deleteQueue() which means finally removing it from the queue.
Drupal Cron system uses Queue very heavily.
QUEUE CLASSES IN CORE:
SystemQueue : Is the default Drupal Queue whaich is backed by MySql, what actually happen here is you handover something to queue drupal wrap it with some metadata and save it to queue table
MemoryQueue : Is totaly in memory queue system. Its very very fast, you can use it for one long running request, Please note dont make an assumption that the things will come out in certain order from the Queue. for more go through the documention on drupal.org
BatchQueue : batchQueue actually extends SystemQueue it help SystemQueue to return thing in some particular sorted order.
BatchMemoryQueue: Batch memory is actually the samething except it extends MemoryQueue.
Lets Do it Practically.
// Create queue object
$queue = DrupalQueue::get('foo_queue');
// Create item
$item = array(
array('title' => 'item 1', 'type' => 'article'),
array('title' => 'item 2', 'type' => 'article'),
);
// Add item to queue
$queue->createItem($item);
// Report on number of items present
echo $queue->numberOfItems(); // Prints "2"
// Get the queue
$queue = DrupalQueue::get('foo_queue');
// Claim item from the queue
while($current_item = $queue->claimItem()) {
// Print data from item
echo $current_item->data['title'];
}
// Releasing And Deleting Items
// releasing item from Queue
$queue->releaseItem($current_item);
// Deleting Item
$queue->deleteItem($current_item);
For detailed explanation please read the below links.
system.queue.inc
Queue example
There is a very nice tutorial John VanDyk have explained Batch vs. Queue
Drupal 7 Queues
Using Drupal's Queue API
0 Comment(s)