Verified Commit fdd6ca5b authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3420988 by godotislate, smustgrave: Convert QueueWorker plugin discovery to attributes

(cherry picked from commit 636182e6)
parent d4565e17
Loading
Loading
Loading
Loading
Loading
+60 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Queue\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Declare a worker class for processing a queue item.
 *
 * Worker plugins are used by some queues for processing the individual items
 * in the queue. In that case, the ID of the worker plugin needs to match the
 * machine name of a queue, so that you can retrieve the queue back end by
 * calling \Drupal\Core\Queue\QueueFactory::get($plugin_id).
 *
 * \Drupal\Core\Cron::processQueues() processes queues that use workers; they
 * can also be processed outside of the cron process.
 *
 * Some queues do not use worker plugins: you can create queues, add items to
 * them, claim them, etc. without using a QueueWorker plugin. However, you will
 * need to take care of processing the items in the queue in that case. You can
 * look at \Drupal\Core\Cron::processQueues() for an example of how to process
 * a queue that uses workers, and adapt it to your queue.
 *
 * Plugin Namespace: Plugin\QueueWorker
 *
 * For a working example, see
 * \Drupal\locale\Plugin\QueueWorker\LocaleTranslation.
 *
 * @see \Drupal\Core\Queue\QueueWorkerInterface
 * @see \Drupal\Core\Queue\QueueWorkerBase
 * @see \Drupal\Core\Queue\QueueWorkerManager
 * @see plugin_api
 *
 * @ingroup queue
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class QueueWorker extends Plugin {

  /**
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup|null $title
   *   The human-readable title of the plugin.
   * @param array|null $cron
   *   (optional) An associative array of settings for cron. The array has one
   *   key, time, which is set to the time Drupal cron should spend on calling
   *   this worker in seconds. The default is set in
   *   \Drupal\Core\Queue\QueueWorkerManager::processDefinition().
   * @param class-string|null $deriver
   *   (optional) The deriver class.
   */
  public function __construct(
    public readonly string $id,
    public readonly ?TranslatableMarkup $title = NULL,
    public readonly ?array $cron = NULL,
    public readonly ?string $deriver = NULL
  ) {}

}
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Queue\Attribute\QueueWorker;

/**
 * Defines the queue worker manager.
@@ -28,7 +29,7 @@ class QueueWorkerManager extends DefaultPluginManager implements QueueWorkerMana
   *   The module handler.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/QueueWorker', $namespaces, $module_handler, 'Drupal\Core\Queue\QueueWorkerInterface', 'Drupal\Core\Annotation\QueueWorker');
    parent::__construct('Plugin/QueueWorker', $namespaces, $module_handler, 'Drupal\Core\Queue\QueueWorkerInterface', QueueWorker::class, 'Drupal\Core\Annotation\QueueWorker');

    $this->setCacheBackend($cache_backend, 'queue_plugins');
    $this->alterInfo('queue_info');
+7 −6
Original line number Diff line number Diff line
@@ -4,19 +4,20 @@

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\Attribute\QueueWorker;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Executes interface translation queue tasks.
 *
 * @QueueWorker(
 *   id = "locale_translation",
 *   title = @Translation("Update translations"),
 *   cron = {"time" = 30}
 * )
 */
#[QueueWorker(
  id: 'locale_translation',
  title: new TranslatableMarkup('Update translations'),
  cron: ['time' => 30]
)]
class LocaleTranslation extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
+7 −6
Original line number Diff line number Diff line
@@ -4,18 +4,19 @@

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\Attribute\QueueWorker;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Process a queue of media items to fetch their thumbnails.
 *
 * @QueueWorker(
 *   id = "media_entity_thumbnail",
 *   title = @Translation("Thumbnail downloader"),
 *   cron = {"time" = 60}
 * )
 */
#[QueueWorker(
  id: 'media_entity_thumbnail',
  title: new TranslatableMarkup('Thumbnail downloader'),
  cron: ['time' => 60]
)]
class ThumbnailDownloader extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
+7 −6
Original line number Diff line number Diff line
@@ -2,18 +2,19 @@

namespace Drupal\cron_queue_test\Plugin\QueueWorker;

use Drupal\Core\Queue\Attribute\QueueWorker;
use Drupal\Core\Queue\DelayedRequeueException;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * A queue worker for testing cron exception handling.
 *
 * @QueueWorker(
 *   id = "cron_queue_test_database_delay_exception",
 *   title = @Translation("Database delay exception test"),
 *   cron = {"time" = 1}
 * )
 */
#[QueueWorker(
  id: 'cron_queue_test_database_delay_exception',
  title: new TranslatableMarkup('Database delay exception test'),
  cron: ['time' => 1]
)]
class CronQueueTestDatabaseDelayException extends QueueWorkerBase {

  const DELAY_INTERVAL = 100;
Loading