Skip to content

Commit

Permalink
ManagedScheduler: sets timezone specified by job to job start and end…
Browse files Browse the repository at this point in the history
… times
  • Loading branch information
mabar committed Jan 13, 2024
1 parent 70665b9 commit a67ba35
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- returns array of `JobSchedule` instead of an array of arrays
- `RunSummary`
- `getJobs()` -> `getJobSummaries()`
- `JobInfo`
- `getStart()->getTimeZone()` - returns timezone specified by the job
- `JobResult`
- `getEnd()->getTimeZone()` - returns timezone specified by the job
- `JobExecutor`
- `runJobs()` accepts list of `JobSchedule` grouped by seconds instead of list of ids
- `runJobs()` returns `Generator<int, JobSummary, void, RunSummary>` instead of `RunSummary`
Expand Down
15 changes: 13 additions & 2 deletions src/ManagedScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Orisai\Scheduler;

use Closure;
use DateTimeImmutable;
use Generator;
use Orisai\Clock\Adapter\ClockAdapterFactory;
use Orisai\Clock\Clock;
Expand Down Expand Up @@ -208,7 +209,7 @@ private function runInternal($id, JobSchedule $jobSchedule, int $runSecond): arr
$expression->getExpression(),
$jobSchedule->getRepeatAfterSeconds(),
$runSecond,
$this->clock->now(),
$this->getCurrentTime($jobSchedule),
);

$lock = $this->lockFactory->createLock("Orisai.Scheduler.Job/$id");
Expand Down Expand Up @@ -240,7 +241,7 @@ private function runInternal($id, JobSchedule $jobSchedule, int $runSecond): arr

$result = new JobResult(
$expression,
$this->clock->now(),
$this->getCurrentTime($jobSchedule),
$throwable === null ? JobResultState::done() : JobResultState::fail(),
);

Expand All @@ -262,6 +263,16 @@ private function runInternal($id, JobSchedule $jobSchedule, int $runSecond): arr
];
}

private function getCurrentTime(JobSchedule $schedule): DateTimeImmutable
{
$now = $this->clock->now();
$timezone = $schedule->getTimeZone();

return $timezone !== null
? $now->setTimezone($timezone)
: $now;
}

/**
* @param Closure(JobInfo, JobResult): void $callback
*/
Expand Down
29 changes: 28 additions & 1 deletion tests/Unit/SimpleSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Cron\CronExpression;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use ErrorException;
use Exception;
Expand Down Expand Up @@ -577,7 +578,7 @@ public function provideJobSummary(): Generator
yield [new RunParameters(30), 30];
}

public function testTimeZone(): void
public function testTimeZoneExecution(): void
{
$defaultTz = new DateTimeZone('UTC');
$otherTz = new DateTimeZone('Europe/Prague');
Expand Down Expand Up @@ -624,6 +625,32 @@ static function () use (&$i2): void {
self::assertSame(2, $i2);
}

public function testTimeZoneInfo(): void
{
$defaultTz = new DateTimeZone('UTC');
$otherTz = new DateTimeZone('Europe/Prague');
$clock = new FrozenClock(0, $defaultTz);

$scheduler = new SimpleScheduler(null, null, null, $clock);
$expression = new CronExpression('* * * * *');

$job = new CallbackJob(static function (): void {
// Noop
});
$scheduler->addJob($job, $expression, null, 0, $otherTz);

$summaries = $scheduler->run()->getJobSummaries();
$summary = $summaries[0];

$start = $summary->getInfo()->getStart();
self::assertEquals($otherTz, $start->getTimezone());
self::assertSame('1970-01-01T01:00:00+01:00', $start->format(DateTimeInterface::ATOM));

$end = $summary->getResult()->getEnd();
self::assertEquals($otherTz, $end->getTimezone());
self::assertSame('1970-01-01T01:00:00+01:00', $end->format(DateTimeInterface::ATOM));
}

public function testLockAlreadyAcquired(): void
{
$lockFactory = new TestLockFactory(new InMemoryStore(), false);
Expand Down

0 comments on commit a67ba35

Please sign in to comment.