Skip to content

Commit

Permalink
CallbackJobManager replaced by SimpleJobManager->addLazyJob() and Sim…
Browse files Browse the repository at this point in the history
…pleScheduler->addLazyJob()
  • Loading branch information
mabar committed Dec 19, 2023
1 parent 8f1f5e2 commit ea267e5
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 109 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `SimpleScheduler`
- `addJob()` accepts parameter `repeatAfterSeconds`
- `addJob()` accepts parameter `timeZone`
- `addLazyJob()` replaces `CallbackJobManager`
- `JobInfo`
- `getRepeatAfterSeconds()`- returns the seconds part of expression
- `getExtendedExpression()` - returns cron expression including seconds
Expand All @@ -23,9 +24,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `SimpleJobManager`
- `addJob()` accepts parameter `repeatAfterSeconds`
- `addJob()` accepts parameter `timeZone`
- `CallbackJobManager`
- `addJob()` accepts parameter `repeatAfterSeconds`
- `addJob()` accepts parameter `timeZone`
- `addLazyJob()` replaces `CallbackJobManager`
- `ListCommand`
- prints `repeatAfterSeconds` parameter
- prints job's `timeZone` parameter
Expand Down Expand Up @@ -57,6 +56,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- `JobManager`
- `getExpressions()` - replaced by `getJobSchedules()`
- `CallbackJobManager` - use `SimpleJobManager->addLazyJob()` instead

### Fixed

Expand Down
24 changes: 18 additions & 6 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -553,23 +553,35 @@ Jobs are executed only when it is their due time. To prevent initializing potent
are not needed, you may lazy load the jobs. This is especially helpful
if [separate processes](#parallelization-and-process-isolation) are used for jobs.

To achieve it, use `ManagedScheduler` instead of `SimpleScheduler`. It is functionally identical, except:
```php
use Orisai\Scheduler\Job\Job;

$jobConstructor = function(): Job {
// Initialize job
};
$scheduler->addLazyJob($jobConstructor, $expression, /* ... */);
```

For the same purpose you can also use `ManagedScheduler` instead of `SimpleScheduler`. It is functionally identical,
except:

- it requires a `JobManager` implementation as a first argument
- `addJob()` method is in `JobManager` instead of scheduler

Either use our `CallbackJobManager` implementation to construct job via a callback or use it as an inspiration to create
Either use our `SimpleJobManager` implementation to construct job via a callback or use it as an inspiration to create
your own DI-specific version.

```php
use Orisai\Scheduler\Job\CallbackJob;
use Orisai\Scheduler\Job\Job;
use Orisai\Scheduler\ManagedScheduler;
use Orisai\Scheduler\Manager\CallbackJobManager;
use Orisai\Scheduler\Manager\SimpleJobManager;

$manager = new CallbackJobManager();
$manager->addJob(fn (): Job => new CallbackJob(/* ... */), $expression);
$manager->addJob(fn (): Job => new CallbackJob(/* ... */), $expression, $id);
$manager = new SimpleJobManager();
$jobConstructor = function(): Job {
// Initialize job
};
$manager->addLazyJob($jobConstructor, $expression, /* ... */);

$scheduler = new ManagedScheduler($manager);
```
Expand Down
48 changes: 0 additions & 48 deletions src/Manager/CallbackJobManager.php

This file was deleted.

22 changes: 22 additions & 0 deletions src/Manager/SimpleJobManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Orisai\Scheduler\Manager;

use Closure;
use Cron\CronExpression;
use DateTimeZone;
use Orisai\Scheduler\Job\Job;
Expand Down Expand Up @@ -33,6 +34,27 @@ public function addJob(
}
}

/**
* @param Closure(): Job $jobConstructor
* @param int<0, 30> $repeatAfterSeconds
*/
public function addLazyJob(
Closure $jobConstructor,
CronExpression $expression,
?string $id = null,
int $repeatAfterSeconds = 0,
?DateTimeZone $timeZone = null
): void
{
$jobSchedule = JobSchedule::createLazy($jobConstructor, $expression, $repeatAfterSeconds, $timeZone);

if ($id === null) {
$this->jobSchedules[] = $jobSchedule;
} else {
$this->jobSchedules[$id] = $jobSchedule;
}
}

public function getJobSchedule($id): ?JobSchedule
{
return $this->jobSchedules[$id] ?? null;
Expand Down
15 changes: 15 additions & 0 deletions src/SimpleScheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,19 @@ public function addJob(
$this->jobManager->addJob($job, $expression, $id, $repeatAfterSeconds, $timeZone);
}

/**
* @param Closure(): Job $jobConstructor
* @param int<0, 30> $repeatAfterSeconds
*/
public function addLazyJob(
Closure $jobConstructor,
CronExpression $expression,
?string $id = null,
int $repeatAfterSeconds = 0,
?DateTimeZone $timeZone = null
): void
{
$this->jobManager->addLazyJob($jobConstructor, $expression, $id, $repeatAfterSeconds, $timeZone);
}

}
52 changes: 0 additions & 52 deletions tests/Unit/Manager/CallbackJobManagerTest.php

This file was deleted.

37 changes: 37 additions & 0 deletions tests/Unit/Manager/SimpleJobManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Cron\CronExpression;
use DateTimeZone;
use Orisai\Scheduler\Job\CallbackJob;
use Orisai\Scheduler\Job\Job;
use Orisai\Scheduler\Job\JobSchedule;
use Orisai\Scheduler\Manager\SimpleJobManager;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -46,4 +47,40 @@ public function test(): void
self::assertSame($manager->getJobSchedule(0), $manager->getJobSchedule(0));
}

public function testLazy(): void
{
$manager = new SimpleJobManager();
self::assertSame([], $manager->getJobSchedules());
self::assertNull($manager->getJobSchedule(0));
self::assertNull($manager->getJobSchedule('id'));
self::assertNull($manager->getJobSchedule(42));

$job1 = new CallbackJob(static function (): void {
// Noop
});
$job1Ctor = static fn (): Job => $job1;
$expression1 = new CronExpression('* * * * *');
$manager->addLazyJob($job1Ctor, $expression1);

$job2 = clone $job1;
$job2Ctor = static fn (): Job => $job2;
$expression2 = clone $expression1;
$manager->addLazyJob($job2Ctor, $expression2, 'id', 1, new DateTimeZone('Europe/Prague'));

$schedule1 = JobSchedule::createLazy($job1Ctor, $expression1, 0);
$schedule2 = JobSchedule::createLazy($job2Ctor, $expression2, 1, new DateTimeZone('Europe/Prague'));

self::assertEquals(
[
0 => $schedule1,
'id' => $schedule2,
],
$manager->getJobSchedules(),
);
self::assertEquals($schedule1, $manager->getJobSchedule(0));
self::assertEquals($schedule2, $manager->getJobSchedule('id'));
self::assertNull($manager->getJobSchedule(42));
self::assertSame($manager->getJobSchedule(0), $manager->getJobSchedule(0));
}

}
30 changes: 30 additions & 0 deletions tests/Unit/SimpleSchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Orisai\Scheduler\Exception\JobFailure;
use Orisai\Scheduler\Exception\RunFailure;
use Orisai\Scheduler\Job\CallbackJob;
use Orisai\Scheduler\Job\Job;
use Orisai\Scheduler\Job\JobSchedule;
use Orisai\Scheduler\SimpleScheduler;
use Orisai\Scheduler\Status\JobInfo;
Expand Down Expand Up @@ -61,6 +62,35 @@ static function () use (&$i): void {
self::assertSame(4, $i);
}

public function testLazyJob(): void
{
$scheduler = new SimpleScheduler();

$executed = false;
$jobConstructor = static function () use (&$executed): Job {
$executed = true;

return new CallbackJob(static function (): void {
});
};
$expression = new CronExpression('* * * * *');

$scheduler->addLazyJob($jobConstructor, $expression);
$scheduler->addLazyJob($jobConstructor, $expression, null, 1, new DateTimeZone('Europe/Prague'));
self::assertFalse($executed);

self::assertEquals(
$scheduler->getJobSchedules(),
[
0 => JobSchedule::createLazy($jobConstructor, $expression, 0),
1 => JobSchedule::createLazy($jobConstructor, $expression, 1, new DateTimeZone('Europe/Prague')),
],
);

$scheduler->runJob(0);
self::assertTrue($executed);
}

public function testJobKey(): void
{
$scheduler = new SimpleScheduler();
Expand Down
5 changes: 5 additions & 0 deletions tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ parameters:
count: 1
path: ../tests/Unit/SimpleSchedulerTest.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with false will always evaluate to false\\.$#"
count: 1
path: ../tests/Unit/SimpleSchedulerTest.php

-
message: "#^Dead catch \\- Tests\\\\Orisai\\\\Scheduler\\\\Doubles\\\\JobInnerFailure is never thrown in the try block\\.$#"
count: 2
Expand Down

0 comments on commit ea267e5

Please sign in to comment.