Introduction
Continuing from our last lesson where we explored how to enqueue items programmatically, it's time now to focus on the mechanism that processes these items—the Queue Worker Plugin. This plugin enables you to define the operations to be performed on each queue item, facilitating efficient background task processing in your Drupal module.
Understanding the Queue Worker Plugin
The Queue Worker Plugin is a fundamental part of executing queued tasks in Drupal. It provides a structured way to write PHP logic that processes each item in the queue. The plugin is registered with an annotation, which Drupal uses to manage how items are processed, including settings for cron execution.
Steps to Create a Queue Worker Plugin
Let's guide you through the steps to create a queue worker plugin in your custom module mymodule
. We'll define a plugin that processes user-related tasks enqueued in a previous lesson.
Step 1: Create the Plugin Directory Structure
First, ensure your module has the correct directory structure to contain the plugin. Under the src
folder, create a directory named Plugin/QueueWorker
if it doesn't already exist.
Step 2: Define the Queue Worker Plugin
Inside the QueueWorker
directory, create a PHP file for your plugin. Let's name it UserTaskQueueWorker.php
:
namespace Drupal\mymodule\Plugin\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
/**
* Processes user tasks in the queue.
*
* @QueueWorker(
* id = "mymodule_user_task",
* title = @Translation("User Task Queue Worker"),
* cron = {"time" = 60}
* )
*/
class UserTaskQueueWorker extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
public function processItem($data) {
// Verify if the data contains the expected user task.
if (isset($data['uid']) && $data['operation'] === 'processUser') {
// Logic to process user task.
$user = \Drupal\user\Entity\User::load($data['uid']);
if ($user) {
// Example: Update a field or trigger an event.
\Drupal::logger('mymodule')--->notice('Processing user ID ' . $data['uid']);
}
else {
\Drupal::logger('mymodule')->warning('User ID ' . $data['uid'] . ' not found.');
}
}
else {
// Log missing operation or data error.
\Drupal::logger('mymodule')->error('Invalid data provided for user task processing.');
}
}
}
Explanation:
This plugin extends QueueWorkerBase
and implements the processItem()
method. With the @QueueWorker
annotation, we define its ID, title, and cron execution settings. Inside processItem()
, add the logic that processes each queue item. Here, we're checking if the data contains the necessary 'uid' and executing a simple logging operation.
Step 3: Register the Queue Worker Plugin
The @QueueWorker
annotation automatically registers the plugin with Drupal, allowing the Queue API to recognize and utilize it for processing items in the corresponding queue.
Step 4: Test Your Queue Worker Plugin
- Enqueue items: Use the techniques from the previous lesson to add items to your queue.
- Run the queue: Execute the queue via the Drupal admin interface (modulename/config/system/cron) or using Drush with the command
drush queue-run mymodule_user_task
. - Verify processing: Check your logs at
/admin/reports/dblog
to see the output and confirm that your items are being processed correctly.
Troubleshooting
If the queue isn't processing items as expected, consider the following:
- Debug the data: Verify that the data enqueued contains all necessary information for processing. Adjust the
processItem()
method to handle unexpected data gracefully. - Check annotations: Ensure your plugin's annotations are correct. Typographical errors or incorrect keys can prevent proper recognition by Drupal.
- Cron scheduling: If you're using cron, ensure that the cron time setting is appropriate for your needs and that cron is configured to run regularly.
Conclusion
You've now learned how to create a queue worker plugin in Drupal, enabling efficient processing of background tasks. By customizing the logic in processItem()
, you have the flexibility to handle complex operations while keeping your main application responsive and focused on user interactions.
In our next lesson, we'll dive into configuring queues for cron execution, allowing you to automate processing and further integrate background tasks with your Drupal site's periodic operations. Join us as we continue to refine and enhance your Drupal development skills!