Queue Operations – Manual or Cron Process Item

30 / Sep / 2022 by rakesh.kumar1 0 comments

The drupal queue is for basically any long-running processes or such of any time-consuming task which is taking too much time, so here Drupal core is giving one of the best queue plugins to add items into the queue and process all items later behind the run-time request rather than taking time when loading the page.

The long-running task might be:

  • Images Scaling
  • Sending bulk email
  • Search indexing
  • A large level of logic
  • Any entity’s events to sync with third-party.

We can add items into a queue on any event and later process all items one by one from the queue and execute them in FIFO order.

Here I’ll show you to create an item and then process all items by both manual and cron API but before all, we need to first create a plugin and must add all queue information in the annotation:

Creating a QueueWorker plugin


As you have seen, there is a file UserSyncEventProcessor.php file inside the \src\Plugin\QueueWorker, and added the annotation to register the new plugin with the id of the QueueWorker plugin.

* A User Sync Queue Publisher that process the queue.
* @QueueWorker(
*   id = "user_sync_queue_processor",
*   title = "User Sync Queue Processor"
*   cron = {"time" = 60}
* )

We have added the QueueWorkerBase class in the new UserSyncQueueProcessor.php

* Provides base functionality for the user sync queue Workers.
class UserSyncQueueProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {

This QueueWorker will play the main role for process all items from the queue and delete if process successfully.

All processes execute in the processItem() method from the QueueWorkerInterface as in the screenshot.

* {@inheritdoc}
public function processItem($data) {

That’s it for registering our QueueWorker plugin.

Adding a Queue item:

Now, we can register our queue item by using the below code at the entity save or at any drupal event.

We are passing some dummy $data which can be changed as per the project requirement and will be available inside the processItem method.

We can confirm all items in our queue table in the database, and all serialized data is our $data.

Find more DatabaseQueue available methods in the /core/lib/Drupal/Core/Queue/DatabaseQueue.php file.

Process item by cron:

hook_cron: Execute this command “drush cron” from the terminal or execute it from the system cron page from the /admin/config/system/cron page then it will process and then delete all items from the queue in FIFO order.

Process item by manual:

Actually, the drush has already written a well-managed queue:run command in /vendor/drush/drush/src/Drupal/Commands/core/QueueCommands.php

drush queue:run user_sync_queue_processor

We can manipulate this code as per our requirements like adding more validation, error handling, or debugging at various points, and execute this code at specific events or approval.

That’s it.



Tag -

Drupal Queue

Leave a Reply

Your email address will not be published. Required fields are marked *