diff --git a/src/Cron/CronManager.php b/src/Cron/CronManager.php index de6a24a1209a43645d1a583a73d5ba923f2c784f..ea221a644ccbc8052c119313be325bfe655809b8 100644 --- a/src/Cron/CronManager.php +++ b/src/Cron/CronManager.php @@ -53,9 +53,13 @@ final class CronManager implements LoggerAwareInterface $previousExpectedRun->setTimezone(new \DateTimeZone('UTC')); $shouldRun = false; - // If we were scheduled to run between now and the previous run (or just before of no previous run exists) - // then we should run - if ($previousExpectedRun <= $currentRun && ($previousRun === null || $previousExpectedRun > $previousRun)) { + if ($previousRun === null) { + // In case there is no previous run we just skip the cron job + // This can happen on re-deployments, and we don't want a cron-storm there, or jobs that run + // way off their schedule + $shouldRun = false; + } elseif ($previousExpectedRun > $previousRun && $previousExpectedRun <= $currentRun) { + // If we were scheduled to run between now and right the previous run then we should run $shouldRun = true; } diff --git a/tests/Cron/CronTest.php b/tests/Cron/CronTest.php index 10fd4973fe6fe8d1df0abdba32b31994ec410c44..7c951cf71a66ef07a2c63546b1ec5a0310e65256 100644 --- a/tests/Cron/CronTest.php +++ b/tests/Cron/CronTest.php @@ -16,8 +16,8 @@ class CronTest extends TestCase $isDue = CronManager::isDue(new \DateTimeImmutable('2021-09-07T09:35:59Z'), new \DateTimeImmutable('2021-09-07T09:36:00Z'), '* * * * *'); $this->assertTrue($isDue); $isDue = CronManager::isDue(null, new \DateTimeImmutable('2021-09-07T09:36:00Z'), '0 0 1 1 *'); - $this->assertTrue($isDue); + $this->assertFalse($isDue); $isDue = CronManager::isDue(null, new \DateTimeImmutable('2021-09-07T09:36:00Z'), '* * * * *'); - $this->assertTrue($isDue); + $this->assertFalse($isDue); } }