diff --git a/core/core.services.yml b/core/core.services.yml index b97f115..d04bcc6 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -150,7 +150,7 @@ services: arguments: ['@typed_data_manager'] cron: class: Drupal\Core\Cron - arguments: ['@module_handler', '@lock', '@queue', '@state', '@current_user', '@session_manager', '@logger.channel.cron'] + arguments: ['@module_handler', '@lock', '@queue', '@state', '@current_user', '@session_manager', '@logger.channel.cron', '@plugin.manager.queue'] diff.formatter: class: Drupal\Core\Diff\DiffFormatter arguments: ['@config.factory'] @@ -346,9 +346,9 @@ services: plugin.manager.display_variant: class: Drupal\Core\Display\VariantManager parent: default_plugin_manager - plugin.manager.queue.item: + plugin.manager.queue: class: Drupal\Core\Queue\QueuePluginManager - arguments: ['@container.namespaces', '@module_handler'] + parent: default_plugin_manager plugin.cache_clearer: class: Drupal\Core\Plugin\CachedDiscoveryClearer paramconverter.menu_link: diff --git a/core/lib/Drupal/Core/Annotation/QueuePlugin.php b/core/lib/Drupal/Core/Annotation/QueuePlugin.php index c30bf2d..5366b51 100644 --- a/core/lib/Drupal/Core/Annotation/QueuePlugin.php +++ b/core/lib/Drupal/Core/Annotation/QueuePlugin.php @@ -26,7 +26,7 @@ * @see hook_cron() * @see hook_queue_info_alter() * @see \Drupal\Core\Queue\QueuePluginInterface - * @see \Drupal\Core\Queue\QueuePluginManager + * @see \Drupal\Core\Queue\QueuePluginManagerInterface * * @Annotation */ diff --git a/core/lib/Drupal/Core/Cron.php b/core/lib/Drupal/Core/Cron.php index cbb1a9c..8bfe378 100644 --- a/core/lib/Drupal/Core/Cron.php +++ b/core/lib/Drupal/Core/Cron.php @@ -8,6 +8,7 @@ namespace Drupal\Core; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\Core\Queue\QueuePluginManagerInterface; use Drupal\Core\State\StateInterface; use Drupal\Core\Lock\LockBackendInterface; use Drupal\Core\Queue\QueueFactory; @@ -72,6 +73,13 @@ class Cron implements CronInterface { protected $logger; /** + * The queue plugin manager. + * + * @var \Drupal\Core\Queue\QueuePluginManagerInterface + */ + protected $queueManager; + + /** * Constructs a cron object. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler @@ -88,8 +96,10 @@ class Cron implements CronInterface { * The session manager. * @param \Psr\Log\LoggerInterface $logger * A logger instance. + * @param \Drupal\Core\Queue\QueuePluginManagerInterface + * The queue plugin manager. */ - public function __construct(ModuleHandlerInterface $module_handler, LockBackendInterface $lock, QueueFactory $queue_factory, StateInterface $state, AccountProxyInterface $current_user, SessionManagerInterface $session_manager, LoggerInterface $logger) { + public function __construct(ModuleHandlerInterface $module_handler, LockBackendInterface $lock, QueueFactory $queue_factory, StateInterface $state, AccountProxyInterface $current_user, SessionManagerInterface $session_manager, LoggerInterface $logger, QueuePluginManagerInterface $queue_manager) { $this->moduleHandler = $module_handler; $this->lock = $lock; $this->queueFactory = $queue_factory; @@ -97,6 +107,7 @@ public function __construct(ModuleHandlerInterface $module_handler, LockBackendI $this->currentUser = $current_user; $this->sessionManager = $session_manager; $this->logger = $logger; + $this->queueManager = $queue_manager; } /** @@ -162,16 +173,13 @@ protected function setCronLastTime() { */ protected function processQueues() { // Grab the defined cron queues. - $manager = \Drupal::service('plugin.manager.queue.item'); - $queues = $manager->getDefinitions(); - - foreach ($queues as $queue_name => $info) { + foreach ($this->queueManager->getDefinitions() as $queue_name => $info) { if (isset($info['cron'])) { // Make sure every queue exists. There is no harm in trying to recreate // an existing queue. $this->queueFactory->get($queue_name)->createQueue(); - $queue_item = $manager->createInstance($queue_name); + $queue_item = $this->queueManager->createInstance($queue_name); $end = time() + (isset($info['cron']['time']) ? $info['cron']['time'] : 15); $queue = $this->queueFactory->get($queue_name); while (time() < $end && ($item = $queue->claimItem())) { diff --git a/core/lib/Drupal/Core/Queue/QueueFactory.php b/core/lib/Drupal/Core/Queue/QueueFactory.php index 6dce837..70824c3 100644 --- a/core/lib/Drupal/Core/Queue/QueueFactory.php +++ b/core/lib/Drupal/Core/Queue/QueueFactory.php @@ -50,7 +50,7 @@ function __construct(Settings $settings) { * least once is important, FALSE if scalability is the main concern. Defaults * to FALSE. * - * @return \Drupal\Core\QueueStore\QueueInterface + * @return \Drupal\Core\Queue\QueueInterface * A queue implementation for the given name. */ public function get($name, $reliable = FALSE) { diff --git a/core/lib/Drupal/Core/Queue/QueuePluginBase.php b/core/lib/Drupal/Core/Queue/QueuePluginBase.php index 04a8288..87fa4cd 100644 --- a/core/lib/Drupal/Core/Queue/QueuePluginBase.php +++ b/core/lib/Drupal/Core/Queue/QueuePluginBase.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\Core\Queue\QueuePluginBase. + * Contains \Drupal\Core\Queue\QueuePluginBase. */ namespace Drupal\Core\Queue; diff --git a/core/lib/Drupal/Core/Queue/QueuePluginInterface.php b/core/lib/Drupal/Core/Queue/QueuePluginInterface.php index 4914270..bada537 100644 --- a/core/lib/Drupal/Core/Queue/QueuePluginInterface.php +++ b/core/lib/Drupal/Core/Queue/QueuePluginInterface.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\Core\Queue\QueuePluginInterface. + * Contains \Drupal\Core\Queue\QueuePluginInterface. */ namespace Drupal\Core\Queue; diff --git a/core/lib/Drupal/Core/Queue/QueuePluginManager.php b/core/lib/Drupal/Core/Queue/QueuePluginManager.php index e79fdae..3253422 100644 --- a/core/lib/Drupal/Core/Queue/QueuePluginManager.php +++ b/core/lib/Drupal/Core/Queue/QueuePluginManager.php @@ -7,13 +7,14 @@ namespace Drupal\Core\Queue; +use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\DefaultPluginManager; /** * Defines the queue item manager. */ -class QueuePluginManager extends DefaultPluginManager { +class QueuePluginManager extends DefaultPluginManager implements QueuePluginManagerInterface { /** * Constructs an QueuePluginManager object. @@ -21,12 +22,16 @@ class QueuePluginManager extends DefaultPluginManager { * @param \Traversable $namespaces * An object that implements \Traversable which contains the root paths * keyed by the corresponding namespace to look for plugin implementations. + * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend + * Cache backend instance to use. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler. */ - public function __construct(\Traversable $namespaces, ModuleHandlerInterface $module_handler) { - parent::__construct('Plugin/QueuePlugin', $namespaces, 'Drupal\Core\Annotation\QueuePlugin'); - $this->alterInfo($module_handler, 'queue_info'); + public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) { + parent::__construct('Plugin/QueuePlugin', $namespaces, $module_handler, 'Drupal\Core\Queue\QueuePluginInterface', 'Drupal\Core\Annotation\QueuePlugin'); + + $this->setCacheBackend($cache_backend, 'queue_plugins'); + $this->alterInfo('queue_info'); } /** @@ -37,10 +42,19 @@ public function processDefinition(&$definition, $plugin_id) { // Assign a default time if a cron is specified. if (isset($definition['cron'])) { - $definition['cron'] += array( + $definition['cron'] += [ 'time' => 15, - ); + ]; } } + /** + * {@inheritdoc} + * + * @return \Drupal\Core\Queue\QueuePluginInterface + */ + public function createInstance($plugin_id, array $configuration = []) { + return parent::createInstance($plugin_id, $configuration); + } + } diff --git a/core/lib/Drupal/Core/Queue/QueuePluginManagerInterface.php b/core/lib/Drupal/Core/Queue/QueuePluginManagerInterface.php new file mode 100644 index 0000000..8fdd555 --- /dev/null +++ b/core/lib/Drupal/Core/Queue/QueuePluginManagerInterface.php @@ -0,0 +1,17 @@ +refreshItems(); + } } } diff --git a/core/modules/aggregator/src/Tests/AggregatorCronTest.php b/core/modules/aggregator/src/Tests/AggregatorCronTest.php index 8a8ee7d..59b6386 100644 --- a/core/modules/aggregator/src/Tests/AggregatorCronTest.php +++ b/core/modules/aggregator/src/Tests/AggregatorCronTest.php @@ -21,11 +21,11 @@ public function testCron() { $this->createSampleNodes(); $feed = $this->createFeed(); $this->cronRun(); - $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField(), 'Expected number of items in database.'); + $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField()); $this->deleteFeedItems($feed); - $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField(), 'Expected number of items in database.'); + $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField()); $this->cronRun(); - $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField(), 'Expected number of items in database.'); + $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField()); // Test feed locking when queued for update. $this->deleteFeedItems($feed); @@ -36,7 +36,7 @@ public function testCron() { )) ->execute(); $this->cronRun(); - $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField(), 'Expected number of items in database.'); + $this->assertEqual(0, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField()); db_update('aggregator_feed') ->condition('fid', $feed->id()) ->fields(array( @@ -44,6 +44,6 @@ public function testCron() { )) ->execute(); $this->cronRun(); - $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField(), 'Expected number of items in database.'); + $this->assertEqual(5, db_query('SELECT COUNT(*) FROM {aggregator_item} WHERE fid = :fid', array(':fid' => $feed->id()))->fetchField()); } } diff --git a/core/modules/locale/lib/Drupal/locale/Plugin/QueuePlugin/LocaleTranslation.php b/core/modules/locale/src/Plugin/QueuePlugin/LocaleTranslation.php similarity index 91% rename from core/modules/locale/lib/Drupal/locale/Plugin/QueuePlugin/LocaleTranslation.php rename to core/modules/locale/src/Plugin/QueuePlugin/LocaleTranslation.php index 0a52ece..6ecb715 100644 --- a/core/modules/locale/lib/Drupal/locale/Plugin/QueuePlugin/LocaleTranslation.php +++ b/core/modules/locale/src/Plugin/QueuePlugin/LocaleTranslation.php @@ -7,8 +7,6 @@ namespace Drupal\locale\Plugin\QueuePlugin; -use Drupal\Core\Annotation\QueuePlugin; -use Drupal\Core\Annotation\Translation; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Queue\QueueInterface; @@ -62,7 +60,7 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, @@ -95,18 +93,18 @@ public function processItem($data) { // through via the queue and is part of the $data. $last = count($args) - 1; if (!is_array($args[$last]) || !isset($args[$last]['finished'])) { - $batch_context = array( - 'sandbox' => array(), - 'results' => array(), + $batch_context = [ + 'sandbox' => [], + 'results' => [], 'finished' => 1, 'message' => '', - ); + ]; } else { $batch_context = $args[$last]; unset ($args[$last]); } - $args = array_merge($args, array(&$batch_context)); + $args = array_merge($args, [&$batch_context]); // Call the batch operation function. call_user_func_array($function, $args); @@ -115,7 +113,7 @@ public function processItem($data) { // continue the task. This is typically the translation import task. if ($batch_context['finished'] < 1) { unset($batch_context['strings']); - $this->queue->createItem(array($function, $args)); + $this->queue->createItem([$function, $args]); } } diff --git a/core/modules/system/system.api.php b/core/modules/system/system.api.php index 932a143..0c3dbb8 100644 --- a/core/modules/system/system.api.php +++ b/core/modules/system/system.api.php @@ -118,35 +118,6 @@ function hook_queue_info_alter(&$queues) { } /** - * Work on a single queue item. - * - * Callback for hook_queue_info(). - * - * @param $queue_item_data - * The data that was passed to \Drupal\Core\Queue\QueueInterface::createItem() - * when the item was queued. - * - * @throws \Exception - * The worker callback may throw an exception to indicate there was a problem. - * The cron process will log the exception, and leave the item in the queue to - * be processed again later. - * @throws \Drupal\Core\Queue\SuspendQueueException - * More specifically, a SuspendQueueException should be thrown when the - * callback is aware that the problem will affect all subsequent workers of - * its queue. For example, a callback that makes HTTP requests may find that - * the remote server is not responding. The cron process will behave as with a - * normal Exception, and in addition will not attempt to process further items - * from the current item's queue during the current cron run. - * - * @see \Drupal\Core\Cron::run() - */ -function callback_queue_worker($queue_item_data) { - $node = node_load($queue_item_data); - $node->title = 'Updated title'; - $node->save(); -} - -/** * Allows modules to declare their own Form API element types and specify their * default values. * diff --git a/core/modules/system/tests/modules/cron_queue_test/cron_queue_test.module b/core/modules/system/tests/modules/cron_queue_test/cron_queue_test.module deleted file mode 100644 index c46fdb9..0000000 --- a/core/modules/system/tests/modules/cron_queue_test/cron_queue_test.module +++ /dev/null @@ -1,38 +0,0 @@ - t('Exception test'), - 'worker callback' => 'cron_queue_test_exception', - // Only needed if this queue should be processed by cron. - 'cron' => array( - 'time' => 60, - ), - ); - $queues['cron_queue_test_broken_queue'] = array( - 'title' => t('Broken queue test'), - 'worker callback' => 'cron_queue_test_broken_queue', - // Only needed if this queue should be processed by cron. - 'cron' => array( - 'time' => 60, - ), - ); - - return $queues; -} - -function cron_queue_test_exception($item) { - throw new Exception('That is not supposed to happen.'); -} - -/** - * Implements callback_queue_worker(). - * - * This queue is declared broken if the queue item data is 'crash'. - */ -function cron_queue_test_broken_queue($queue_item_data) { - if ($queue_item_data == 'crash') { - throw new \Drupal\Core\Queue\SuspendQueueException('The queue is broken.'); - } - // Do nothing otherwise. -} diff --git a/core/modules/system/tests/modules/cron_queue_test/src/Plugin/QueuePlugin/CronQueueTestBrokenQueue.php b/core/modules/system/tests/modules/cron_queue_test/src/Plugin/QueuePlugin/CronQueueTestBrokenQueue.php new file mode 100644 index 0000000..b70f7b6 --- /dev/null +++ b/core/modules/system/tests/modules/cron_queue_test/src/Plugin/QueuePlugin/CronQueueTestBrokenQueue.php @@ -0,0 +1,32 @@ +