1

I implemented hook_queue_info() with the following code.

function ecc_general_queue_info() {
  $queues = array();
  $queues['adding_checklist'] = array(
    'worker callback' => '_ecc_general_apply_queue',
    'time' => 60,
  );
  return $queues;
}

I create items for the queue with the following code.

$queue_factory = \Drupal::service('queue');
$queue = $queue_factory->get('adding_checklist');
$queue->createItem([$node, 'insert']);

The callback function is the following.

function _ecc_general_apply_queue($item) {
  // Some code here.
}

I want to run this callback for each item in the queue. How can I process the queue to execute its worker callback for each item?

I don't want to use cron tasks for processing the queue.

  • 1
    I'm not sure what you are trying to do because the code sample doesn't give enough context but if you want to execute something when a node is updated you can use hook_entity_update or hook_entity_presave for example – wranvaud Jun 30 at 8:55
  • @wranvaud Sorry if couldn't explain the problem properly. The issue is actually with queue processing, I want to do some operations using Drupal 8 Queue API. I have created the queue, pushed items to it and now don't know how to process it. – Mohandes Jun 30 at 9:20
  • Take a look at the documentation for hook_entity_update, this hook is called every time an entity is updated. Try calling your callback function there? – wranvaud Jun 30 at 9:44
  • Getting closer to the solution, thank you @wranvaud. I used the hook_entity_update and successfully called my callback function inside of that hook. Now I want to run this callback function by Queue, so that I want to run this function for each item in the queue. – Mohandes Jun 30 at 9:54
  • hook_general_queue_info doesn't seem to exist. If you need to run your queue not with cron, then you need to code your own way of running the queue. There could be any number of ways to do this, but looking at how cron runs through queues would be a start. There might be some queue runner modules that fit your use case. – mradcliffe Jun 30 at 11:43
1

Drupal 8 doesn't use hook_queue_info() but annotates the queue class using the @QueueWorker annotation, as in the following code taken from a Drupal core module.

/**
 * Updates a feed's items.
 *
 * @QueueWorker(
 *   id = "aggregator_feeds",
 *   title = @Translation("Aggregator refresh"),
 *   cron = {"time" = 60}
 * )
 */
class AggregatorRefresh extends QueueWorkerBase {

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
    if ($data instanceof FeedInterface) {
      $data
        ->refreshItems();
    }
  }

}

That, anyway, is not an annotation for a generic queue, but for a queue handled by the Cron class (which is the class implementing the cron service).

If you don't need to handle your queue during cron tasks, you simply don't use the @QueueWorker annotation.

The code to consume queue items is similar to the following one. (The code is specific for your case.)

$queue = \Drupal::service('queue')->get('adding_checklist');
while ($item = $queue->claimItem()) {
  $node = $item->data[0];
  // ...
  $queue->deleteItem($item);
}
2

Queue is processed by cron through core. You can also use Drush and Drupal Console to run queue items as well.

Drush:

drush queue:list to get the available queues.

drush queue:run to run a single item.

drush queue:delete to clear a queue.

Drupal Console:

drupal debug:queue to debug your queue.

drupal queue:run <name-of-the-queue> To run a single item.

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.