Skip to content

Commit

Permalink
add cron expression and next due to info
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Dec 12, 2023
1 parent ed03a5e commit 8cd9e69
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/SymfonyConsoleEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down
17 changes: 10 additions & 7 deletions tests/Unit/SymfonyConsoleEventListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']],
};
}));

Expand Down

0 comments on commit 8cd9e69

Please sign in to comment.