From d06dfb35eb1ac148f3a3e5a03931929391a1c015 Mon Sep 17 00:00:00 2001 From: catch Date: Mon, 23 Jan 2023 13:09:03 +0000 Subject: [PATCH] Issue #1875020 by longwave, David_Rothstein, slip, alexpott, catch, smustgrave, jhodgdon: Cron queue gets processed every time cron is called, regardless of whether it's already being processed elsewhere --- core/lib/Drupal/Core/Cron.php | 7 ++++--- core/tests/Drupal/Tests/Core/CronTest.php | 25 ++++++++++++----------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/core/lib/Drupal/Core/Cron.php b/core/lib/Drupal/Core/Cron.php index 74e50206428..569616087d4 100644 --- a/core/lib/Drupal/Core/Cron.php +++ b/core/lib/Drupal/Core/Cron.php @@ -134,6 +134,10 @@ class Cron implements CronInterface { } else { $this->invokeCronHandlers(); + + // Process cron queues. + $this->processQueues(); + $this->setCronLastTime(); // Release cron lock. @@ -143,9 +147,6 @@ class Cron implements CronInterface { $return = TRUE; } - // Process cron queues. - $this->processQueues(); - // Restore the user. $this->accountSwitcher->switchBack(); diff --git a/core/tests/Drupal/Tests/Core/CronTest.php b/core/tests/Drupal/Tests/Core/CronTest.php index 835eb74fab5..52c2d09a78d 100644 --- a/core/tests/Drupal/Tests/Core/CronTest.php +++ b/core/tests/Drupal/Tests/Core/CronTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\Core; +use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\ImmutableConfig; use Drupal\Core\Cron; use Drupal\Core\KeyValueStore\KeyValueMemoryFactory; use Drupal\Core\Queue\DelayedRequeueException; @@ -64,14 +66,8 @@ class CronTest extends UnitTestCase { // Create a mock logger to set a flag in the resulting state. $logger = $this->prophesize('Drupal\Core\Logger\LoggerChannelInterface'); - // Safely ignore the cron re-run message when failing to acquire a lock. - // - // We don't need to run regular cron tasks, and we're still implicitly - // testing that queues are being processed. - // - // This argument will need to be updated to match the message text in - // Drupal\Core\Cron::run() should the original text ever be updated. - $logger->warning(Argument::exact('Attempting to re-run cron while it is already running.'))->shouldBeCalled(); + // Safely ignore the cron success message. + $logger->info('Cron run completed.')->shouldBeCalled(); // Set a flag to track when a message is logged by adding a callback // function for each logging method. foreach (get_class_methods(LoggerInterface::class) as $logger_method) { @@ -87,11 +83,18 @@ class CronTest extends UnitTestCase { // Create a mock time service. $time = $this->prophesize('Drupal\Component\Datetime\TimeInterface'); + // Create a mock config factory and config object. + $config_factory = $this->prophesize(ConfigFactoryInterface::class); + $config = $this->prophesize(ImmutableConfig::class); + $config->get('logging')->willReturn(FALSE); + $config_factory->get('system.cron')->willReturn($config->reveal()); + // Build the container using the resulting mock objects. \Drupal::setContainer(new ContainerBuilder()); \Drupal::getContainer()->set('logger.factory', $logger_factory->reveal()); \Drupal::getContainer()->set('datetime.time', $time->reveal()); \Drupal::getContainer()->set('state', $this->state); + \Drupal::getContainer()->set('config.factory', $config_factory->reveal()); // Create mock objects for constructing the Cron class. $module_handler = $this->prophesize('Drupal\Core\Extension\ModuleHandlerInterface'); @@ -99,11 +102,9 @@ class CronTest extends UnitTestCase { $queue_worker_manager = $this->prophesize('Drupal\Core\Queue\QueueWorkerManagerInterface'); $state = $this->prophesize('Drupal\Core\State\StateInterface'); $account_switcher = $this->prophesize('Drupal\Core\Session\AccountSwitcherInterface'); - - // Create a lock that will always fail when attempting to acquire; we're - // only interested in testing ::processQueues(), not the other stuff. $lock_backend = $this->prophesize('Drupal\Core\Lock\LockBackendInterface'); - $lock_backend->acquire(Argument::exact('cron'), Argument::cetera())->willReturn(FALSE); + $lock_backend->acquire('cron', Argument::cetera())->willReturn(TRUE); + $lock_backend->release('cron')->shouldBeCalled(); // Create a queue worker definition for testing purposes. $queue_worker = $this->randomMachineName();