6

I am retrieving remote data by clicking an update button. I want to make it automatic. I read many articles about it but I didn't find a good example for using hook_cron_queue_info in my module. can anybody help me?

  • There are working examples on the docs page...do they not work for you? If not, could you add some more information: show the code you've tried, and what errors were produced – Clive Apr 9 '13 at 14:00
  • I used the examples which has been on that page but nothing happened! no drupal message has been set after running the function. ok I will put some parts of my code since it is very long – weber85 Apr 9 '13 at 14:12
  • Yeah some code will help a lot :) I'm a bit confused what you mean though, none of the examples on that page set a message of any kind... – Clive Apr 9 '13 at 14:14
  • $function = 'itunesdec_batch_update_apps'; $_SESSION['http_request_count'] = 0; // reset counter for debug information. $batch = $function(); batch_set($batch); this is what i use in my hook_form_submit. I want to do this b a cron automatically so that user won't be forced to do this job. – weber85 Apr 9 '13 at 19:24
  • I found this drupal.org/project/examples very helpful. – weber85 Apr 10 '13 at 7:20
13

You can look at the example code shown in the documentation from hook_cron_queue_info(), which still needs an explanation on how it is used.

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

The code show is actually the implementation of hook_cron_queue_info() made from the Aggregator module. (See aggregator_cron_queue_info().)

"aggregator_feeds" is the name of a Drupal queue that is filled with items that need to be processed; "aggregator_refresh" is the name of a function that will be called when Drupal executes cron tasks, once for each item contained in the "aggregator_feeds" Drupal queue. aggregator_refresh() gets the item taken from that Drupal queue as first parameter.

The question you would ask at this point is: When are the Drupal queue items added to the queue?

The Aggregator module does that in its hook_cron() implementation, but any function could add items to a Drupal queue, including other hooks (with the exception of hook_cron_queue_info() and similar hooks*), form submission handlers, page callbacks, or any function called from those functions.

* By "similar hooks" I mean all those hooks that return information, and that are not invoked to handle an event or after/before an event occurred/occurs (as in the case of hook_node_delete()). In the list of "similar hooks," I would add, for example:

  • your answer made clear many things for me including the steps which happen in whole story. thanks a lot – weber85 Apr 9 '13 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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