Skip to content

Commit

Permalink
Job: getName()
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 9, 2023
1 parent 03ddac2 commit e3cd096
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 9 deletions.
9 changes: 8 additions & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ $scheduler->addBeforeJobCallback(
function(JobInfo $info): void {
// Executes before job start

$info->getStart(); // DateTimeImmutable
$name = $info->getName(); // string
$start = $info->getStart(); // DateTimeImmutable
},
);

Expand Down Expand Up @@ -196,6 +197,12 @@ use Orisai\Scheduler\Job\Job;
final class CustomJob implements Job
{

public function getName(): string
{
// Provide (preferably unique) name of the job. It will be used in jobs overview
return static::class;
}

public function run(): void
{
// Do whatever you need to
Expand Down
15 changes: 15 additions & 0 deletions src/Job/CallbackJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Orisai\Scheduler\Job;

use Closure;
use ReflectionFunction;
use function str_ends_with;

final class CallbackJob implements Job
{
Expand All @@ -18,6 +20,19 @@ public function __construct(Closure $callback)
$this->callback = $callback;
}

public function getName(): string
{
$ref = new ReflectionFunction($this->callback);
$refName = $ref->getName();

$class = $ref->getClosureScopeClass();
if ($class !== null && !str_ends_with($refName, '{closure}')) {
return "{$class->getName()}::$refName()";
}

return $refName;
}

public function run(): void
{
($this->callback)();
Expand Down
2 changes: 2 additions & 0 deletions src/Job/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
interface Job
{

public function getName(): string;

public function run(): void;

}
1 change: 1 addition & 0 deletions src/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function run(): void

foreach ($jobs as [$job, $expression]) {
$info = new JobInfo(
$job->getName(),
$expression->getExpression(),
$this->clock->now(),
);
Expand Down
10 changes: 9 additions & 1 deletion src/Status/JobInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,24 @@
final class JobInfo
{

private string $name;

private string $expression;

private DateTimeImmutable $start;

public function __construct(string $expression, DateTimeImmutable $start)
public function __construct(string $name, string $expression, DateTimeImmutable $start)
{
$this->name = $name;
$this->expression = $expression;
$this->start = $start;
}

public function getName(): string
{
return $this->name;
}

public function getExpression(): string
{
return $this->expression;
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/Job/CallbackJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Orisai\Scheduler\Unit\Job;

use Closure;
use Orisai\Scheduler\Job\CallbackJob;
use PHPUnit\Framework\TestCase;

Expand All @@ -18,11 +19,26 @@ static function () use (&$i): void {
},
);

self::assertSame(
'Tests\Orisai\Scheduler\Unit\Job\{closure}',
$job->getName(),
);

$job->run();
self::assertSame(1, $i);

$job->run();
self::assertSame(2, $i);
}

public function testStaticName(): void
{
$job = new CallbackJob(Closure::fromCallable([$this, 'testStaticName']));

self::assertSame(
'Tests\Orisai\Scheduler\Unit\Job\CallbackJobTest::testStaticName()',
$job->getName(),
);
}

}
10 changes: 6 additions & 4 deletions tests/Unit/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,19 @@ static function (): void {

self::assertEquals(
[
new JobInfo('* * * * *', $now),
new JobInfo('* * * * *', $now),
new JobInfo('Tests\Orisai\Scheduler\Unit\{closure}', '* * * * *', $now),
new JobInfo('Tests\Orisai\Scheduler\Unit\{closure}', '* * * * *', $now),
],
$beforeCollected,
);
self::assertEquals(
[
[
new JobInfo('* * * * *', $now),
new JobInfo('Tests\Orisai\Scheduler\Unit\{closure}', '* * * * *', $now),
new JobResult($now, new Exception('test')),
],
[
new JobInfo('* * * * *', $now),
new JobInfo('Tests\Orisai\Scheduler\Unit\{closure}', '* * * * *', $now),
new JobResult($now, null),
],
],
Expand Down Expand Up @@ -151,6 +151,7 @@ static function (): void {
self::assertEquals(
[
new JobInfo(
'Tests\Orisai\Scheduler\Unit\{closure}',
'* * * * *',
DateTimeImmutable::createFromFormat('U', '1'),
),
Expand All @@ -161,6 +162,7 @@ static function (): void {
[
[
new JobInfo(
'Tests\Orisai\Scheduler\Unit\{closure}',
'* * * * *',
DateTimeImmutable::createFromFormat('U', '1'),
),
Expand Down
4 changes: 3 additions & 1 deletion tests/Unit/Status/JobInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ final class JobInfoTest extends TestCase

public function test(): void
{
$name = 'name';
$expression = '* * * * *';
$start = new DateTimeImmutable();

$info = new JobInfo($expression, $start);
$info = new JobInfo($name, $expression, $start);
self::assertSame($name, $info->getName());
self::assertSame($expression, $info->getExpression());
self::assertSame($start, $info->getStart());
}
Expand Down
4 changes: 2 additions & 2 deletions tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\$expression of class Orisai\\\\Scheduler\\\\Status\\\\JobInfo constructor expects string, string\\|null given\\.$#"
message: "#^Parameter \\#2 \\$expression of class Orisai\\\\Scheduler\\\\Status\\\\JobInfo constructor expects string, string\\|null given\\.$#"
count: 1
path: ../src/Scheduler.php

Expand Down Expand Up @@ -36,6 +36,6 @@ parameters:
path: ../tests/Unit/SchedulerTest.php

-
message: "#^Parameter \\#2 \\$start of class Orisai\\\\Scheduler\\\\Status\\\\JobInfo constructor expects DateTimeImmutable, DateTimeImmutable\\|false given\\.$#"
message: "#^Parameter \\#3 \\$start of class Orisai\\\\Scheduler\\\\Status\\\\JobInfo constructor expects DateTimeImmutable, DateTimeImmutable\\|false given\\.$#"
count: 2
path: ../tests/Unit/SchedulerTest.php

0 comments on commit e3cd096

Please sign in to comment.