From 8cd9e690fd4e4db23280d726c1bf06919013001b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Demonchaux?= Date: Tue, 12 Dec 2023 14:55:29 +0100 Subject: [PATCH] add cron expression and next due to info --- src/SymfonyConsoleEventListener.php | 11 +++++++++-- tests/Unit/SymfonyConsoleEventListenerTest.php | 17 ++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/SymfonyConsoleEventListener.php b/src/SymfonyConsoleEventListener.php index 4a8465c..fb99276 100644 --- a/src/SymfonyConsoleEventListener.php +++ b/src/SymfonyConsoleEventListener.php @@ -4,6 +4,7 @@ namespace JobRunner\JobRunner\SymfonyConsole; +use Cron\CronExpression; use JobRunner\JobRunner\Event\JobEvent; use JobRunner\JobRunner\Event\JobFailEvent; use JobRunner\JobRunner\Event\JobIsLockedEvent; @@ -25,7 +26,7 @@ public function __construct( private readonly ConsoleSectionOutput $tableSection, private readonly Table $table, ) { - $this->table->setHeaders(['Job name', 'state', 'output']); + $this->table->setHeaders(['Job name', 'cron expression', 'next run date', 'state', 'output']); } public function start(Job $job): void @@ -55,7 +56,13 @@ public function isLocked(Job $job): void private function doIt(Job $job, string $state, string|null $output = null): void { - $this->rows[$job->getName()] = [$job->getName(), $state, $output]; + $this->rows[$job->getName()] = [ + $job->getName(), + $job->getCronExpression(), + (new CronExpression($job->getCronExpression()))->getNextRunDate()->format('Y-m-d H:i:s'), + $state, + $output, + ]; $this->tableSection->clear(); $this->table->setRows(array_values($this->rows)); $this->table->render(); diff --git a/tests/Unit/SymfonyConsoleEventListenerTest.php b/tests/Unit/SymfonyConsoleEventListenerTest.php index c14d406..e5a1784 100644 --- a/tests/Unit/SymfonyConsoleEventListenerTest.php +++ b/tests/Unit/SymfonyConsoleEventListenerTest.php @@ -4,6 +4,7 @@ namespace JobRunner\JobRunner\SymfonyConsole\Tests\Unit; +use DateTimeImmutable; use JobRunner\JobRunner\Job\Job; use JobRunner\JobRunner\SymfonyConsole\SymfonyConsoleEventListener; use PHPUnit\Framework\TestCase; @@ -34,18 +35,20 @@ public function testSuccess(): void $table = self::createMock(Table::class); $consoleSectionOutput = self::createMock(ConsoleSectionOutput::class); $job = self::createMock(Job::class); + $nextHour = (new DateTimeImmutable())->setTime((int) (new DateTimeImmutable())->modify('+1 hour')->format('H'), 0, 0); $table->expects($this->exactly(5))->method('render'); $job->expects($this->any())->method('getName')->willReturn('myName'); + $job->expects($this->any())->method('getCronExpression')->willReturn('0 * * * *'); $consoleSectionOutput->expects($this->exactly(5))->method('clear'); - $table->expects($this->once())->method('setHeaders')->with(['Job name', 'state', 'output']); - $table->expects($this->exactly(5))->method('setRows')->with($this->callback(function (mixed $param) { + $table->expects($this->once())->method('setHeaders')->with(['Job name', 'cron expression', 'next run date', 'state', 'output']); + $table->expects($this->exactly(5))->method('setRows')->with($this->callback(function (mixed $param) use ($nextHour) { return $param === match ($this->getNextIncrement('setRows')) { - 1 => [['myName', 'start', null]], - 2 => [['myName', 'fail', 'toto']], - 3 => [['myName', 'notDue', null]], - 4 => [['myName', 'isLocked', null]], - 5 => [['myName', 'success', 'toto']], + 1 => [['myName', '0 * * * *', $nextHour->format('Y-m-d H:i:s'), 'start', null]], + 2 => [['myName', '0 * * * *', $nextHour->format('Y-m-d H:i:s'), 'fail', 'toto']], + 3 => [['myName', '0 * * * *', $nextHour->format('Y-m-d H:i:s'), 'notDue', null]], + 4 => [['myName', '0 * * * *', $nextHour->format('Y-m-d H:i:s'), 'isLocked', null]], + 5 => [['myName', '0 * * * *', $nextHour->format('Y-m-d H:i:s'), 'success', 'toto']], }; }));