Skip to content

Commit

Permalink
JobInfo: isForcedRun()
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Apr 7, 2024
1 parent 93ef368 commit 5ea942a
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 33 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `SymfonyCommandJob`
- `JobInfo`
- `getTimeZone()` returns timezone job should run in
- `isForcedRun()`returns whether job was run via $scheduler->runJob() or scheduler:run-job command, ignoring the cron expression

### Changed

Expand Down
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ $errorHandler = function(Throwable $throwable, JobInfo $info, JobResult $result)
'runSecond' => $info->getRunSecond(),
'start' => $info->getStart()->format(DateTimeInterface::ATOM),
'end' => $result->getEnd()->format(DateTimeInterface::ATOM),
'forcedRun' => $info->isForcedRun(),
]);
},
$scheduler = new SimpleScheduler($errorHandler);
Expand Down Expand Up @@ -602,6 +603,7 @@ $timeZone = $info->getTimeZone(); // DateTimeZone|null
$extendedExpression = $info->getExtendedExpression(); // string, e.g. '* * * * * / 30 (Europe/Prague)'
$runSecond = $info->getRunSecond(); // int
$start = $info->getStart(); // DateTimeImmutable
$forcedRun = $info->isForcedRun(); // bool, happens when running job via $scheduler->runJob() or scheduler:run-job command, ignoring the cron expression
```

Result:
Expand Down
3 changes: 2 additions & 1 deletion src/Executor/ProcessJobExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function runJobs(
$jobExecutions = $this->startJobs(
$jobSchedulesBySecond[$currentSecond],
$jobExecutions,
new RunParameters($currentSecond),
new RunParameters($currentSecond, false),
);
unset($jobSchedulesBySecond[$currentSecond]);
}
Expand Down Expand Up @@ -182,6 +182,7 @@ private function createSummary(array $raw, JobSchedule $jobSchedule): JobSummary
$raw['info']['runSecond'],
DateTimeImmutable::createFromFormat('U.u e', $raw['info']['start']),
$jobSchedule->getTimeZone(),
$raw['info']['forcedRun'],
),
new JobResult(
$jobSchedule->getExpression(),
Expand Down
12 changes: 6 additions & 6 deletions src/ManagedScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function __construct(
fn ($id, JobSchedule $jobSchedule, int $runSecond): array => $this->runInternal(
$id,
$jobSchedule,
$runSecond,
new RunParameters($runSecond, false),
),
);
}
Expand All @@ -99,7 +99,7 @@ public function getJobSchedules(): array
public function runJob($id, bool $force = true, ?RunParameters $parameters = null): ?JobSummary
{
$jobSchedule = $this->jobManager->getJobSchedule($id);
$parameters ??= new RunParameters(0);
$parameters ??= new RunParameters(0, $force);

if ($jobSchedule === null) {
$message = Message::create()
Expand All @@ -126,7 +126,7 @@ public function runJob($id, bool $force = true, ?RunParameters $parameters = nul
return null;
}

[$summary, $throwable] = $this->runInternal($id, $jobSchedule, $parameters->getSecond());
[$summary, $throwable] = $this->runInternal($id, $jobSchedule, $parameters);

if ($throwable !== null) {
throw JobFailure::create($summary, $throwable);
Expand Down Expand Up @@ -238,10 +238,9 @@ public function run(): RunSummary

/**
* @param string|int $id
* @param int<0, max> $runSecond
* @return array{JobSummary, Throwable|null}
*/
private function runInternal($id, JobSchedule $jobSchedule, int $runSecond): array
private function runInternal($id, JobSchedule $jobSchedule, RunParameters $runParameters): array
{
$job = $jobSchedule->getJob();
$expression = $jobSchedule->getExpression();
Expand All @@ -251,9 +250,10 @@ private function runInternal($id, JobSchedule $jobSchedule, int $runSecond): arr
$job->getName(),
$expression->getExpression(),
$jobSchedule->getRepeatAfterSeconds(),
$runSecond,
$runParameters->getSecond(),
$this->getCurrentTime($jobSchedule),
$jobSchedule->getTimeZone(),
$runParameters->isForcedRun(),
);

$lock = $this->lockFactory->createLock("Orisai.Scheduler.Job/$id");
Expand Down
12 changes: 11 additions & 1 deletion src/Status/JobInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ final class JobInfo

private ?DateTimeZone $timeZone;

private bool $forcedRun;

/**
* @param string|int $id
* @param int<0, 30> $repeatAfterSeconds
Expand All @@ -37,7 +39,8 @@ public function __construct(
int $repeatAfterSeconds,
int $runSecond,
DateTimeImmutable $start,
?DateTimeZone $timeZone
?DateTimeZone $timeZone,
bool $forcedRun
)
{
$this->id = $id;
Expand All @@ -47,6 +50,7 @@ public function __construct(
$this->runSecond = $runSecond;
$this->start = $start;
$this->timeZone = $timeZone;
$this->forcedRun = $forcedRun;
}

/**
Expand Down Expand Up @@ -111,6 +115,11 @@ public function getTimeZone(): ?DateTimeZone
return $this->timeZone;
}

public function isForcedRun(): bool
{
return $this->forcedRun;
}

/**
* @return array<mixed>
*/
Expand All @@ -123,6 +132,7 @@ public function toArray(): array
'repeatAfterSeconds' => $this->getRepeatAfterSeconds(),
'runSecond' => $this->getRunSecond(),
'start' => $this->getStart()->format('U.u e'),
'forcedRun' => $this->forcedRun,
];
}

Expand Down
13 changes: 11 additions & 2 deletions src/Status/RunParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ final class RunParameters
/** @var int<0, max> */
private int $second;

private bool $forcedRun;

/**
* @param int<0, max> $second
*/
public function __construct(int $second)
public function __construct(int $second, bool $forcedRun)
{
$this->second = $second;
$this->forcedRun = $forcedRun;
}

/**
* @param array<mixed> $raw
*/
public static function fromArray(array $raw): self
{
return new self($raw['second']);
return new self($raw['second'], $raw['forcedRun']);
}

/**
Expand All @@ -35,13 +38,19 @@ public function getSecond(): int
return $this->second;
}

public function isForcedRun(): bool
{
return $this->forcedRun;
}

/**
* @return array<mixed>
*/
public function toArray(): array
{
return [
'second' => $this->second,
'forcedRun' => $this->forcedRun,
];
}

Expand Down
12 changes: 8 additions & 4 deletions tests/Unit/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ 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",
Expand All @@ -120,7 +121,8 @@ public function testSuccess(): void
"expression": "* * * * *",
"repeatAfterSeconds": 0,
"runSecond": 0,
"start": "1.000000 UTC"
"start": "1.000000 UTC",
"forcedRun": false
},
"result": {
"end": "1.000000 UTC",
Expand Down Expand Up @@ -182,7 +184,8 @@ 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",
Expand All @@ -196,7 +199,8 @@ 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",
Expand Down
8 changes: 5 additions & 3 deletions tests/Unit/Command/RunJobCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public function testJson(): void
$tester->execute([
'id' => 0,
'--json' => true,
'--parameters' => json_encode((new RunParameters(30))->toArray(), JSON_THROW_ON_ERROR),
'--parameters' => json_encode((new RunParameters(30, false))->toArray(), JSON_THROW_ON_ERROR),
]);

self::assertSame(
Expand All @@ -252,7 +252,8 @@ 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",
Expand Down Expand Up @@ -311,7 +312,8 @@ 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",
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(), null);
$info = new JobInfo('id', 'name', '* * * * *', 0, 0, new DateTimeImmutable(), null, false);
$result = new JobResult(
new CronExpression('* * * * *'),
new DateTimeImmutable(),
Expand Down
32 changes: 25 additions & 7 deletions tests/Unit/SimpleSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,18 @@ public function testJobEvents(): void
0,
$now,
null,
false,
),
new JobInfo(
1,
'Tests\Orisai\Scheduler\Doubles\CallbackList::job1()',
'* * * * *',
0,
0,
$now,
null,
false,
),
new JobInfo(1, 'Tests\Orisai\Scheduler\Doubles\CallbackList::job1()', '* * * * *', 0, 0, $now, null),
],
$beforeCollected,
);
Expand All @@ -275,6 +285,7 @@ public function testJobEvents(): void
0,
$now,
null,
false,
),
new JobResult(new CronExpression('* * * * *'), $now, JobResultState::fail()),
],
Expand All @@ -287,6 +298,7 @@ public function testJobEvents(): void
0,
$now,
null,
false,
),
new JobResult(new CronExpression('* * * * *'), $now, JobResultState::done()),
],
Expand Down Expand Up @@ -337,6 +349,7 @@ static function () use ($clock): void {
0,
DateTimeImmutable::createFromFormat('U', '1'),
null,
false,
),
],
$beforeCollected,
Expand All @@ -352,6 +365,7 @@ static function () use ($clock): void {
0,
DateTimeImmutable::createFromFormat('U', '1'),
null,
false,
),
new JobResult(
new CronExpression('* * * * *'),
Expand Down Expand Up @@ -602,6 +616,7 @@ public function testRunSummary(): void
0,
$before,
null,
false,
),
new JobResult(new CronExpression('* * * * *'), $before, JobResultState::done()),
),
Expand All @@ -614,6 +629,7 @@ public function testRunSummary(): void
0,
$before,
null,
false,
),
new JobResult(new CronExpression('* * * * *'), $after, JobResultState::done()),
),
Expand Down Expand Up @@ -650,6 +666,7 @@ public function testJobSummary(?RunParameters $parameters, int $second): void
$second,
$now,
null,
$parameters !== null ? $parameters->isForcedRun() : true,
),
$summary->getInfo(),
);
Expand All @@ -662,8 +679,8 @@ public function testJobSummary(?RunParameters $parameters, int $second): void
public function provideJobSummary(): Generator
{
yield [null, 0];
yield [new RunParameters(10), 10];
yield [new RunParameters(30), 30];
yield [new RunParameters(10, false), 10];
yield [new RunParameters(30, true), 30];
}

public function testTimeZoneExecution(): void
Expand Down Expand Up @@ -777,11 +794,11 @@ static function () use (&$i2): void {
self::assertEquals(
[
new JobSummary(
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now(), null),
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now(), null, false),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::lock()),
),
new JobSummary(
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now(), null),
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now(), null, false),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::done()),
),
],
Expand Down Expand Up @@ -810,11 +827,11 @@ static function () use (&$i2): void {
self::assertEquals(
[
new JobSummary(
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now(), null),
new JobInfo(0, 'job1', '* * * * *', 0, 0, $clock->now(), null, false),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::done()),
),
new JobSummary(
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now(), null),
new JobInfo(1, 'job2', '* * * * *', 0, 0, $clock->now(), null, false),
new JobResult(new CronExpression('* * * * *'), $clock->now(), JobResultState::done()),
),
],
Expand Down Expand Up @@ -984,6 +1001,7 @@ public function testLockedJobEvent(): void
0,
$now,
null,
false,
),
new JobResult(new CronExpression('* * * * *'), $now, JobResultState::lock()),
],
Expand Down
Loading

0 comments on commit 5ea942a

Please sign in to comment.