Skip to content

Commit

Permalink
feat: Add $expression param (#26)
Browse files Browse the repository at this point in the history
Co-authored-by: Sergey Syomkin <[email protected]>
  • Loading branch information
thebbstudio and Sergey Syomkin authored Dec 27, 2024
1 parent 59f0c84 commit 2ef4bd1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand All @@ -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,
);

Expand All @@ -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,
Expand All @@ -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);
}
Expand Down
13 changes: 13 additions & 0 deletions tests/src/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Spiral\Scheduler\Tests;

use Cron\CronExpression;
use Spiral\Scheduler\CommandRunner;
use Spiral\Scheduler\Mutex\JobMutexInterface;
use Spiral\Scheduler\ProcessFactory;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2ef4bd1

Please sign in to comment.