diff --git a/core/lib/Drupal/Core/Cron.php b/core/lib/Drupal/Core/Cron.php index 0dad9211313c892421077a212204fcaae4184ebd..e48e7ef6a79d2e0f2cb84a1982e2ee2b73f93680 100644 --- a/core/lib/Drupal/Core/Cron.php +++ b/core/lib/Drupal/Core/Cron.php @@ -191,23 +191,19 @@ protected function setCronLastTime() { protected function processQueues() { $max_wait = (float) $this->queueConfig['suspendMaximumWait']; - $queues = array_filter( - array_values($this->queueManager->getDefinitions()), - function (array $queueInfo) { - return isset($queueInfo['cron']); - } - ); - // Build a stack of queues to work on. /** @var array<array{process_from: int<0, max>, queue: \Drupal\Core\Queue\QueueInterface, worker: \Drupal\Core\Queue\QueueWorkerInterface}> $queues */ - $queues = array_map(function (array $queue_info) { - $queue_name = $queue_info['id']; + $queues = []; + foreach ($this->queueManager->getDefinitions() as $queue_name => $queue_info) { + if (!isset($queue_info['cron'])) { + continue; + } $queue = $this->queueFactory->get($queue_name); // Make sure every queue exists. There is no harm in trying to recreate // an existing queue. $queue->createQueue(); $worker = $this->queueManager->createInstance($queue_name); - return [ + $queues[] = [ // Set process_from to zero so each queue is always processed // immediately for the first time. This process_from timestamp will // change if a queue throws a delayable SuspendQueueException. @@ -215,7 +211,7 @@ function (array $queueInfo) { 'queue' => $queue, 'worker' => $worker, ]; - }, $queues); + } // Work through stack of queues, re-adding to the stack when a delay is // necessary. diff --git a/core/modules/system/tests/modules/cron_queue_test/src/Plugin/Derivative/CronQueueTestDeriver.php b/core/modules/system/tests/modules/cron_queue_test/src/Plugin/Derivative/CronQueueTestDeriver.php new file mode 100644 index 0000000000000000000000000000000000000000..b4943fc3365befa070b306eb49620e4350cdc15c --- /dev/null +++ b/core/modules/system/tests/modules/cron_queue_test/src/Plugin/Derivative/CronQueueTestDeriver.php @@ -0,0 +1,33 @@ +<?php + +namespace Drupal\cron_queue_test\Plugin\Derivative; + +use Drupal\Component\Plugin\Derivative\DeriverBase; + +/** + * Provides a deriver for testing cron queues. + */ +class CronQueueTestDeriver extends DeriverBase { + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + $example_data = [ + 'foo' => 'Foo', + 'bar' => 'Bar', + ]; + + $derivatives = []; + foreach ($example_data as $key => $label) { + $derivatives[$key] = [ + 'title' => strtr('Cron queue test: @label', [ + '@label' => $label, + ]), + ] + $base_plugin_definition; + } + + return $derivatives; + } + +} diff --git a/core/modules/system/tests/modules/cron_queue_test/src/Plugin/QueueWorker/CronQueueTestDeriverQueue.php b/core/modules/system/tests/modules/cron_queue_test/src/Plugin/QueueWorker/CronQueueTestDeriverQueue.php new file mode 100644 index 0000000000000000000000000000000000000000..f181887694f9b5a19de5df307ce2c8674848b852 --- /dev/null +++ b/core/modules/system/tests/modules/cron_queue_test/src/Plugin/QueueWorker/CronQueueTestDeriverQueue.php @@ -0,0 +1,31 @@ +<?php + +namespace Drupal\cron_queue_test\Plugin\QueueWorker; + +use Drupal\Core\Queue\QueueWorkerBase; + +/** + * @QueueWorker( + * id = \Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestDeriverQueue::PLUGIN_ID, + * title = @Translation("Deriver queue test"), + * cron = {"time" = 1}, + * deriver = \Drupal\cron_queue_test\Plugin\Derivative\CronQueueTestDeriver::class + * ) + */ +class CronQueueTestDeriverQueue extends QueueWorkerBase { + + /** + * The plugin ID. + */ + public const PLUGIN_ID = 'cron_queue_test_deriver'; + + /** + * {@inheritdoc} + */ + public function processItem($data) { + $state = \Drupal::state(); + $processed = $state->get(self::PLUGIN_ID, 0); + $state->set(self::PLUGIN_ID, ++$processed); + } + +} diff --git a/core/modules/system/tests/src/Kernel/System/CronQueueTest.php b/core/modules/system/tests/src/Kernel/System/CronQueueTest.php index 3935454feca7f2af3bb87ffa79967075b06a0266..4e2f1e7858cb0d20348369cb04872e26eb638fd6 100644 --- a/core/modules/system/tests/src/Kernel/System/CronQueueTest.php +++ b/core/modules/system/tests/src/Kernel/System/CronQueueTest.php @@ -7,6 +7,7 @@ use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Queue\DatabaseQueue; use Drupal\Core\Queue\Memory; +use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestDeriverQueue; use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestException; use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestRequeueException; use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestSuspendQueue; @@ -330,6 +331,19 @@ public function testQueueWorkerManagerSafeguard(): void { $this->assertEquals(QueueWorkerManagerInterface::DEFAULT_QUEUE_CRON_TIME, $definition['cron']['time']); } + /** + * Tests that cron queues from derivers work. + */ + public function testQueueWorkerDeriver(): void { + $this->assertEquals(0, \Drupal::state()->get(CronQueueTestDeriverQueue::PLUGIN_ID, 0)); + $queue = \Drupal::queue(sprintf('%s:foo', CronQueueTestDeriverQueue::PLUGIN_ID)); + $queue->createItem('foo'); + + $this->cron->run(); + + $this->assertEquals(1, \Drupal::state()->get(CronQueueTestDeriverQueue::PLUGIN_ID)); + } + /** * {@inheritdoc} */