В Drupal 8 имеется инструмент для автоматической обработки очередей по крону. Имя ему QueueWorker. Более подробно о нем можно узнать здесь.
Я же расскажу о том, как сделать так, что бы обработанные задачи перемещались в конец очереди для повторной обработки. Причины для такого поведения могут быть самые разные. Например, ноды определенного типа должны синхронизироваться с внешними сервисами до тех пор, пока эти ноды не будут удалены. Для этого мы при создании ноды создаем 1 раз задачу на синхронизацию, а затем QueueWorker будет их помещать в конец очереди для повторной обработки (реально будет создаваться новая задача, но нас это полностью устраивает).
За основу возьмем такой плагин:
<?php
namespace Drupal\example\Plugin\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
/**
* Synchronises nodes.
*
* @QueueWorker(
* id = "node_sync",
* title = @Translation("Node synchronise worker"),
* cron = {"time" = 60}
* )
*/
class NodeSync extends QueueWorkerBase {
/**
* {@inheritdoc}
*/
public function processItem($data) {
$node = Node::load($data['nid']);
if (!$node instanceof NodeInterface) {
return;
}
// Perform sync operation.
}
}
Для решения поставленной задачи, создадим новый сервис, который будет помечен тегом needs_destruction. Данный тег позволяет выполнить какие либо операции, перед тем, как сервис будет разрушен. Этот сервис будет использоваться для временного хранения обработых задач, а при разрушении будет повторно создавать новые задачи.
Описываем сервис в файле example.services.yml:
services:
example.queue_worker.tracker:
class: Drupal\example\Queue\QueueWorkerTracker
arguments: ['@queue']
tags:
- { name: needs_destruction }
Создаем интерфейс для сервиса src/Queue/QueueWorkerTrackerInterface.php:
<?php
namespace Drupal\example\Queue;
/**
* Defines an interface for the tracker of the processed items by queue workers.
*/
interface QueueWorkerTrackerInterface {
/**
* Tracks given data to be re-queued for processing again on next cron run.
*
* @param string $queue_name
* The name of the queue to push the given data.
* @param mixed $data
* Arbitrary data to be associated with the new task in the queue.
*/
public function trackItem(string $queue_name, $data);
}
Создаем класс для сервиса src/Queue/QueueWorkerTracker.php:
<?php
namespace Drupal\example\Queue;
use Drupal\Core\DestructableInterface;
use Drupal\Core\Queue\QueueFactory;
/**
* Defines the tracker of the processed items by queue workers.
*/
class QueueWorkerTracker implements QueueWorkerTrackerInterface, DestructableInterface {
/**
* The queue service.
*
* @var \Drupal\Core\Queue\QueueFactory
*/
protected $queueFactory;
/**
* The tracked items grouped by queue name to be re-queued again.
*
* @var array
*/
protected $items;
/**
* QueueWorkerTracker constructor.
*
* @param \Drupal\Core\Queue\QueueFactory $queue_factory
* The queue service.
*/
public function __construct(QueueFactory $queue_factory) {
$this->queueFactory = $queue_factory;
}
/**
* {@inheritdoc}
*/
public function destruct() {
foreach ($this->items as $queue_name => $items) {
$queue = $this->queueFactory->get($queue_name);
foreach ($items as $item) {
$queue->createItem($item);
}
}
}
/**
* {@inheritdoc}
*/
public function trackItem(string $queue_name, $data) {
$this->items[$queue_name][] = $data;
}
}
Теперь изменяем сам QueueWorker, пробрасываем в него созданный сервис и регистрируем обработанные данные:
<?php
namespace Drupal\example\Plugin\QueueWorker;
use Drupal\example\Queue\QueueWorkerTrackerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Synchronises nodes.
*
* @QueueWorker(
* id = "node_sync",
* title = @Translation("Node synchronise worker"),
* cron = {"time" = 60}
* )
*/
class NodeSync extends QueueWorkerBase implements ContainerFactoryPluginInterface {
/**
* The queue worker tracker service.
*
* @var \Drupal\example\Queue\QueueWorkerTrackerInterface
*/
protected $queueWorkerTracker;
/**
* NodeSync constructor.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\example\Queue\QueueWorkerTrackerInterface $queue_worker_tracker
* The queue worker tracker service.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
QueueWorkerTrackerInterface $queue_worker_tracker
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->queueWorkerTracker = $queue_worker_tracker;
}
/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('example.queue_worker.tracker')
);
}
/**
* {@inheritdoc}
*/
public function processItem($data) {
$node = Node::load($data['nid']);
if (!$node instanceof NodeInterface) {
return;
}
// Perform sync operation.
// Track processed item to re-queue it again on service destruction.
$this->queueWorkerTracker->trackItem($this->getPluginId(), $data);
}
}
Задачу можно решить и без использования дополнительного сервиса с помощью метода __destruct() внутри самого воркера, но если вы используете модуль Queue UI и запускаете очередь из админки, то он не подойтет, поскольку это приведет к зацикливанию очереди и батч никогда не закончит работу. Метод с отдельным сервисом универсальнее и проверялся при запуске через Cron, Drush и Queue UI.