Skip to content

Commit

Permalink
Fix support for expresion aliases like '@Yearly'
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed May 25, 2024
1 parent 36ff547 commit 7059384
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- `SchedulerExtension`
- `jobs > <id> > enabled` option - allows disabling job via configuration

### Fixed

- support for expression aliases like `'@yearly'`

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

### Added
Expand Down
16 changes: 14 additions & 2 deletions src/DI/SchedulerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@
use Orisai\Scheduler\Manager\JobManager;
use Orisai\Scheduler\Scheduler;
use stdClass;
use function assert;
use function class_exists;
use function function_exists;
use function in_array;
use function is_array;
use function method_exists;
use function str_starts_with;
use function substr;
use function timezone_identifiers_list;

/**
Expand All @@ -43,7 +46,7 @@ public function getConfigSchema(): Schema
{
return Expect::structure([
'errorHandler' => Expect::anyOf(
/* @infection-ignore-all */
/* @infection-ignore-all */
Expect::array()->min(2)->max(2),
'tracy',
null,
Expand Down Expand Up @@ -101,7 +104,16 @@ public function getConfigSchema(): Schema
'enabled' => Expect::bool(true),
'expression' => Expect::string()
->assert(
static fn (string $value): bool => CronExpression::isValidExpression($value),
static function (string $value): bool {
if (str_starts_with($value, '@@')) { // '@yearly' - string
$value = substr($value, 1);
assert($value !== false);
} elseif (str_starts_with($value, '@')) { // @yearly - service reference
return false;
}

return CronExpression::isValidExpression($value);
},
'Valid cron expression',
),
'callback' => Expect::anyOf(
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DI/SchedulerExtension.jobSchedules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ orisai.scheduler:
repeatAfterSeconds: 1
timeZone: Europe/Prague
2:
expression: 1 * * * *
expression: '@yearly'
job: Tests\OriNette\Scheduler\Doubles\TestJob('2')
repeatAfterSeconds: 30
timeZone: UTC
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/DI/SchedulerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function testJobSchedules(): void
self::assertEquals(new DateTimeZone('Europe/Prague'), $schedule->getTimeZone());

$schedule = $schedules[2];
self::assertEquals(new CronExpression('1 * * * *'), $schedule->getExpression());
self::assertEquals(new CronExpression('@yearly'), $schedule->getExpression());
self::assertSame(30, $schedule->getRepeatAfterSeconds());
self::assertEquals(new DateTimeZone('UTC'), $schedule->getTimeZone());
}
Expand Down

0 comments on commit 7059384

Please sign in to comment.