Skip to content

Commit

Permalink
JobInfo: getTimeZone(), getExtendedExpression() contains timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 22, 2024
1 parent f9934e9 commit 35f6fa0
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `ListCommand`
- adds `--explain` option to explain whole expression
- `SymfonyCommandJob`
- `JobInfo`
- `getTimeZone()` returns timezone job should run in

### Changed

Expand All @@ -25,6 +27,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- logs unexpected stderr instead of throwing an exception (via optional logger)
- `JobInfo`
- `getExtendedExpression()` includes seconds only if seconds are used
- `getExtendedExpression()` includes timezone if timezone is used

### Fixed

Expand Down
6 changes: 4 additions & 2 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,8 @@ $scheduler->addBeforeRunCallback(
$jobInfo->getId(); // int|string
$jobInfo->getName(); // string
$jobInfo->getExpression(); // string, e.g. * * * * *
$jobInfo->getExtendedExpression(); // string, e.g. * * * * * / 30
$jobInfo->getTimeZone(); // DateTimeZone|null
$jobInfo->getExtendedExpression(); // string, e.g. '* * * * * / 30 [Europe/Prague]'
$jobInfo->getRepeatAfterSeconds(); // int<0, 30>
$jobInfo->getRunsCountPerMinute(); // int<1, max>
$jobInfo->getEstimatedStartTimes(); // list<DateTimeImmutable>
Expand Down Expand Up @@ -596,7 +597,8 @@ $id = $info->getId(); // string|int
$name = $info->getName(); // string
$expression = $info->getExpression(); // string, e.g. '* * * * *'
$repeatAfterSeconds = $info->getRepeatAfterSeconds(); // int<0, 30>
$extendedExpression = $info->getExtendedExpression(); // string, e.g. '* * * * * / 30'
$timeZone = $info->getTimeZone(); // DateTimeZone|null
$extendedExpression = $info->getExtendedExpression(); // string, e.g. '* * * * * / 30 [Europe/Prague]'
$runSecond = $info->getRunSecond(); // int
$start = $info->getStart(); // DateTimeImmutable
```
Expand Down
8 changes: 4 additions & 4 deletions src/Executor/ProcessJobExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Orisai\Scheduler\Executor;

use Closure;
use Cron\CronExpression;
use DateTimeImmutable;
use Generator;
use JsonException;
Expand Down Expand Up @@ -126,7 +125,7 @@ public function runJobs(
$this->logUnexpectedStderr($execution, $jobId, $stderr);
}

yield $jobSummaries[] = $this->createSummary($decoded, $jobSchedule->getExpression());
yield $jobSummaries[] = $this->createSummary($decoded, $jobSchedule);
}

// Nothing to do, wait
Expand Down Expand Up @@ -172,7 +171,7 @@ private function startJobs(array $jobSchedules, array $jobExecutions, RunParamet
/**
* @param array<mixed> $raw
*/
private function createSummary(array $raw, CronExpression $cronExpression): JobSummary
private function createSummary(array $raw, JobSchedule $jobSchedule): JobSummary
{
return new JobSummary(
new JobInfo(
Expand All @@ -182,9 +181,10 @@ private function createSummary(array $raw, CronExpression $cronExpression): JobS
$raw['info']['repeatAfterSeconds'],
$raw['info']['runSecond'],
DateTimeImmutable::createFromFormat('U.u e', $raw['info']['start']),
$jobSchedule->getTimeZone(),
),
new JobResult(
$cronExpression,
$jobSchedule->getExpression(),
DateTimeImmutable::createFromFormat('U.u e', $raw['result']['end']),
JobResultState::from($raw['result']['state']),
),
Expand Down
1 change: 1 addition & 0 deletions src/ManagedScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ private function runInternal($id, JobSchedule $jobSchedule, int $runSecond): arr
$jobSchedule->getRepeatAfterSeconds(),
$runSecond,
$this->getCurrentTime($jobSchedule),
$jobSchedule->getTimeZone(),
);

$lock = $this->lockFactory->createLock("Orisai.Scheduler.Job/$id");
Expand Down
16 changes: 15 additions & 1 deletion src/Status/JobInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Orisai\Scheduler\Status;

use DateTimeImmutable;
use DateTimeZone;

final class JobInfo
{
Expand All @@ -22,6 +23,8 @@ final class JobInfo

private DateTimeImmutable $start;

private ?DateTimeZone $timeZone;

/**
* @param string|int $id
* @param int<0, 30> $repeatAfterSeconds
Expand All @@ -33,7 +36,8 @@ public function __construct(
string $expression,
int $repeatAfterSeconds,
int $runSecond,
DateTimeImmutable $start
DateTimeImmutable $start,
?DateTimeZone $timeZone
)
{
$this->id = $id;
Expand All @@ -42,6 +46,7 @@ public function __construct(
$this->repeatAfterSeconds = $repeatAfterSeconds;
$this->runSecond = $runSecond;
$this->start = $start;
$this->timeZone = $timeZone;
}

/**
Expand Down Expand Up @@ -81,6 +86,10 @@ public function getExtendedExpression(): string
$expression .= " / $this->repeatAfterSeconds";
}

if ($this->timeZone !== null) {
$expression .= " [{$this->timeZone->getName()}]";
}

return $expression;
}

Expand All @@ -97,6 +106,11 @@ public function getStart(): DateTimeImmutable
return $this->start;
}

public function getTimeZone(): ?DateTimeZone
{
return $this->timeZone;
}

/**
* @return array<mixed>
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Exception/JobFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class JobFailureTest extends TestCase

public function test(): void
{
$info = new JobInfo('id', 'name', '* * * * *', 0, 0, new DateTimeImmutable());
$info = new JobInfo('id', 'name', '* * * * *', 0, 0, new DateTimeImmutable(), null);
$result = new JobResult(
new CronExpression('* * * * *'),
new DateTimeImmutable(),
Expand Down
37 changes: 30 additions & 7 deletions tests/Unit/SimpleSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,16 @@ public function testJobEvents(): void

self::assertEquals(
[
new JobInfo(0, 'Tests\Orisai\Scheduler\Doubles\CallbackList::exceptionJob()', '* * * * *', 0, 0, $now),
new JobInfo(1, 'Tests\Orisai\Scheduler\Doubles\CallbackList::job1()', '* * * * *', 0, 0, $now),
new JobInfo(
0,
'Tests\Orisai\Scheduler\Doubles\CallbackList::exceptionJob()',
'* * * * *',
0,
0,
$now,
null,
),
new JobInfo(1, 'Tests\Orisai\Scheduler\Doubles\CallbackList::job1()', '* * * * *', 0, 0, $now, null),
],
$beforeCollected,
);
Expand All @@ -266,11 +274,20 @@ public function testJobEvents(): void
0,
0,
$now,
null,
),
new JobResult(new CronExpression('* * * * *'), $now, JobResultState::fail()),
],
[
new JobInfo(1, 'Tests\Orisai\Scheduler\Doubles\CallbackList::job1()', '* * * * *', 0, 0, $now),
new JobInfo(
1,
'Tests\Orisai\Scheduler\Doubles\CallbackList::job1()',
'* * * * *',
0,
0,
$now,
null,
),
new JobResult(new CronExpression('* * * * *'), $now, JobResultState::done()),
],
],
Expand Down Expand Up @@ -319,6 +336,7 @@ static function () use ($clock): void {
0,
0,
DateTimeImmutable::createFromFormat('U', '1'),
null,
),
],
$beforeCollected,
Expand All @@ -333,6 +351,7 @@ static function () use ($clock): void {
0,
0,
DateTimeImmutable::createFromFormat('U', '1'),
null,
),
new JobResult(
new CronExpression('* * * * *'),
Expand Down Expand Up @@ -582,6 +601,7 @@ public function testRunSummary(): void
0,
0,
$before,
null,
),
new JobResult(new CronExpression('* * * * *'), $before, JobResultState::done()),
),
Expand All @@ -593,6 +613,7 @@ public function testRunSummary(): void
0,
0,
$before,
null,
),
new JobResult(new CronExpression('* * * * *'), $after, JobResultState::done()),
),
Expand Down Expand Up @@ -628,6 +649,7 @@ public function testJobSummary(?RunParameters $parameters, int $second): void
0,
$second,
$now,
null,
),
$summary->getInfo(),
);
Expand Down Expand Up @@ -755,11 +777,11 @@ static function () use (&$i2): void {
self::assertEquals(
[
new JobSummary(
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now()),
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now(), null),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::lock()),
),
new JobSummary(
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now()),
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now(), null),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::done()),
),
],
Expand Down Expand Up @@ -788,11 +810,11 @@ static function () use (&$i2): void {
self::assertEquals(
[
new JobSummary(
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now()),
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now(), null),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::done()),
),
new JobSummary(
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now()),
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now(), null),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::done()),
),
],
Expand Down Expand Up @@ -961,6 +983,7 @@ public function testLockedJobEvent(): void
0,
0,
$now,
null,
),
new JobResult(new CronExpression('* * * * *'), $now, JobResultState::lock()),
],
Expand Down
29 changes: 28 additions & 1 deletion tests/Unit/Status/JobInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Orisai\Scheduler\Unit\Status;

use DateTimeImmutable;
use DateTimeZone;
use Generator;
use Orisai\Scheduler\Status\JobInfo;
use PHPUnit\Framework\TestCase;
Expand All @@ -24,14 +25,16 @@ public function test(
int $repeatAfterSeconds,
int $runSecond,
DateTimeImmutable $start,
?DateTimeZone $timeZone,
string $extendedExpression
): void
{
$info = new JobInfo($id, $name, $expression, $repeatAfterSeconds, $runSecond, $start);
$info = new JobInfo($id, $name, $expression, $repeatAfterSeconds, $runSecond, $start, $timeZone);
self::assertSame($id, $info->getId());
self::assertSame($name, $info->getName());
self::assertSame($expression, $info->getExpression());
self::assertSame($repeatAfterSeconds, $info->getRepeatAfterSeconds());
self::assertSame($timeZone, $info->getTimeZone());
self::assertSame($extendedExpression, $info->getExtendedExpression());
self::assertSame($runSecond, $info->getRunSecond());
self::assertSame($start, $info->getStart());
Expand All @@ -58,6 +61,7 @@ public function provide(): Generator
0,
0,
new DateTimeImmutable(),
null,
'* * * * *',
];

Expand All @@ -68,8 +72,31 @@ public function provide(): Generator
10,
15,
new DateTimeImmutable(),
null,
'* * * * */5 / 10',
];

yield [
'dunno',
'still dunno',
'* * 6 9 *',
0,
2,
new DateTimeImmutable(),
new DateTimeZone('Europe/Prague'),
'* * 6 9 * [Europe/Prague]',
];

yield [
'whatever',
'whatever but pale blue',
'* 1 9 8 4',
30,
2,
new DateTimeImmutable(),
new DateTimeZone('UTC'),
'* 1 9 8 4 / 30 [UTC]',
];
}

}
2 changes: 1 addition & 1 deletion tests/Unit/Status/JobSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final class JobSummaryTest extends TestCase

public function test(): void
{
$info = new JobInfo('id', 'name', '* * * * *', 0, 0, new DateTimeImmutable());
$info = new JobInfo('id', 'name', '* * * * *', 0, 0, new DateTimeImmutable(), null);
$result = new JobResult(
new CronExpression('* * * * *'),
new DateTimeImmutable(),
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Status/RunSummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public function test(): void
$end = new DateTimeImmutable();
$jobSummaries = [
new JobSummary(
new JobInfo('id', '1', '* * * * *', 0, 0, new DateTimeImmutable()),
new JobInfo('id', '1', '* * * * *', 0, 0, new DateTimeImmutable(), null),
new JobResult(new CronExpression('* * * * *'), new DateTimeImmutable(), JobResultState::done()),
),
new JobSummary(
new JobInfo('id', '2', '1 * * * *', 5, 10, new DateTimeImmutable()),
new JobInfo('id', '2', '1 * * * *', 5, 10, new DateTimeImmutable(), null),
new JobResult(new CronExpression('1 * * * *'), new DateTimeImmutable(), JobResultState::done()),
),
];
Expand Down

0 comments on commit 35f6fa0

Please sign in to comment.