7

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.

8
  • 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 Commented Jun 30, 2019 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
    Commented Jun 30, 2019 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? Commented Jun 30, 2019 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
    Commented Jun 30, 2019 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
    Commented Jun 30, 2019 at 11:43

2 Answers 2

7

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);
}
5
  • 1
    but is not processed in background like cron, right? Commented Jan 7, 2020 at 9:25
  • 1
    Are you referring to the last snippet I show? It depends from the hook where you put the code, and the meaning you give to background. A hook that isn't executed because something I did on a site (for example, a hook invoked during cron tasks) is background, for me.
    – avpaderno
    Commented Jan 7, 2020 at 9:42
  • 1
    I mean a background task is like what is done by cron, without staying on current page, invisible to users, but i think drupal cron is the only way to do it (or by external cron in combination with controller and a routing path). Commented Jan 7, 2020 at 17:46
  • 2
    It is called from the code that invokes hook_cron(), but it could be called also from a different function/method, even from a module you wrote.
    – avpaderno
    Commented Jun 12, 2020 at 16:05
  • In Drupal 9.5.1, you can get queues skipped by cron simply by not providing the cron section of the @QueueWorker annotation. Queue processing can then be done using drush queue:run. The reason for that can be seen in Drupal\Core\Cron\Cron::processQueues().
    – Jedihe
    Commented Jan 30, 2023 at 21:44
9

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 and acknowledge you have read our privacy policy.

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