From fcb563cdd4f9d0f1d951fb355d911adcaba639c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Poirier=20Th=C3=A9or=C3=AAt?= Date: Mon, 22 Apr 2024 13:54:47 -0400 Subject: [PATCH] [CronJob] fix executed when forced --- packages/cron-job/Entity/CronJobExecution.php | 4 +++- .../cron-job/Tests/Entity/CronJobExecutionTest.php | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/cron-job/Entity/CronJobExecution.php b/packages/cron-job/Entity/CronJobExecution.php index 586e2619..f07c85a4 100644 --- a/packages/cron-job/Entity/CronJobExecution.php +++ b/packages/cron-job/Entity/CronJobExecution.php @@ -146,7 +146,9 @@ public function getCronJob(): ?CronJob public function isExecutable(\DateTimeImmutable $dateTime): bool { - if (!($cronJob = $this->getCronJob())?->isActive()) { + $cronJob = $this->getCronJob(); + + if (!$this->isForce() && !$cronJob?->isActive()) { return false; } diff --git a/packages/cron-job/Tests/Entity/CronJobExecutionTest.php b/packages/cron-job/Tests/Entity/CronJobExecutionTest.php index a3169b96..f3b65bcd 100644 --- a/packages/cron-job/Tests/Entity/CronJobExecutionTest.php +++ b/packages/cron-job/Tests/Entity/CronJobExecutionTest.php @@ -20,7 +20,8 @@ public function testIsExecutable( bool $active, int $timeToLive = 0, ?\DateTimeImmutable $requestedAt = null, - \DateTimeImmutable $now = new \DateTimeImmutable() + \DateTimeImmutable $now = new \DateTimeImmutable(), + bool $forced = false ): void { Carbon::setTestNow($now); @@ -29,7 +30,7 @@ public function testIsExecutable( ->setActive($active) ->setTimeToLive($timeToLive), $requestedAt, - false + $forced ); static::assertSame($expectedExecutable, $execution->isExecutable(Carbon::now()->toDateTimeImmutable())); @@ -44,6 +45,15 @@ public static function provideDataForTestIsExecutable(): iterable '$requestedAt' => new \DateTimeImmutable('2024-04-17 00:00:00'), ]; + yield 'inactive-forced' => [ + '$expectedExecutable' => true, + '$active' => false, + '$timeToLive' => 0, + '$requestedAt' => new \DateTimeImmutable('2024-04-17 00:00:00'), + '$now' => new \DateTimeImmutable('2024-04-17 01:00:00'), + '$forced' => true, + ]; + yield 'active with no time to live' => [ '$expectedExecutable' => true, '$active' => true,