Skip to content

Commit f1a8004

Browse files
authored
Show the next possible run dates when listing tasks (#20)
1 parent 47c4405 commit f1a8004

File tree

10 files changed

+87
-13
lines changed

10 files changed

+87
-13
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@
33
/web/node_modules
44
.idea
55
yarn-error.log
6-
web/upload/articles
6+
web/upload/articles
7+
/.phpunit.result.cache
8+
/composer.lock

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ cache:
88
- $HOME/.composer/cache/files
99

1010
before_install:
11-
- composer require --dev --no-update symfony/symfony:$SYMFONY_VERSION
11+
- composer require --dev --no-update symfony/framework-bundle:$SYMFONY_VERSION
1212

1313
install:
1414
- composer install

Command/ListCommand.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
use Symfony\Component\Console\Command\Command;
1111
use Symfony\Component\Console\Helper\Table;
1212
use Symfony\Component\Console\Input\InputInterface;
13+
use Symfony\Component\Console\Input\InputOption;
1314
use Symfony\Component\Console\Output\OutputInterface;
1415

1516
class ListCommand extends Command {
17+
private const NUMBER_OF_RUN_DATES = 3;
18+
1619
/**
1720
* @var Scheduler
1821
*/
@@ -28,19 +31,32 @@ protected function configure() {
2831
$this
2932
->setName("ts:list")
3033
->setDescription("List the existing tasks")
31-
->setHelp("This command display the list of registered tasks.");
34+
->setHelp("This command display the list of registered tasks.")
35+
->addOption("show-run-dates", null, InputOption::VALUE_OPTIONAL, "Show next run dates (default value: " . self::NUMBER_OF_RUN_DATES. ")", false);
3236
}
3337

3438
protected function execute(InputInterface $input, OutputInterface $output) {
39+
$numberOfRunDates = $input->getOption('show-run-dates') ?? self::NUMBER_OF_RUN_DATES;
40+
$showRunDates = $numberOfRunDates !== false;
41+
3542
$table = new Table($output);
36-
$table->setHeaders([
37-
"ID",
38-
"Class",
39-
]);
40-
41-
$id = 1;
42-
foreach ($this->scheduler->getTasks() as $task) {
43-
$table->addRow([$id++, get_class($task)]);
43+
$tableHeaders = ["ID", "Class"];
44+
45+
if ($showRunDates) {
46+
$tableHeaders[] = "Next " . $numberOfRunDates . " run dates";
47+
}
48+
49+
$table->setHeaders($tableHeaders);
50+
51+
foreach ($this->scheduler->getTasks() as $id => $task) {
52+
$row = [($id + 1), get_class($task)];
53+
54+
if ($showRunDates) {
55+
$nextRunDates = $task->getNextRunDates($numberOfRunDates);
56+
$row[] = implode(', ', $nextRunDates);
57+
}
58+
59+
$table->addRow($row);
4460
};
4561

4662
$table->render();

Task/AbstractScheduledTask.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,19 @@ public function getSchedule() {
3434
return $this->schedule;
3535
}
3636

37+
public function getNextRunDates($counter): array {
38+
$result = [];
39+
40+
if ($counter < 1) {
41+
return $result;
42+
}
43+
44+
for ($i = 0; $i < $counter; $i++) {
45+
$result[] = $this->schedule->getCron()->getNextRunDate('now', $i)->format(DATE_ATOM);
46+
}
47+
48+
return $result;
49+
}
50+
3751
abstract protected function initialize(Schedule $schedule);
38-
}
52+
}

Task/TaskInterface.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ interface TaskInterface {
1616
*/
1717
public function isDue($currentTime) : bool;
1818

19+
/**
20+
* Get the next run dates for this job
21+
* @param int $counter
22+
* @return string[]
23+
*/
24+
public function getNextRunDates($counter) : array;
25+
1926
/**
2027
* Execute the task
2128
*/

Tests/Command/ListCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,28 @@ public function testListCommand() {
3535
]);
3636

3737
$output = $commandTester->getDisplay();
38+
$this->assertStringNotContainsString("run dates", $output);
3839
$this->assertStringContainsString("| 1 | Rewieer\TaskSchedulerBundle\Tests\TaskMock |", $output);
3940
}
41+
42+
public function testListCommandWithOption() {
43+
$container = $this->loadContainer();
44+
$scheduler = $container->get("ts.scheduler");
45+
$scheduler->addTask(new TaskMock());
46+
47+
$application = new Application();
48+
$application->add(new ListCommand($scheduler));
49+
50+
$command = $application->find("ts:list");
51+
52+
$commandTester = new CommandTester($command);
53+
$commandTester->execute([
54+
"command" => $command->getName(),
55+
"--show-run-dates" => 42,
56+
]);
57+
58+
$output = $commandTester->getDisplay();
59+
$this->assertStringContainsString("42 run dates", $output);
60+
$this->assertStringContainsString("| 1 | Rewieer\TaskSchedulerBundle\Tests\TaskMock | nextRunDate, anotherRunDate", $output);
61+
}
4062
}

Tests/DependencyInjection/Compiler/TaskPassTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function isDue($currentTime): bool {
1919
return true;
2020
}
2121

22+
public function getNextRunDates($counter): array {
23+
return [];
24+
}
25+
2226
public function run() {
2327
self::$runCount++;
2428
}

Tests/Task/SchedulerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function isDue($currentTime) : bool {
2222
return $this->enable;
2323
}
2424

25+
public function getNextRunDates($counter): array {
26+
return [];
27+
}
28+
2529
public function run() {
2630
static::$runCount++;
2731
}

Tests/TaskMock.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ public function isDue($currentTime): bool {
1818
return true;
1919
}
2020

21+
public function getNextRunDates($counter): array {
22+
return ['nextRunDate', 'anotherRunDate'];
23+
}
24+
2125
public function run() {
2226
self::$runCount++;
2327
$this->localCount++;
2428
}
25-
}
29+
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"keywords": ["cron", "task", "scheduler", "symfony", "bundle"],
1111
"require": {
1212
"php": "^7.2",
13+
"symfony/framework-bundle": "^3.4|^4.4|^5.0",
1314
"symfony/console": "^3.4|^4.4|^5.0",
1415
"dragonmantank/cron-expression": "^2.3|^3.0"
1516
},

0 commit comments

Comments
 (0)