blog-banner

Drupal Cron-Queue

  • Drupal
  • Drupal 6
  • Drupal Planet

Drupal Cron

 

Drupal cron queue is very useful while running a big cron job. It will split a big cron job into small chunks and execute without getting timed out or being abruptly ended.

Three important things to remember in queue implementations are

  1. Create cron queue in hook_cron_queue_info
  2. Fill the queue with data
  3. Define a worker callback function

 

STEP 1 - Create cron queue in hook_cron_queue_info

 

Create a cron queue with the callback function that will be called for each item in the queue.

Two important attributes are: 

  1. worker callback: It will be called with one argument, the item.

  2. time : (optional) How much time Drupal should spend on calling this worker in seconds. Defaults to 15.

function mail_cron_queue_info() {

$queue['mail_sending_queue'] = array(

   'worker callback' => '_mail_sending_queue_worker',

   'time' => 45,

);

}

 

STEP 2 - Fill the queue with data in hook_cron

 

function mail_cron(){

  $queue = drupal_queue_get('mail_sending_queue'); // grab queue

  $queue->createQueue(); // initialize

  while ($item = db_fetch_array($result)) {

    $queue->createItem($item); // each item passed as param to worker

  }
}

 

STEP 3 - Define a worker callback function

 

It will take only one argument, the item that is what we passed through createItem() in hook_cron().

Processing of data will take place here only.

function _mail_sending_queue_worker($item){

…………….

………….…

}

Drush commands related to a queue are 

drush queue-list

drush queue-run

Get awesome tech content in your inbox