Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "task" option for schedule:run command #129

Merged
merged 6 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ This option allow to run all tasks regardless of configured run time,
part of issue [#11](https://github.com/lavary/crunz/issues/11) -
PR [#120](https://github.com/lavary/crunz/pull/120) by [@PabloKowalczyk](https://github.com/PabloKowalczyk)

- `--task=TASK-NUMBER` option to `schedule:run` command.
This option allow to run only one specific task,
part of issue [#11](https://github.com/lavary/crunz/issues/11) -
PR [#129](https://github.com/lavary/crunz/pull/129) by [@PabloKowalczyk](https://github.com/PabloKowalczyk)

### Changed
- Tasks in `schedule:list` is sorted by filename,
PR [#129](https://github.com/lavary/crunz/pull/129) by [@PabloKowalczyk](https://github.com/PabloKowalczyk)

## 1.7.3 - 2018-06-15

### Fixed
Expand Down
6 changes: 6 additions & 0 deletions config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<argument type="service" id="Crunz\Configuration\Configuration"/>
<argument type="service" id="Crunz\EventRunner"/>
<argument type="service" id="Crunz\Task\Timezone"/>
<argument type="service" id="Crunz\Schedule\ScheduleFactory"/>
</service>

<service
Expand Down Expand Up @@ -152,6 +153,11 @@
id="Symfony\Component\Filesystem\Filesystem"
/>

<service
class="Crunz\Schedule\ScheduleFactory"
id="Crunz\Schedule\ScheduleFactory"
/>

<service
class="Crunz\Task\Timezone"
id="Crunz\Task\Timezone"
Expand Down
57 changes: 44 additions & 13 deletions src/Console/Command/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Crunz\EventRunner;
use Crunz\Schedule;
use Crunz\Task\Collection;
use Crunz\Task\TaskNumber;
use Crunz\Task\Timezone;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -28,17 +29,21 @@ class ScheduleRunCommand extends Command
private $eventRunner;
/** @var Timezone */
private $taskTimezone;
/** @var Schedule\ScheduleFactory */
private $scheduleFactory;

public function __construct(
Collection $taskCollection,
Configuration $configuration,
EventRunner $eventRunner,
Timezone $taskTimezone
Timezone $taskTimezone,
Schedule\ScheduleFactory $scheduleFactory
) {
$this->taskCollection = $taskCollection;
$this->configuration = $configuration;
$this->eventRunner = $eventRunner;
$this->taskTimezone = $taskTimezone;
$this->scheduleFactory = $scheduleFactory;

parent::__construct();
}
Expand Down Expand Up @@ -70,6 +75,13 @@ protected function configure()
InputOption::VALUE_NONE,
'Run all tasks regardless of configured run time.'
)
->addOption(
'task',
't',
InputOption::VALUE_REQUIRED,
'Which task to run. Provide task number from <info>schedule:list</info> command.',
null
)
->setHelp('This command starts the Crunz event runner.');
}

Expand All @@ -80,19 +92,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->arguments = $input->getArguments();
$this->options = $input->getOptions();
$task = $this->options['task'];
$files = $this->taskCollection
->all($this->arguments['source'])
;

if (!count($files)) {
if (!\count($files)) {
$output->writeln('<comment>No task found! Please check your source path.</comment>');

return 0;
}

// List of schedules
$schedules = [];

$tasksTimezone = $this->taskTimezone
->timezoneForComparisons()
;
Expand All @@ -103,20 +115,39 @@ protected function execute(InputInterface $input, OutputInterface $output)
continue;
}

if (false === $this->options['force']) {
// We keep the events which are due and dismiss the rest.
$schedule->events(
$schedule->dueEvents(
$tasksTimezone
)
);
}

if (count($schedule->events())) {
if (\count($schedule->events())) {
$schedules[] = $schedule;
}
}

// Is specified task should be invoked?
if (null !== $task) {
$schedules = $this->scheduleFactory
->singleTaskSchedule(TaskNumber::fromString($task), ...$schedules);
}

$schedules = \array_map(
function (Schedule $schedule) use ($tasksTimezone) {
if (false === $this->options['force']) {
// We keep the events which are due and dismiss the rest.
$schedule->events(
$schedule->dueEvents(
$tasksTimezone
)
);
}

return $schedule;
},
$schedules
);
$schedules = \array_filter(
$schedules,
function (Schedule $schedule) {
return \count($schedule->events());
}
);

if (!count($schedules)) {
$output->writeln('<comment>No event is due!</comment>');

Expand Down
7 changes: 7 additions & 0 deletions src/Exception/TaskNotExistException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Crunz\Exception;

class TaskNotExistException extends CrunzException
{
}
7 changes: 7 additions & 0 deletions src/Exception/WrongTaskNumberException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Crunz\Exception;

class WrongTaskNumberException extends CrunzException
{
}
42 changes: 42 additions & 0 deletions src/Schedule/ScheduleFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Crunz\Schedule;

use Crunz\Exception\TaskNotExistException;
use Crunz\Schedule;
use Crunz\Task\TaskNumber;

class ScheduleFactory
{
/**
* @param TaskNumber $taskNumber
* @param Schedule[] $schedules
*
* @return Schedule[]
*
* @throws TaskNotExistException
*/
public function singleTaskSchedule(TaskNumber $taskNumber, Schedule ...$schedules)
{
$events = \array_map(
function (Schedule $schedule) {
return $schedule->events();
},
$schedules
);

$flattenEvents = \array_merge(...$events);

if (!isset($flattenEvents[$taskNumber->asArrayIndex()])) {
$tasksCount = \count($flattenEvents);
throw new TaskNotExistException("Task with id '{$taskNumber->asInt()}' not found. Last task id is '{$tasksCount}'.");
}

$event = $flattenEvents[$taskNumber->asArrayIndex()];

$schedule = new Schedule();
$schedule->events([$event]);

return [$schedule];
}
}
1 change: 1 addition & 0 deletions src/Task/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function all($source)
$iterator = $this->finder
->files()
->name("*{$suffix}")
->sortByName()
->in($source)
;

Expand Down
64 changes: 64 additions & 0 deletions src/Task/TaskNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Crunz\Task;

use Crunz\Exception\WrongTaskNumberException;

class TaskNumber
{
const MIN_VALUE = 1;
/** @var int */
private $number;

/**
* @throws WrongTaskNumberException
*
* @param $number int
*/
private function __construct($number)
{
if ($number < self::MIN_VALUE) {
throw new WrongTaskNumberException('Passed task number must be greater or equal to 1.');
}

$this->number = $number;
}

/**
* @param $value string
*
* @return TaskNumber
*
* @throws WrongTaskNumberException
*/
public static function fromString($value)
{
if (!\is_string($value)) {
throw new WrongTaskNumberException('Passed task number is not string.');
}

if (!\is_numeric($value)) {
throw new WrongTaskNumberException("Task number '{$value}' is not numeric.");
}

$number = (int) $value;

return new self($number);
}

/**
* @return int
*/
public function asInt()
{
return $this->number;
}

/**
* @return int
*/
public function asArrayIndex()
{
return $this->number - 1;
}
}
Loading