1

For each cron run, I would like to be able to process only 1 item in my queue using QueueWorker. At the moment it seems to me that Drupal keep processing items until it reaches the time parameter for the queue worker. I want to change this behavior to process 1 queue item at a time regardless of the time param. This is what I have so far:

/**
 * Processes Tasks.
 *
 * @QueueWorker(
 *   id = "data_provider_queue",
 *   title = @Translation("task worker: data provider queue"),
 *   cron = {"time" = 60}
 * )
 */
class SomeQueueWorker extends QueueWorkerBase {
  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
       // Do something with data.
       // This seems to keep getting called for more than 
       // one item in the queue. How do we change that?
  }
}

Is this possible with QueueWorker or should I just process queue items in a cron hook manually?

2
  • Why do you want them to be processed just one at a cron run?
    – Eyal
    Jan 16, 2018 at 18:51
  • process might be time consuming and I want to ensure integrity of each. So yea I guess I'll just be using regular cron hook.
    – awm
    Jan 16, 2018 at 19:03

2 Answers 2

2

Queue workers are meant to be processed in batches, not 1-each-cron-run.

If you want to process only a single item every cron run, then use the cron-hook.

1

As per @Eyal's answer, process it via a cron hook like:

/**
 * Implements hook_cron().
 */
function example_cron() {
  $queue = \Drupal::queue('data_provider_queue');

  if ($queue_item = $queue->claimItem()) {

    /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
    $queue_worker = \Drupal::service('plugin.manager.queue_worker')
      ->createInstance('data_provider_queue');

    try {
      // Process only 1 queue item per cron run.
      $queue_worker->processItem($queue_item->data);
      $queue->deleteItem($queue_item);
    }
    catch (\Exception $e) {
      // Something went wrong...
    }
  }
}

And do not forget to adjust your QueueWorker and discard the cron time, so that it only processes via your cron hook:

/**
 * Processes Tasks.
 *
 * @QueueWorker(
 *   id = "data_provider_queue",
 *   title = @Translation("task worker: data provider queue"),
 * )
 */

Your Answer

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

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