Skip to content
Snippets Groups Projects
Verified Commit f2e1e5c5 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3348832 by acbramley, MegaChriz, smustgrave: Running cron queues that...

Issue #3348832 by acbramley, MegaChriz, smustgrave: Running cron queues that use a derivative is broken
parent 6a8409a5
No related branches found
No related tags found
26 merge requests!12227Issue #3181946 by jonmcl, mglaman,!54479.5.x SF update,!5014Issue #3071143: Table Render Array Example Is Incorrect,!4868Issue #1428520: Improve menu parent link selection,!4289Issue #1344552 by marcingy, Niklas Fiekas, Ravi.J, aleevas, Eduardo Morales...,!4114Issue #2707291: Disable body-level scrolling when a dialog is open as a modal,!4100Issue #3249600: Add support for PHP 8.1 Enums as allowed values for list_* data types,!2378Issue #2875033: Optimize joins and table selection in SQL entity query implementation,!2334Issue #3228209: Add hasRole() method to AccountInterface,!2062Issue #3246454: Add weekly granularity to views date sort,!1591Issue #3199697: Add JSON:API Translation experimental module,!1484Exposed filters get values from URL when Ajax is on,!1255Issue #3238922: Refactor (if feasible) uses of the jQuery serialize function to use vanillaJS,!1162Issue #3100350: Unable to save '/' root path alias,!1105Issue #3025039: New non translatable field on translatable content throws error,!1073issue #3191727: Focus states on mobile second level navigation items fixed,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!925Issue #2339235: Remove taxonomy hard dependency on node module,!877Issue #2708101: Default value for link text is not saved,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links,!844Resolve #3036010 "Updaters",!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493,!485Sets the autocomplete attribute for username/password input field on login form.,!30Issue #3182188: Updates composer usage to point at ./vendor/bin/composer
...@@ -191,23 +191,19 @@ protected function setCronLastTime() { ...@@ -191,23 +191,19 @@ protected function setCronLastTime() {
protected function processQueues() { protected function processQueues() {
$max_wait = (float) $this->queueConfig['suspendMaximumWait']; $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. // 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 */ /** @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) { $queues = [];
$queue_name = $queue_info['id']; foreach ($this->queueManager->getDefinitions() as $queue_name => $queue_info) {
if (!isset($queue_info['cron'])) {
continue;
}
$queue = $this->queueFactory->get($queue_name); $queue = $this->queueFactory->get($queue_name);
// Make sure every queue exists. There is no harm in trying to recreate // Make sure every queue exists. There is no harm in trying to recreate
// an existing queue. // an existing queue.
$queue->createQueue(); $queue->createQueue();
$worker = $this->queueManager->createInstance($queue_name); $worker = $this->queueManager->createInstance($queue_name);
return [ $queues[] = [
// Set process_from to zero so each queue is always processed // Set process_from to zero so each queue is always processed
// immediately for the first time. This process_from timestamp will // immediately for the first time. This process_from timestamp will
// change if a queue throws a delayable SuspendQueueException. // change if a queue throws a delayable SuspendQueueException.
...@@ -215,7 +211,7 @@ function (array $queueInfo) { ...@@ -215,7 +211,7 @@ function (array $queueInfo) {
'queue' => $queue, 'queue' => $queue,
'worker' => $worker, 'worker' => $worker,
]; ];
}, $queues); }
// Work through stack of queues, re-adding to the stack when a delay is // Work through stack of queues, re-adding to the stack when a delay is
// necessary. // necessary.
......
<?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;
}
}
<?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);
}
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Queue\DatabaseQueue; use Drupal\Core\Queue\DatabaseQueue;
use Drupal\Core\Queue\Memory; 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\CronQueueTestException;
use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestRequeueException; use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestRequeueException;
use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestSuspendQueue; use Drupal\cron_queue_test\Plugin\QueueWorker\CronQueueTestSuspendQueue;
...@@ -330,6 +331,19 @@ public function testQueueWorkerManagerSafeguard(): void { ...@@ -330,6 +331,19 @@ public function testQueueWorkerManagerSafeguard(): void {
$this->assertEquals(QueueWorkerManagerInterface::DEFAULT_QUEUE_CRON_TIME, $definition['cron']['time']); $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} * {@inheritdoc}
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment