4

I've been searching around, but I can not find a good explanation (or understanding) what the 'time' value in hook_cron_queue_info actually does, or how to set it to work properly.

From: https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_cron_queue_info/7

it is said that: 'time': (optional) How much time Drupal should spend on calling this worker in seconds. Defaults to 15.

I don't quite understand that. Does calling the worker mean time allowed to execute the cron queue job? And does the next cron queue job starts after this time? Or when the previous job is completed?

3 Answers 3

6

It's literally what the description says it is - during a cron run, Drupal will spend a maximum of the seconds you provide in that value processing that particular queue, regardless of the number of jobs in it.

If you're looking for the relevant code it's in drupal_cron_run()

foreach ($queues as $queue_name => $info) {
  if (!empty($info['skip on cron'])) {
    // Do not run if queue wants to skip.
    continue;
  }
  $function = $info['worker callback'];
  $end = time() + (isset($info['time']) ? $info['time'] : 15);
  $queue = DrupalQueue::get($queue_name);
  while (time() < $end && ($item = $queue->claimItem())) {
    $function($item->data);
    $queue->deleteItem($item);
  }
}
2
  • 1
    Thanks Clive.I can see where my mix up was. I thought it was time allowed doing ONE job (thinking in case of server time outs). Now I know it is time allowed to do a batch of jobs. Apr 14, 2014 at 8:28
  • Clive, can you have a time greater than PHP's max_execution_timeout? Is leaving it at 15 a best practice if you have the timeout set to 30 sec in php.ini? What if you have 3 queues provided by modules that take 15 sec each?
    – Bart
    Aug 8, 2014 at 9:48
2

Queue items will be processed until the "time" value has expired. The processing may take longer than this, but no new items will be processed after this time has passed.

So, for example, if "time" is 15 and each queue item takes 14 seconds to process, the processing will deal with two items, taking 28 seconds in total. The second item is processed because the first item has finished before the time has expired.

0

The answer is obvious if you look at the following two items in combination:

  • 'worker callback': The name of the function to call. It will be called with one argument, the item created via DrupalQueue::createItem().

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

So the optional time value is the maximum time that will spent, during a cron run, to execute the callback worker callback function that you supply in your implementation of this hook.

3
  • OK - somebody seems to think this answer is wrong. Could someone (not necessarely the downvoter) please explain why it is wrong, given what the documentation says? Apr 14, 2014 at 8:05
  • This definitely isn't wrong, can't think why it's been downvoted
    – Clive
    Apr 14, 2014 at 8:39
  • I'm not the downvoter ;). Your answer is not wrong but did not answer my question though. And I'm the one that was confused. Apr 14, 2014 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.