0

I think I am close to getting a queue to work with the Drupal 7 Queue API. I have some code that returns a value to me that I want to retrieve on cron.

function ...{
  $guid_to_enqueue = $guid;
  //Add GUID into the queue so we can work with it on cron
  $queue = DrupalQueue::get('custom_application_guid_queue');
  $queue->createQueue();
  $queue->createItem($guid_to_enqueue);
}

I then have

function custom_application_cron_queue_info() {
  $queues[ 'custom_application_guid_queue' ] = array(
    'worker callback' => '_custom_application_guid_item',
    'time' => 30,
  );
  watchdog('custom_application_ques_on_cron',  '<pre>' . print_r($queues, TRUE) . '</pre>');
  return $queues;
}

I think the problem is when I try and use the callback. I am not sure how to iterate over my data that is in the queue. I know it is in the queue using Drush and if I use Watchdog() with $guid_to_enqueue I can see the right value in the variable.

function _custom_application_guid_item($queues) {
 foreach($queues as $quid_item) {
   watchdog('custom_application_guid_item_foreach', $quid_item);
 }
}

Any help with figuring out what I am doing wrong here is greatly appreciated. I have also tried getting the number of items in the queue but nothing is getting that far in my code.

I will add that I will rarely have more than two items in the queue if that changes anything.

Thanks, Josh

  • It's first in first out I believe – Kevin Jul 28 '17 at 1:21
2

Your queue worker callback takes a single queue item to process at a time, and you do not invoke it manually because you have setup cron to do it for you.

Drupal cron will try to process as many queue items within the time that you provided in hook_cron_queue_info(), and this should be first in, first out by the time the queue item was added and then by the primary key.

So in your cron queue worker callback you should expect the data that you enqueued.

function _custom_application_guid_item($guid_enqueued) {
  // Do something with $guid_enqueued
}
  • Sorry, I am still new to PHP where is $guid_enqueued coming from? I thought this would be $queues because I return $queues in custom_application_cron_queue_info(). Also, I am confused because when I use watchdog() or DSM() there is nothing in these vars. – Josh Jul 28 '17 at 17:44
  • 1
    When cron fires and calls the callback, it will pass one item to it from the queue, in this example it is $guid_enqueued or call it whatever you want. That is what you will operate on. Is it a string, an array, an object that is stored in the queue item? If it is not an array, the foreach in your post will do nothing, hence, watchdog doesn't get hit. After that, it is then removed from the queue (I think), because it was "processed" out. – Kevin Jul 28 '17 at 19:33
  • 1
    Check the docs on DrupalQueue as well. Basically the callback passes whatever item was stored in the given queue for processing, it will do as many as it can in the given time limit. Here is an article that might shed more light: internetdevels.com/blog/drupal-7-queue-api – Kevin Jul 28 '17 at 19:41

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.