[Drupal] How to use Drupal Queue Classes ?
The Queue API in Drupal is designed to manage tasks.The queue system allows placing items in a queue and processing them later.The Queue API is a simple,reliable way to keep track huge lists of tasks without requiring you to know anything about the whole group.Prepare your data, put it in one item at a time, and take back one item at a time to process. When you are done with a single item, just report back that you have finished and the item is removed from the queue.
Queue API is to overcome memory/time limit problems, and to simplify your development by dealing with single items instead of huge lists.
Drupal Queue Classes
There are three classes within the Queues API that provide the queue functionality.
These are DrupalQueue, SystemQueue and MemoryQueue.
DrupalQueue
The DrupalQueue class consists of a single static method called get().
To use it you pass the name of the queue you want to retrieve and the method will return an object that you can use to interact with your queue.
The following code is needed to get a queue object.
$my_queue = DrupalQueue::get('my_queue');
The queue object returned will be either a custom queue object or one of the default system objects, depending on what variables have been set. Any queue you create will have a unique set of items that will hang from the queue name that you issued. The get() method has a second parameter, which is a boolean value that forces the queue object to be reliable. The default value is FALSE, means that the get() method will not force the returned queue to be reliable. The default returned object is SystemQueue.
SystemQueue
The SystemQueue class is the default queue class in Drupal.
It is an example of a reliable queue class and implements the DrupalReliableQueueInterface interface. SystemQueue class uses the database table queue to store and retrieve the queue. Any items that are retrieved from the queue are 'leased' to ensure that no two processes get the same queue item.
The queue table is created during the Drupal install process and has the following structure.
Column Type Comment
item_id int(10) unsigned Auto Increment Primary Key: Unique item ID.
name varchar(255) The queue name.
data longblob NULL The arbitrary data for the item.
expire int(11) Timestamp when the claim lease expires on the item.
created int(11) Timestamp when the item was created.
MemoryQueue
The MemoryQueue class is an example of a non-reliable queue class and therefore does not implement the DrupalReliableQueueInterface interface.
All of the queue items are stored in memory as a $queue parameter in the class. This class also implements item leasing to stop the same items being retrieved from the queue over and over again.