diff --git a/src/Schedule.php b/src/Schedule.php index 37b0609..9f70c06 100644 --- a/src/Schedule.php +++ b/src/Schedule.php @@ -26,7 +26,7 @@ public function __construct( /** * Add a new console command to the schedule. */ - public function command(string $commandName, array $parameters = [], ?string $description = null): CommandJob + public function command(string $commandName, array $parameters = [], ?string $description = null, ?CronExpression $expression = null): CommandJob { if (\class_exists($commandName)) { /** @var Command $command */ @@ -42,13 +42,14 @@ public function command(string $commandName, array $parameters = [], ?string $de $this->commandRunner->formatCommandString($commandName), $parameters, $description, + $expression ); } /** * Add a new command job to the schedule. */ - public function exec(string $command, array $parameters = [], ?string $description = null): CommandJob + public function exec(string $command, array $parameters = [], ?string $description = null, ?CronExpression $expression = null): CommandJob { if (\count($parameters)) { $command .= ' ' . CommandUtils::compileParameters($parameters); @@ -58,7 +59,7 @@ public function exec(string $command, array $parameters = [], ?string $descripti commandBuilder: new CommandBuilder($this->commandRunner), processFactory: $this->processFactory, mutex: $this->jobMutex, - expression: $this->createCronExpression(), + expression: $expression ?? $this->createDefaultCronExpression(), command: $command, ); @@ -72,7 +73,7 @@ public function call(string $description, \Closure $callback, array $parameters { $job = new CallbackJob( mutex: $this->jobMutex, - expression: $this->createCronExpression(), + expression: $this->createDefaultCronExpression(), description: $description, callback: $callback, parameters: $parameters, @@ -83,7 +84,7 @@ public function call(string $description, \Closure $callback, array $parameters return $job; } - private function createCronExpression(): CronExpression + private function createDefaultCronExpression(): CronExpression { return new CronExpression(self::DEFAULT_EXPRESSION); } diff --git a/tests/src/ScheduleTest.php b/tests/src/ScheduleTest.php index fa69e39..e26d50c 100644 --- a/tests/src/ScheduleTest.php +++ b/tests/src/ScheduleTest.php @@ -4,6 +4,7 @@ namespace Spiral\Scheduler\Tests; +use Cron\CronExpression; use Spiral\Scheduler\CommandRunner; use Spiral\Scheduler\Mutex\JobMutexInterface; use Spiral\Scheduler\ProcessFactory; @@ -43,6 +44,18 @@ public function testRegisterCommandByClassname(): void $this->registry->assertRegisteredJob($job); } + public function testRegisterCommandWithCronExpression(): void + { + $job = $this->schedule + ->command(SimpleCommand::class, ['baz' => 'biz'], 'Simple job', new CronExpression('*/2 * * * *')); + + $this->assertSame('/usr/bin/php app.php foo:bar baz=\'biz\'', $job->getName()); + $this->assertSame('Simple command', $job->getDescription()); + $this->assertSame('*/2 * * * *', $job->getExpression()); + + $this->registry->assertRegisteredJob($job); + } + public function testRegisterCallableJob(): void { $job = $this->schedule