function hook_cron_queue_info

You are here

7 system.api.php hook_cron_queue_info()

Declare queues holding items that need to be run periodically.

While there can be only one hook_cron() process running at the same time, there can be any number of processes defined here running. Because of this, long running tasks are much better suited for this API. Items queued in hook_cron() might be processed in the same cron run if there are not many items in the queue, otherwise it might take several requests, which can be run in parallel.

Return value

An associative array where the key is the queue name and the value is again an associative array. Possible keys are:

  • '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.
  • 'skip on cron': (optional) Set to TRUE to avoid being processed during cron runs (for example, if you want to control all queue execution manually).

See also

hook_cron()

hook_cron_queue_info_alter()

Related topics

1 function implements hook_cron_queue_info()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

aggregator_cron_queue_info in modules/aggregator/aggregator.module
Implements hook_cron_queue_info().
1 invocation of hook_cron_queue_info()
drupal_cron_run in includes/common.inc
Executes a cron run when called.

File

modules/system/system.api.php, line 620
Hooks provided by Drupal core and the System module.

Code

function hook_cron_queue_info() {
  $queues['aggregator_feeds'] = array(
    'worker callback' => 'aggregator_refresh',
    'time' => 60,
  );
  return $queues;
}

Comments

Invoked by includes/common.inc

The argument of the worker callback is the data passed to DrupalQueue::createItem(), not to be confused with the queue item returned by the claimItem() method.

I add a cron task,and let all node title add a suffix "abc",just in order to learn how to use these hook.The code can run properly.

<?php
/*
* Implements hook_cron().
*/
function hf_remind_cron(){
 
$result = db_query('SELECT title,nid FROM {node}');
 
$queue = DrupalQueue::get('send_emails');
  foreach (
$result as $hf_info) {
   
$queue->createItem($hf_info);
  }
}

/*
* Implements hook_cron_queue_info().
*/
function hf_remind_cron_queue_info(){
 
$queues['send_emails'] = array(
   
'worker callback' => 'hf_sender',
   
'time' => 120,
  );
  return
$queues;
}

function

hf_sender($data){
 
$result = db_update('node')
        ->
fields(array('title' => $data->title . 'abc'))
        ->
condition('nid', $data->nid)
        ->
execute();
}
?>

A number of example uses of this hook, including the above, explicitly create the queue in the hook_cron(). It's important to note that this is NOT required to process queues during cron.

If you are pre-populating your queue with items somewhere else in Drupal (for example, on node creation, user saves, or any other arbitrary action), then all that's required to process a queue on cron is to invoke hook_cron_queue_info() using the name of the queue.

Also note that if you're familiar scheduling tasks in hook_cron() using this method, you can use the same logic to return only relevant portions of the array returned in hook_cron_queue_info() to effectively schedule queue processing.

Seeing several examples like the above had me wondering why implementing hook_cron should be required to use cron queues.

I was wondering about this too - and after having ready your post, I found this other post by you where you give an example - linking to it here for anyone else looking for this info:
https://drupal.org/node/1301030

Be aware that after the 'worker callback' function is called, the item will be deleted from the queue whether you want it to be or not.

Does anyone know how I can check to see if I already added the item to the queue and it still exists (ie. node id)?

Use cool Queue UI module for such task.

Use cool Queue UI module for such task.

It seems the only way to check if you've already added an item to a queue is to use your own table to store IDs and queue times for your items. The example hook_cron() code in modules/system/system.api.php shows one way to do this.

What is the best way to set a conditional task? I want enable user 1 to turn off the queue and the cron using a system form. Is it possible to just do this

<?php
function module_name_cron_queue_info() {
  if (
variable_get(' module_name_enable_task', TRUE)) {
   
$queues['some_queue'] = array(
     
'worker callback' => 'module_name_worker_cakkback',
     
'time' => 180,
     
'reliable' => TRUE,
    );
    return
$queues;
  }
  else {
    return array();
  }
 
}
?>

At first glance that should work. Make sure you don't include the ?>! Also, this may be easier to read/write and maintain:

<?php
function module_name_cron_queue_info() {
  $queues = array();
  if (variable_get(' module_name_enable_task', TRUE)) {
    $queues['some_queue'] = array(
      'worker callback' => 'module_name_worker_callback',
      'time' => 180,
      'reliable' => TRUE,
    );
  }
  return $queues;
}

Hi, guys, I'm trying to send a couple of emails using hook_cron, but I'm having some troubles, for each row the function foobar send a mail; when the query returns one row, It's no problem, the mail has two parts one part it's a automatically generated google image, and the other part it's a table with some data related with the image. The problem begins when query returns with two or more rows, in the second mail, the image it's the same of the first mail but the table it's filled with correct data. This is the code.

function my_module_cron(){
$query="SELECT * FROM my_table WHERE date(next_date)='".date('Y-m-d')."'";
$result=db_query($query);
$queue=DrupalQueue::get("foo_item");
foreach($result as $data){
$queue->createItem($data);
}
}

function my_module_cron_queue_info(){
$queues=array();
$queues['foo_item']=array(
"worker callback"=>"foobar",
"time"=>160,
);
return $queues;
}

function foobar($data){
foo_send_mail($data);
}

Anyone know what's going wrong?