From bbd2599e7e6040c7090bfb11bc2f913b5a73d5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 27 Dec 2022 00:09:45 +0100 Subject: [PATCH] Fixed: [cron] store the last store of executed tasks correctly --- .../ChillMainBundle/Cron/CronManager.php | 5 ++-- ...aphicalUnitMaterializedViewCronJobTest.php | 24 ++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Cron/CronManager.php b/src/Bundle/ChillMainBundle/Cron/CronManager.php index 52cba3d38..f69dcba76 100644 --- a/src/Bundle/ChillMainBundle/Cron/CronManager.php +++ b/src/Bundle/ChillMainBundle/Cron/CronManager.php @@ -44,7 +44,7 @@ class CronManager implements CronManagerInterface private const UPDATE_AFTER_EXEC = 'UPDATE ' . CronJobExecution::class . ' cr SET cr.lastEnd = :now, cr.lastStatus = :status WHERE cr.key = :key'; - private const UPDATE_BEFORE_EXEC = 'UPDATE ' . CronJobExecution::class . ' cr SET cr.lastExecution = :now WHERE cr.key = :key'; + private const UPDATE_BEFORE_EXEC = 'UPDATE ' . CronJobExecution::class . ' cr SET cr.lastStart = :now WHERE cr.key = :key'; private CronJobExecutionRepositoryInterface $cronJobExecutionRepository; @@ -90,7 +90,8 @@ class CronManager implements CronManagerInterface ->setParameters([ 'now' => new DateTimeImmutable('now'), 'key' => $job->getKey(), - ]); + ]) + ->execute(); } else { $execution = new CronJobExecution($job->getKey()); $this->entityManager->persist($execution); diff --git a/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php b/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php index 1125c260a..687d5df2b 100644 --- a/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php +++ b/src/Bundle/ChillMainBundle/Tests/Services/AddressGeographicalUnit/RefreshAddressToGeographicalUnitMaterializedViewCronJobTest.php @@ -44,7 +44,7 @@ final class RefreshAddressToGeographicalUnitMaterializedViewCronJobTest extends $executedForFirstTime = 0; $executedAfterPreviousExecution = 0; - for ($round = 0; 20 > $round; ++$round ) { + for ($round = 0; 20 > $round; ++$round) { if ($job->canRun(null)) { ++$executedForFirstTime; } @@ -58,6 +58,28 @@ final class RefreshAddressToGeographicalUnitMaterializedViewCronJobTest extends $this->assertGreaterThan(0, $executedAfterPreviousExecution); } + public function testCanRunShouldReturnFalse(): void + { + // As the can run is executed one of ten, this should be executed at least one after + // 10 + 5 executions + $job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob( + $this->connection + ); + + $lastExecution = new CronJobExecution($job->getKey()); + $lastExecution->setLastStart(new DateTimeImmutable('2 hours ago')); + + $executedAfterPreviousExecution = 0; + + for ($round = 0; 20 > $round; ++$round) { + if ($job->canRun($lastExecution)) { + ++$executedAfterPreviousExecution; + } + } + + $this->assertEquals(0, $executedAfterPreviousExecution); + } + public function testFullRun(): void { $job = new \Chill\MainBundle\Service\AddressGeographicalUnit\RefreshAddressToGeographicalUnitMaterializedViewCronJob(