Defining a queue for background tasksfor Drupal 8 , 9 , 10 , and 11

Last updated :  

Introduction

As you develop more complex Drupal modules, you might find the need to handle tasks that aren't user time-sensitive. For example, sending out bulk emails, processing data imports, or syncing content. These tasks can be efficiently managed using Drupal's Queue API. In this lesson, we'll explore how to define a queue for background tasks within a Drupal module.

Understanding the Queue API

The Queue API in Drupal provides a mechanism for managing tasks that need to be processed asynchronously. This allows you to defer processing and focus on delivering real-time content to users, improving overall site performance.

Key Concepts:

  • Queue: The storage location for tasks to be processed.
  • Worker: The code responsible for processing the tasks in the queue.
  • Lease: The time duration for which a worker holds a queue item before releasing it if it hasn't finished processing.

Implementing a Queue in a Drupal Module

Let's go through the steps to define a queue for handling background tasks in a module. We'll continue with our example module mymodule to illustrate this.

Step 1: Define the Queue Service

In the mymodule.services.yml file, define a new service for the queue. This service will allow you to specify a queue worker that will handle your tasks:


services:
  mymodule.queue_some_task_worker:
    class: \Drupal\mymodule\Plugin\QueueWorker\SomeTaskQueueWorker
    tags:
      - { name: 'queue_worker', title: 'Some Task Queue', cron: 'time_specification' }

Explanation:

This service definition specifies a class to handle the tasks and tags it as a queue_worker. The cron key allows you to specify when the queue should be run using Drupal's cron system, like 'every hour', for example.

Step 2: Create the Queue Worker Class

Next, create the queue worker class in src/Plugin/QueueWorker/SomeTaskQueueWorker.php:



namespace Drupal\mymodule\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;

/**
 * A queue worker for some tasks.
 *
 * @QueueWorker(
 *   id = "mymodule_some_task",
 *   title = @Translation("Some Task Worker"),
 *   cron = {"time" : 60}
 * )
 */
class SomeTaskQueueWorker extends QueueWorkerBase {

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {
    // Your code to process a queue item goes here.
    \Drupal::logger('mymodule')--->notice('Processing item: ' . print_r($data, TRUE));
  }

}

Explanation:

This class extends QueueWorkerBase and implements a processItem() method where the logic for processing each queue item is defined. The @QueueWorker annotation gives it an ID and a title, making it recognizable within Drupal.

Running the Queue

With the worker defined, you can manage when the queue processes items using either manual cron runs or automated with Drush commands. Here are a couple of ways to run the queue:

  • Manual Cron Run: Navigate to /admin/config/system/cron and run cron tasks manually.
  • Drush Command: Use drush queue-run mymodule_some_task to process the queue items with Drush, Drupal's command-line interface tool.

Testing the Queue

You can test the queue worker by adding items to the queue and observing how they are processed. You might want to log each item or change the status of entities processed by the queue to track progress.

This basic framework sets up a queue for managing background tasks in Drupal. It separates lengthy operations from critical user interactions, effectively balancing performance and functionality.

Conclusion

In this lesson, we've explored how to define a queue for background tasks in a Drupal module. Queues are essential for tasks that aren't immediately critical, allowing your site to handle operations efficiently without impacting user experience.

Queues are uniquely effective, providing flexible and powerful asynchronous operations. In the next lesson, we'll delve into enqueuing items programmatically to further enhance your understanding of working with queues in Drupal. Stay engaged as we continue to build on these concepts!