knackforge
June 15, 2017
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
Create a cron queue with the callback function that will be called for each item in the queue.
Two important attributes are:
worker callback: It will be called with one argument, the item.
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,
);
}
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
}
}
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
Just like how your fellow techies do.
We'd love to talk about how we can work together
Take control of your AWS cloud costs that enables you to grow!