Skip to content

Commit

Permalink
JobInfo: getStart(), JobResult: getEnd() includes correct timezone (i…
Browse files Browse the repository at this point in the history
…n main process, when using ProcessJobExecutor)
  • Loading branch information
mabar committed Mar 22, 2024
1 parent 5a1e8b9 commit f9934e9
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 32 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- `ListCommand`
- on invalid input option - writes error to output instead of throwing exception
- `JobInfo`
- `getStart()` includes correct timezone (in main process, when using `ProcessJobExecutor`)
- `JobResult`
- `getEnd()` includes correct timezone (in main process, when using `ProcessJobExecutor`)

## [2.0.0](https://github.com/orisai/scheduler/compare/1.0.0...2.0.0) - 2024-01-26

Expand Down
12 changes: 11 additions & 1 deletion src/Command/BaseRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Orisai\Scheduler\Status\JobResultState;
use Orisai\Scheduler\Status\JobSummary;
use Psr\Clock\ClockInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Terminal;
Expand All @@ -19,6 +20,14 @@
abstract class BaseRunCommand extends Command
{

private ClockInterface $clock;

public function __construct(ClockInterface $clock)
{
parent::__construct();
$this->clock = $clock;
}

protected function getTerminalWidth(): int
{
return (new Terminal())->getWidth();
Expand All @@ -29,7 +38,8 @@ protected function renderJob(JobSummary $summary, int $terminalWidth, OutputInte
$info = $summary->getInfo();
$result = $summary->getResult();

$runStart = $info->getStart()->format('Y-m-d H:i:s');
$timezone = $this->clock->now()->getTimezone();
$runStart = $info->getStart()->setTimezone($timezone)->format('Y-m-d H:i:s');
$running = ' Running ';
$id = $info->getId();
$name = $info->getName();
Expand Down
6 changes: 4 additions & 2 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Orisai\Scheduler\Command;

use Generator;
use Orisai\Clock\SystemClock;
use Orisai\Scheduler\Scheduler;
use Orisai\Scheduler\Status\JobResultState;
use Orisai\Scheduler\Status\JobSummary;
use Orisai\Scheduler\Status\RunSummary;
use Psr\Clock\ClockInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -19,9 +21,9 @@ final class RunCommand extends BaseRunCommand

private Scheduler $scheduler;

public function __construct(Scheduler $scheduler)
public function __construct(Scheduler $scheduler, ?ClockInterface $clock = null)
{
parent::__construct();
parent::__construct($clock ?? new SystemClock());
$this->scheduler = $scheduler;
}

Expand Down
6 changes: 4 additions & 2 deletions src/Command/RunJobCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Orisai\Scheduler\Command;

use Orisai\Clock\SystemClock;
use Orisai\Scheduler\Scheduler;
use Orisai\Scheduler\Status\JobResultState;
use Orisai\Scheduler\Status\JobSummary;
use Orisai\Scheduler\Status\RunParameters;
use Psr\Clock\ClockInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand All @@ -24,9 +26,9 @@ final class RunJobCommand extends BaseRunCommand

private Scheduler $scheduler;

public function __construct(Scheduler $scheduler)
public function __construct(Scheduler $scheduler, ?ClockInterface $clock = null)
{
parent::__construct();
parent::__construct($clock ?? new SystemClock());
$this->scheduler = $scheduler;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Executor/ProcessJobExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,11 @@ private function createSummary(array $raw, CronExpression $cronExpression): JobS
$raw['info']['expression'],
$raw['info']['repeatAfterSeconds'],
$raw['info']['runSecond'],
DateTimeImmutable::createFromFormat('U.u', $raw['info']['start']),
DateTimeImmutable::createFromFormat('U.u e', $raw['info']['start']),
),
new JobResult(
$cronExpression,
DateTimeImmutable::createFromFormat('U.u', $raw['result']['end']),
DateTimeImmutable::createFromFormat('U.u e', $raw['result']['end']),
JobResultState::from($raw['result']['state']),
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/Status/JobInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function toArray(): array
'expression' => $this->getExpression(),
'repeatAfterSeconds' => $this->getRepeatAfterSeconds(),
'runSecond' => $this->getRunSecond(),
'start' => $this->getStart()->format('U.u'),
'start' => $this->getStart()->format('U.u e'),
];
}

Expand Down
2 changes: 1 addition & 1 deletion src/Status/JobResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function getNextRunDates(int $total): array
public function toArray(): array
{
return [
'end' => $this->getEnd()->format('U.u'),
'end' => $this->getEnd()->format('U.u e'),
'state' => $this->getState()->value,
];
}
Expand Down
25 changes: 14 additions & 11 deletions tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,12 @@ public function testSuccess(): void
$scheduler->addJob(
new CallbackJob(Closure::fromCallable([$cbs, 'job2'])),
new CronExpression('* * * * *'),
null,
0,
new DateTimeZone('UTC'),
);

$command = new RunCommand($scheduler);
$command = new RunCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -103,10 +106,10 @@ public function testSuccess(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000"
"start": "1.000000 Europe\/Prague"
},
"result": {
"end": "1.000000",
"end": "1.000000 Europe\/Prague",
"state": "done"
}
},
Expand All @@ -117,10 +120,10 @@ public function testSuccess(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000"
"start": "1.000000 UTC"
},
"result": {
"end": "1.000000",
"end": "1.000000 UTC",
"state": "done"
}
}
Expand Down Expand Up @@ -150,7 +153,7 @@ public function testFailure(): void
new CronExpression('* * * * *'),
);

$command = new RunCommand($scheduler);
$command = new RunCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -179,10 +182,10 @@ public function testFailure(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000"
"start": "1.000000 Europe\/Prague"
},
"result": {
"end": "1.000000",
"end": "1.000000 Europe\/Prague",
"state": "done"
}
},
Expand All @@ -193,10 +196,10 @@ public function testFailure(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000"
"start": "1.000000 Europe\/Prague"
},
"result": {
"end": "1.000000",
"end": "1.000000 Europe\/Prague",
"state": "fail"
}
}
Expand Down Expand Up @@ -226,7 +229,7 @@ public function testLock(): void
$lock = $lockFactory->createLock('Orisai.Scheduler.Job/0');
$lock->acquire();

$command = new RunCommand($scheduler);
$command = new RunCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down
20 changes: 10 additions & 10 deletions tests/Unit/Command/RunJobCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testSuccess(): void
new CronExpression('* * * * *'),
);

$command = new RunJobCommand($scheduler);
$command = new RunJobCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -107,7 +107,7 @@ public function testFailure(): void
new CronExpression('* * * * *'),
);

$command = new RunJobCommand($scheduler);
$command = new RunJobCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -143,7 +143,7 @@ public function testLock(): void
$lock = $lockFactory->createLock('Orisai.Scheduler.Job/0');
$lock->acquire();

$command = new RunJobCommand($scheduler);
$command = new RunJobCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -172,7 +172,7 @@ public function testNoForce(): void
new CronExpression('0 * * * *'),
);

$command = new RunJobCommand($scheduler);
$command = new RunJobCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -217,7 +217,7 @@ public function testJson(): void
new CronExpression('1 * * * *'),
);

$command = new RunJobCommand($scheduler);
$command = new RunJobCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -252,10 +252,10 @@ public function testJson(): void
"expression": "1 * * * *",
"repeatAfterSeconds": 0,
"runSecond": 30,
"start": "61.000000"
"start": "61.000000 Europe\/Prague"
},
"result": {
"end": "61.000000",
"end": "61.000000 Europe\/Prague",
"state": "done"
},
"stdout": ""
Expand All @@ -278,7 +278,7 @@ public function testEchoingJob(): void
new CronExpression('* * * * *'),
);

$command = new RunJobCommand($scheduler);
$command = new RunJobCommand($scheduler, $clock);
$tester = new CommandTester($command);

putenv('COLUMNS=80');
Expand Down Expand Up @@ -311,10 +311,10 @@ public function testEchoingJob(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000"
"start": "1.000000 Europe\/Prague"
},
"result": {
"end": "1.000000",
"end": "1.000000 Europe\/Prague",
"state": "done"
},
"stdout": "output"
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Status/JobInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function test(
'expression' => $expression,
'repeatAfterSeconds' => $repeatAfterSeconds,
'runSecond' => $runSecond,
'start' => $start->format('U.u'),
'start' => $start->format('U.u e'),
],
$info->toArray(),
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Status/JobResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function test(): void

self::assertSame(
[
'end' => $end->format('U.u'),
'end' => $end->format('U.u e'),
'state' => JobResultState::done()->value,
],
$result->toArray(),
Expand Down

0 comments on commit f9934e9

Please sign in to comment.