Skip to content

Commit

Permalink
[minor] Schedule::addPing()/CompoundTask::addPing() convenience methods
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Oct 30, 2020
1 parent 8ecb46b commit 2f788a6
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 16 deletions.
4 changes: 1 addition & 3 deletions doc/define-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,9 @@ tools like [Oh Dear](https://ohdear.app/), [Cronitor](https://cronitor.io/) and
**Define in [PHP](define-schedule.md#schedulebuilder-service):**

```php
use Zenstruck\ScheduleBundle\Schedule\Task\PingTask;
/* @var \Zenstruck\ScheduleBundle\Schedule $schedule */
$schedule->add(new PingTask('https://example.com'));
$schedule->addPing('https://example.com');
```

**Define in [Configuration](define-schedule.md#bundle-configuration):**
Expand Down
16 changes: 12 additions & 4 deletions src/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Zenstruck\ScheduleBundle;

use Symfony\Component\Process\Process;
use Zenstruck\ScheduleBundle\Schedule\Exception\SkipSchedule;
use Zenstruck\ScheduleBundle\Schedule\Extension\CallbackExtension;
use Zenstruck\ScheduleBundle\Schedule\Extension\EmailExtension;
Expand All @@ -15,6 +14,7 @@
use Zenstruck\ScheduleBundle\Schedule\Task\CallbackTask;
use Zenstruck\ScheduleBundle\Schedule\Task\CommandTask;
use Zenstruck\ScheduleBundle\Schedule\Task\CompoundTask;
use Zenstruck\ScheduleBundle\Schedule\Task\PingTask;
use Zenstruck\ScheduleBundle\Schedule\Task\ProcessTask;

/**
Expand Down Expand Up @@ -55,29 +55,37 @@ public function add(Task $task): Task
}

/**
* @param string $name Command class or name (my:command)
* @see CommandTask::__construct()
*/
public function addCommand(string $name, string ...$arguments): CommandTask
{
return $this->add(new CommandTask($name, ...$arguments));
}

/**
* @param callable $callback Return value is considered "output"
* @see CallbackTask::__construct()
*/
public function addCallback(callable $callback): CallbackTask
{
return $this->add(new CallbackTask($callback));
}

/**
* @param string|Process $process
* @see ProcessTask::__construct()
*/
public function addProcess($process): ProcessTask
{
return $this->add(new ProcessTask($process));
}

/**
* @see PingTask::__construct()
*/
public function addPing(string $url, string $method = 'GET', array $options = []): PingTask
{
return $this->add(new PingTask($url, $method, $options));
}

public function addCompound(): CompoundTask
{
return $this->add(new CompoundTask());
Expand Down
3 changes: 3 additions & 0 deletions src/Schedule/Task/CommandTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ final class CommandTask extends Task
private $name;
private $arguments;

/**
* @param string $name Command class or name (my:command)
*/
public function __construct(string $name, string ...$arguments)
{
$parts = \explode(' ', $name, 2);
Expand Down
23 changes: 19 additions & 4 deletions src/Schedule/Task/CompoundTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Zenstruck\ScheduleBundle\Schedule\Task;

use Symfony\Component\Process\Process;
use Zenstruck\ScheduleBundle\Schedule\Task;

/**
Expand Down Expand Up @@ -30,29 +29,45 @@ public function add(Task $task): self
}

/**
* @param string $name Command class or name (my:command)
* @see CommandTask::__construct()
*
* @param string|null $description optional description
*/
public function addCommand(string $name, array $arguments = [], string $description = null): self
{
return $this->addWithDescription(new CommandTask($name, ...$arguments), $description);
}

/**
* @param callable $callback Return value is considered "output"
* @see CallbackTask::__construct()
*
* @param string|null $description optional description
*/
public function addCallback(callable $callback, string $description = null): self
{
return $this->addWithDescription(new CallbackTask($callback), $description);
}

/**
* @param string|Process $process
* @see ProcessTask::__construct()
*
* @param string|null $description optional description
*/
public function addProcess($process, string $description = null): self
{
return $this->addWithDescription(new ProcessTask($process), $description);
}

/**
* @see PingTask::__construct()
*
* @param string|null $description optional description
*/
public function addPing(string $url, string $method = 'GET', array $options = [], string $description = null): self
{
return $this->addWithDescription(new PingTask($url, $method, $options), $description);
}

/**
* @return Task[]
*/
Expand Down
16 changes: 11 additions & 5 deletions tests/ScheduleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ public function can_add_tasks()
$schedule->addProcess('php -v')->description('task3');
$schedule->addProcess(new Process(['php -v']))->description('task4');
$schedule->addCommand('my:command')->description('task5');
$schedule->addPing('https://example.com')->description('task6');

$this->assertCount(5, $schedule->all());
$this->assertSame(['task1', 'task2', 'task3', 'task4', 'task5'], \array_map(
$this->assertCount(6, $schedule->all());
$this->assertSame(['task1', 'task2', 'task3', 'task4', 'task5', 'task6'], \array_map(
function(Task $task) {
return $task->getDescription();
},
$schedule->all()
));

$this->assertCount(5, $schedule->all(), 'Caches the tasks');
$this->assertCount(6, $schedule->all(), 'Caches the tasks');

$schedule->addCommand('another:command');

$this->assertCount(6, $schedule->all(), 'Resets the task cache on add');
$this->assertCount(7, $schedule->all(), 'Resets the task cache on add');
}

/**
Expand All @@ -63,12 +64,13 @@ public function can_add_compound_tasks()
->sundays()
->timezone('America/Los_Angeles')
)
->addPing('https://example.com', 'GET', [], 'task7')
->timezone('UTC')
->mondays()
->onSingleServer()
;

$this->assertCount(6, $schedule->all());
$this->assertCount(7, $schedule->all());
$this->assertSame('task1', $schedule->all()[0]->getDescription());
$this->assertSame('* * * * 2', (string) $schedule->all()[0]->getExpression());
$this->assertNull($schedule->all()[0]->getTimezone());
Expand All @@ -93,6 +95,10 @@ public function can_add_compound_tasks()
$this->assertSame('* * * * 1', (string) $schedule->all()[5]->getExpression());
$this->assertSame('UTC', $schedule->all()[5]->getTimezone()->getName());
$this->assertCount(1, $schedule->all()[5]->getExtensions());
$this->assertSame('task7', $schedule->all()[6]->getDescription());
$this->assertSame('* * * * 1', (string) $schedule->all()[6]->getExpression());
$this->assertSame('UTC', $schedule->all()[6]->getTimezone()->getName());
$this->assertCount(1, $schedule->all()[6]->getExtensions());
}

/**
Expand Down

0 comments on commit 2f788a6

Please sign in to comment.