Skip to content

Commit

Permalink
Show the next possible run dates when listing tasks (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
NicoHaase authored Nov 5, 2020
1 parent 47c4405 commit f1a8004
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
/web/node_modules
.idea
yarn-error.log
web/upload/articles
web/upload/articles
/.phpunit.result.cache
/composer.lock
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cache:
- $HOME/.composer/cache/files

before_install:
- composer require --dev --no-update symfony/symfony:$SYMFONY_VERSION
- composer require --dev --no-update symfony/framework-bundle:$SYMFONY_VERSION

install:
- composer install
Expand Down
34 changes: 25 additions & 9 deletions Command/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class ListCommand extends Command {
private const NUMBER_OF_RUN_DATES = 3;

/**
* @var Scheduler
*/
Expand All @@ -28,19 +31,32 @@ protected function configure() {
$this
->setName("ts:list")
->setDescription("List the existing tasks")
->setHelp("This command display the list of registered tasks.");
->setHelp("This command display the list of registered tasks.")
->addOption("show-run-dates", null, InputOption::VALUE_OPTIONAL, "Show next run dates (default value: " . self::NUMBER_OF_RUN_DATES. ")", false);
}

protected function execute(InputInterface $input, OutputInterface $output) {
$numberOfRunDates = $input->getOption('show-run-dates') ?? self::NUMBER_OF_RUN_DATES;
$showRunDates = $numberOfRunDates !== false;

$table = new Table($output);
$table->setHeaders([
"ID",
"Class",
]);

$id = 1;
foreach ($this->scheduler->getTasks() as $task) {
$table->addRow([$id++, get_class($task)]);
$tableHeaders = ["ID", "Class"];

if ($showRunDates) {
$tableHeaders[] = "Next " . $numberOfRunDates . " run dates";
}

$table->setHeaders($tableHeaders);

foreach ($this->scheduler->getTasks() as $id => $task) {
$row = [($id + 1), get_class($task)];

if ($showRunDates) {
$nextRunDates = $task->getNextRunDates($numberOfRunDates);
$row[] = implode(', ', $nextRunDates);
}

$table->addRow($row);
};

$table->render();
Expand Down
16 changes: 15 additions & 1 deletion Task/AbstractScheduledTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,19 @@ public function getSchedule() {
return $this->schedule;
}

public function getNextRunDates($counter): array {
$result = [];

if ($counter < 1) {
return $result;
}

for ($i = 0; $i < $counter; $i++) {
$result[] = $this->schedule->getCron()->getNextRunDate('now', $i)->format(DATE_ATOM);
}

return $result;
}

abstract protected function initialize(Schedule $schedule);
}
}
7 changes: 7 additions & 0 deletions Task/TaskInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ interface TaskInterface {
*/
public function isDue($currentTime) : bool;

/**
* Get the next run dates for this job
* @param int $counter
* @return string[]
*/
public function getNextRunDates($counter) : array;

/**
* Execute the task
*/
Expand Down
22 changes: 22 additions & 0 deletions Tests/Command/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ public function testListCommand() {
]);

$output = $commandTester->getDisplay();
$this->assertStringNotContainsString("run dates", $output);
$this->assertStringContainsString("| 1 | Rewieer\TaskSchedulerBundle\Tests\TaskMock |", $output);
}

public function testListCommandWithOption() {
$container = $this->loadContainer();
$scheduler = $container->get("ts.scheduler");
$scheduler->addTask(new TaskMock());

$application = new Application();
$application->add(new ListCommand($scheduler));

$command = $application->find("ts:list");

$commandTester = new CommandTester($command);
$commandTester->execute([
"command" => $command->getName(),
"--show-run-dates" => 42,
]);

$output = $commandTester->getDisplay();
$this->assertStringContainsString("42 run dates", $output);
$this->assertStringContainsString("| 1 | Rewieer\TaskSchedulerBundle\Tests\TaskMock | nextRunDate, anotherRunDate", $output);
}
}
4 changes: 4 additions & 0 deletions Tests/DependencyInjection/Compiler/TaskPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function isDue($currentTime): bool {
return true;
}

public function getNextRunDates($counter): array {
return [];
}

public function run() {
self::$runCount++;
}
Expand Down
4 changes: 4 additions & 0 deletions Tests/Task/SchedulerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public function isDue($currentTime) : bool {
return $this->enable;
}

public function getNextRunDates($counter): array {
return [];
}

public function run() {
static::$runCount++;
}
Expand Down
6 changes: 5 additions & 1 deletion Tests/TaskMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ public function isDue($currentTime): bool {
return true;
}

public function getNextRunDates($counter): array {
return ['nextRunDate', 'anotherRunDate'];
}

public function run() {
self::$runCount++;
$this->localCount++;
}
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"keywords": ["cron", "task", "scheduler", "symfony", "bundle"],
"require": {
"php": "^7.2",
"symfony/framework-bundle": "^3.4|^4.4|^5.0",
"symfony/console": "^3.4|^4.4|^5.0",
"dragonmantank/cron-expression": "^2.3|^3.0"
},
Expand Down

0 comments on commit f1a8004

Please sign in to comment.