Skip to content

Commit

Permalink
SchedulerExtension: 'jobs > <id> > enabled' option
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Apr 7, 2024
1 parent 8d37a43 commit 4c6c741
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- compatibility with orisai/scheduler:^2.1.0
- `scheduler:explain` command is available
- `SchedulerExtension`
- `jobs > <id> > enabled` option - allows disabling job via configuration

## [1.1.0](https://github.com/orisai/nette-scheduler/compare/1.0.2...1.1.0) - 2024-01-26

Expand Down
14 changes: 14 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [Cron expression - minutes and above](#cron-expression---minutes-and-above)
- [Seconds](#seconds)
- [Timezones](#timezones)
- [Disabling job](#disabling-job)
- [Events](#events)
- [Before job event](#before-job-event)
- [After job event](#after-job-event)
Expand Down Expand Up @@ -249,6 +250,19 @@ timezone checking logic yourself. Several time zones have deviations of either 3
is the standard time in Newfoundland, while Nepal's standard time is UTC+05:45. Indian Standard Time is UTC+05:30, and
Myanmar Standard Time is UTC+06:30.

## Disabling job

You can disable any job by `enabled: false`. It will be skipped during jobs registration and not appear on the jobs
list.

```neon
orisai.scheduler:
jobs:
-
enabled: false # bool
expression: # ...
```

## Events

Run callbacks to collect statistics, etc.
Expand Down
9 changes: 9 additions & 0 deletions src/DI/SchedulerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public function getConfigSchema(): Schema
]),
'jobs' => Expect::arrayOf(
Expect::structure([
'enabled' => Expect::bool(true),
'expression' => Expect::string()
->assert(
static fn (string $value): bool => CronExpression::isValidExpression($value),
Expand Down Expand Up @@ -203,6 +204,10 @@ private function registerJobManager(ContainerBuilder $builder, stdClass $config)
$jobs = [];
$expressions = [];
foreach ($config->jobs as $id => $job) {
if (!$job->enabled) {
continue;
}

/** @codeCoverageIgnore */
if ($job->repeatAfterSeconds !== 0) {
throw InvalidArgument::create()
Expand Down Expand Up @@ -234,6 +239,10 @@ private function registerJobManager(ContainerBuilder $builder, stdClass $config)

$jobSchedules = [];
foreach ($config->jobs as $id => $job) {
if (!$job->enabled) {
continue;
}

$jobDefinitionName = $this->registerJob($id, $job, $builder, $loader);
$jobSchedules[$id] = [
'job' => $jobDefinitionName,
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/DI/SchedulerExtension.enabledJob.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extensions:
orisai.scheduler: OriNette\Scheduler\DI\SchedulerExtension

orisai.scheduler:
executor: basic

jobs:
0:
expression: * * * * *
job: Tests\OriNette\Scheduler\Doubles\TestJob('0')
enabled: false
1:
expression: * * * * *
job: Tests\OriNette\Scheduler\Doubles\TestJob('1')
enabled: true
2:
expression: * * * * *
job: Tests\OriNette\Scheduler\Doubles\TestJob('2')

services:
- Orisai\Clock\FrozenClock(1)
32 changes: 32 additions & 0 deletions tests/Unit/DI/SchedulerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,38 @@ public function testJobSchedules(): void
self::assertEquals(new DateTimeZone('UTC'), $schedule->getTimeZone());
}

public function testEnabledJob(): void
{
$configurator = new ManualConfigurator($this->rootDir);
$configurator->setForceReloadContainer();
$configurator->addConfig(__DIR__ . '/SchedulerExtension.enabledJob.neon');

$container = $configurator->createContainer();

$scheduler = $container->getByType(Scheduler::class);

self::assertFalse($container->hasService('orisai.scheduler.job.0'));
$job1 = $container->getService('orisai.scheduler.job.1');
self::assertInstanceOf(TestJob::class, $job1);
$job2 = $container->getService('orisai.scheduler.job.2');
self::assertInstanceOf(TestJob::class, $job2);

self::assertSame(0, $job1->executions);
self::assertSame(0, $job2->executions);

$result = $scheduler->run();

// Compat - orisai/scheduler v1
if (method_exists($result, 'getJobs')) {
self::assertCount(2, $result->getJobs());
} else {
self::assertCount(2, $result->getJobSummaries());
}

self::assertSame(1, $job1->executions);
self::assertSame(1, $job2->executions);
}

public function testInvalidTimeZone(): void
{
$configurator = new ManualConfigurator($this->rootDir);
Expand Down
2 changes: 1 addition & 1 deletion tools/phpstan.baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ parameters:

-
message: "#^Call to function method_exists\\(\\) with Orisai\\\\Scheduler\\\\Status\\\\RunSummary and 'getJobs' will always evaluate to false\\.$#"
count: 2
count: 3
path: ../tests/Unit/DI/SchedulerExtensionTest.php

0 comments on commit 4c6c741

Please sign in to comment.