From 4c6c7410bacdec03359ce89e6a1d9f1dacd5e5b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Barto=C5=A1?= Date: Sun, 7 Apr 2024 23:46:26 +0200 Subject: [PATCH] SchedulerExtension: 'jobs > > enabled' option --- CHANGELOG.md | 2 ++ docs/README.md | 14 ++++++++ src/DI/SchedulerExtension.php | 9 ++++++ .../DI/SchedulerExtension.enabledJob.neon | 21 ++++++++++++ tests/Unit/DI/SchedulerExtensionTest.php | 32 +++++++++++++++++++ tools/phpstan.baseline.neon | 2 +- 6 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 tests/Unit/DI/SchedulerExtension.enabledJob.neon diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fd5a4e..956109d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 > > 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 diff --git a/docs/README.md b/docs/README.md index 6d03669..4a313d5 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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) @@ -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. diff --git a/src/DI/SchedulerExtension.php b/src/DI/SchedulerExtension.php index dfdd41b..15161e4 100644 --- a/src/DI/SchedulerExtension.php +++ b/src/DI/SchedulerExtension.php @@ -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), @@ -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() @@ -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, diff --git a/tests/Unit/DI/SchedulerExtension.enabledJob.neon b/tests/Unit/DI/SchedulerExtension.enabledJob.neon new file mode 100644 index 0000000..11d4698 --- /dev/null +++ b/tests/Unit/DI/SchedulerExtension.enabledJob.neon @@ -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) diff --git a/tests/Unit/DI/SchedulerExtensionTest.php b/tests/Unit/DI/SchedulerExtensionTest.php index 8a5e5a8..2a44fc8 100644 --- a/tests/Unit/DI/SchedulerExtensionTest.php +++ b/tests/Unit/DI/SchedulerExtensionTest.php @@ -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); diff --git a/tools/phpstan.baseline.neon b/tools/phpstan.baseline.neon index 4b0d516..b5b4d18 100644 --- a/tools/phpstan.baseline.neon +++ b/tools/phpstan.baseline.neon @@ -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