Skip to content

Commit

Permalink
Run summary
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 9, 2023
1 parent e3cd096 commit 6791d35
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 4 deletions.
41 changes: 38 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Cron job scheduler - with locks, parallelism and more
- [Job types](#job-types)
- [Callback job](#callback-job)
- [Custom job](#custom-job)
- [Job info and result](#job-info-and-result)
- [Run summary](#run-summary)

## Why do you need it?

Expand Down Expand Up @@ -135,13 +137,12 @@ $scheduler->addBeforeJobCallback(
$scheduler->addAfterJobCallback(
function(JobInfo $info, JobResult $result): void {
// Executes after job finish

$end = $result->getEnd(); // DateTimeImmutable
$throwable = $result->getThrowable(); // Throwable|null
},
);
```

Check [job info and result](#job-info-and-result) for available status info

## Logging errors

Because any exceptions and errors are suppressed to prevent one failing job from failing others, you have to handle
Expand Down Expand Up @@ -217,3 +218,37 @@ $scheduler->addJob(
/* ... */,
);
```

## Job info and result

Status information available via [events](#events) and [run summary](#run-summary)

Info:

```php
$name = $info->getName(); // string
$expression = $info->getExpression(); // string, e.g. '* * * * *'
$start = $info->getStart(); // DateTimeImmutable
```

Result:

```php
$end = $result->getEnd(); // DateTimeImmutable
$throwable = $result->getThrowable(); // Throwable|null
```

## Run summary

Scheduler run returns summary for inspection

```php
$summary = $scheduler->run(); // RunSummary

foreach ($summary->getJobs() as [$info, $result]) {
// $info instanceof JobInfo
// $result instanceof JobResult
}
```

Check [job info and result](#job-info-and-result) for available jobs status info
8 changes: 7 additions & 1 deletion src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Orisai\Scheduler\Job\Job;
use Orisai\Scheduler\Status\JobInfo;
use Orisai\Scheduler\Status\JobResult;
use Orisai\Scheduler\Status\RunSummary;
use Psr\Clock\ClockInterface;
use Throwable;

Expand Down Expand Up @@ -35,7 +36,7 @@ public function addJob(Job $job, CronExpression $expression): void
$this->jobs[] = [$job, $expression];
}

public function run(): void
public function run(): RunSummary
{
$runStart = $this->clock->now();
$jobs = [];
Expand All @@ -45,6 +46,7 @@ public function run(): void
}
}

$summaryJobs = [];
foreach ($jobs as [$job, $expression]) {
$info = new JobInfo(
$job->getName(),
Expand All @@ -68,7 +70,11 @@ public function run(): void
foreach ($this->afterJob as $cb) {
$cb($info, $result);
}

$summaryJobs[] = [$info, $result];
}

return new RunSummary($summaryJobs);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions src/Status/RunSummary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace Orisai\Scheduler\Status;

final class RunSummary
{

/** @var list<array{JobInfo, JobResult}> */
private array $jobs;

/**
* @param list<array{JobInfo, JobResult}> $jobs
*/
public function __construct(array $jobs)
{
$this->jobs = $jobs;
}

/**
* @return list<array{JobInfo, JobResult}>
*/
public function getJobs(): array
{
return $this->jobs;
}

}
39 changes: 39 additions & 0 deletions tests/Unit/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,43 @@ static function () use (&$i): void {
self::assertSame(1, $i);
}

public function testRunSummary(): void
{
$clock = new FrozenClock(1);
$scheduler = new Scheduler($clock);

$job = new CallbackJob(
static function (): void {
// Noop
},
);
$scheduler->addJob($job, new CronExpression('* * * * *'));
$scheduler->addJob($job, new CronExpression('* * * * *'));

$summary = $scheduler->run();

$now = $clock->now();
self::assertEquals(
[
[
new JobInfo(
'Tests\Orisai\Scheduler\Unit\{closure}',
'* * * * *',
$now,
),
new JobResult($now, null),
],
[
new JobInfo(
'Tests\Orisai\Scheduler\Unit\{closure}',
'* * * * *',
$now,
),
new JobResult($now, null),
],
],
$summary->getJobs(),
);
}

}
34 changes: 34 additions & 0 deletions tests/Unit/Status/RunSummaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Tests\Orisai\Scheduler\Unit\Status;

use DateTimeImmutable;
use Orisai\Scheduler\Status\JobInfo;
use Orisai\Scheduler\Status\JobResult;
use Orisai\Scheduler\Status\RunSummary;
use PHPUnit\Framework\TestCase;

final class RunSummaryTest extends TestCase
{

public function test(): void
{
$jobs = [
[
new JobInfo('1', '* * * * *', new DateTimeImmutable()),
new JobResult(new DateTimeImmutable(), null),
],
[
new JobInfo('2', '1 * * * *', new DateTimeImmutable()),
new JobResult(new DateTimeImmutable(), null),
],
];

$summary = new RunSummary($jobs);
self::assertSame(
$jobs,
$summary->getJobs(),
);
}

}

0 comments on commit 6791d35

Please sign in to comment.