Skip to content

Commit

Permalink
ProcessJobExecutor: start and end times passed from job process to ma…
Browse files Browse the repository at this point in the history
…in process are not shifted by offset of current timezone from UTC
  • Loading branch information
mabar committed Jul 1, 2024
1 parent a83ae12 commit db214fb
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 28 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased](https://github.com/orisai/scheduler/compare/2.1.1...v2.x)

### Fixed

- `ProcessJobExecutor`
- start and end times passed from job process to main process are not shifted by offset of current timezone from UTC

## [2.1.1](https://github.com/orisai/scheduler/compare/2.1.0...2.1.1) - 2024-06-21

### Added
Expand Down
7 changes: 5 additions & 2 deletions src/Executor/ProcessJobExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use DateTimeImmutable;
use DateTimeZone;
use Generator;
use JsonException;
use Orisai\Clock\Adapter\ClockAdapterFactory;
Expand Down Expand Up @@ -180,13 +181,15 @@ private function createSummary(array $raw, JobSchedule $jobSchedule): JobSummary
$raw['info']['expression'],
$raw['info']['repeatAfterSeconds'],
$raw['info']['runSecond'],
DateTimeImmutable::createFromFormat('U.u e', $raw['info']['start']),
DateTimeImmutable::createFromFormat('U.u', $raw['info']['start'][0])
->setTimezone(new DateTimeZone($raw['info']['start'][1])),
$jobSchedule->getTimeZone(),
$raw['info']['forcedRun'],
),
new JobResult(
$jobSchedule->getExpression(),
DateTimeImmutable::createFromFormat('U.u e', $raw['result']['end']),
DateTimeImmutable::createFromFormat('U.u', $raw['result']['end'][0])
->setTimezone(new DateTimeZone($raw['result']['end'][1])),
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 @@ -131,7 +131,7 @@ public function toArray(): array
'expression' => $this->getExpression(),
'repeatAfterSeconds' => $this->getRepeatAfterSeconds(),
'runSecond' => $this->getRunSecond(),
'start' => $this->getStart()->format('U.u e'),
'start' => [$this->start->format('U.u'), $this->start->getTimezone()->getName()],
'forcedRun' => $this->forcedRun,
];
}
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 e'),
'end' => [$this->end->format('U.u'), $this->end->getTimezone()->getName()],
'state' => $this->getState()->value,
];
}
Expand Down
40 changes: 32 additions & 8 deletions tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,17 @@ public function testSuccess(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000 Europe\/Prague",
"start": [
"1.000000",
"Europe\/Prague"
],
"forcedRun": false
},
"result": {
"end": "1.000000 Europe\/Prague",
"end": [
"1.000000",
"Europe\/Prague"
],
"state": "done"
}
},
Expand All @@ -121,11 +127,17 @@ public function testSuccess(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000 UTC",
"start": [
"1.000000",
"UTC"
],
"forcedRun": false
},
"result": {
"end": "1.000000 UTC",
"end": [
"1.000000",
"UTC"
],
"state": "done"
}
}
Expand Down Expand Up @@ -184,11 +196,17 @@ public function testFailure(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000 Europe\/Prague",
"start": [
"1.000000",
"Europe\/Prague"
],
"forcedRun": false
},
"result": {
"end": "1.000000 Europe\/Prague",
"end": [
"1.000000",
"Europe\/Prague"
],
"state": "done"
}
},
Expand All @@ -199,11 +217,17 @@ public function testFailure(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000 Europe\/Prague",
"start": [
"1.000000",
"Europe\/Prague"
],
"forcedRun": false
},
"result": {
"end": "1.000000 Europe\/Prague",
"end": [
"1.000000",
"Europe\/Prague"
],
"state": "fail"
}
}
Expand Down
20 changes: 16 additions & 4 deletions tests/Unit/Command/RunJobCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,17 @@ public function testJson(): void
"expression": "1 * * * *",
"repeatAfterSeconds": 0,
"runSecond": 30,
"start": "61.000000 Europe\/Prague",
"start": [
"61.000000",
"Europe\/Prague"
],
"forcedRun": false
},
"result": {
"end": "61.000000 Europe\/Prague",
"end": [
"61.000000",
"Europe\/Prague"
],
"state": "done"
},
"stdout": ""
Expand Down Expand Up @@ -312,11 +318,17 @@ public function testEchoingJob(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000 Europe\/Prague",
"start": [
"1.000000",
"Europe\/Prague"
],
"forcedRun": true
},
"result": {
"end": "1.000000 Europe\/Prague",
"end": [
"1.000000",
"Europe\/Prague"
],
"state": "done"
},
"stdout": "output"
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Status/JobInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function test(
'expression' => $expression,
'repeatAfterSeconds' => $repeatAfterSeconds,
'runSecond' => $runSecond,
'start' => $start->format('U.u e'),
'start' => [$start->format('U.u'), $start->getTimezone()->getName()],
'forcedRun' => $forcedRun,
],
$info->toArray(),
Expand Down Expand Up @@ -87,7 +87,7 @@ public function provide(): Generator
'* * 6 9 *',
0,
2,
new DateTimeImmutable(),
(new DateTimeImmutable())->setTimezone(new DateTimeZone('Europe/Prague')),
new DateTimeZone('Europe/Prague'),
'* * 6 9 * (Europe/Prague)',
false,
Expand All @@ -99,7 +99,7 @@ public function provide(): Generator
'* 1 9 8 4',
30,
2,
new DateTimeImmutable(),
(new DateTimeImmutable())->setTimezone(new DateTimeZone('UTC')),
new DateTimeZone('UTC'),
'* 1 9 8 4 / 30 (UTC)',
false,
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 e'),
'end' => [$end->format('U.u'), $end->getTimezone()->getName()],
'state' => JobResultState::done()->value,
],
$result->toArray(),
Expand Down
11 changes: 3 additions & 8 deletions tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,12 @@ parameters:
path: ../src/Executor/ProcessJobExecutor.php

-
message: "#^Instanceof between Symfony\\\\Component\\\\Process\\\\Process and Symfony\\\\Component\\\\Process\\\\Process will always evaluate to true\\.$#"
count: 1
path: ../src/Executor/ProcessJobExecutor.php

-
message: "#^Parameter \\#2 \\$end of class Orisai\\\\Scheduler\\\\Status\\\\JobResult constructor expects DateTimeImmutable, DateTimeImmutable\\|false given\\.$#"
count: 1
message: "#^Cannot call method setTimezone\\(\\) on DateTimeImmutable\\|false\\.$#"
count: 2
path: ../src/Executor/ProcessJobExecutor.php

-
message: "#^Parameter \\#6 \\$start of class Orisai\\\\Scheduler\\\\Status\\\\JobInfo constructor expects DateTimeImmutable, DateTimeImmutable\\|false given\\.$#"
message: "#^Instanceof between Symfony\\\\Component\\\\Process\\\\Process and Symfony\\\\Component\\\\Process\\\\Process will always evaluate to true\\.$#"
count: 1
path: ../src/Executor/ProcessJobExecutor.php

Expand Down

0 comments on commit db214fb

Please sign in to comment.