From f1a8004fa3f7c472536d5d511e1efc4420644c5e Mon Sep 17 00:00:00 2001 From: Nico Haase Date: Thu, 5 Nov 2020 13:29:26 +0100 Subject: [PATCH] Show the next possible run dates when listing tasks (#20) --- .gitignore | 4 ++- .travis.yml | 2 +- Command/ListCommand.php | 34 ++++++++++++++----- Task/AbstractScheduledTask.php | 16 ++++++++- Task/TaskInterface.php | 7 ++++ Tests/Command/ListCommandTest.php | 22 ++++++++++++ .../Compiler/TaskPassTest.php | 4 +++ Tests/Task/SchedulerTest.php | 4 +++ Tests/TaskMock.php | 6 +++- composer.json | 1 + 10 files changed, 87 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index b25aefd..1313d08 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,6 @@ /web/node_modules .idea yarn-error.log -web/upload/articles \ No newline at end of file +web/upload/articles +/.phpunit.result.cache +/composer.lock diff --git a/.travis.yml b/.travis.yml index 2f33088..d3c1e27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/Command/ListCommand.php b/Command/ListCommand.php index 43a4500..56f66ee 100644 --- a/Command/ListCommand.php +++ b/Command/ListCommand.php @@ -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 */ @@ -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(); diff --git a/Task/AbstractScheduledTask.php b/Task/AbstractScheduledTask.php index 518ab41..f68498e 100644 --- a/Task/AbstractScheduledTask.php +++ b/Task/AbstractScheduledTask.php @@ -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); -} \ No newline at end of file +} diff --git a/Task/TaskInterface.php b/Task/TaskInterface.php index bd375a6..efd6f5a 100644 --- a/Task/TaskInterface.php +++ b/Task/TaskInterface.php @@ -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 */ diff --git a/Tests/Command/ListCommandTest.php b/Tests/Command/ListCommandTest.php index e4a798a..bf7ff61 100644 --- a/Tests/Command/ListCommandTest.php +++ b/Tests/Command/ListCommandTest.php @@ -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); + } } diff --git a/Tests/DependencyInjection/Compiler/TaskPassTest.php b/Tests/DependencyInjection/Compiler/TaskPassTest.php index 610115d..b940e7e 100644 --- a/Tests/DependencyInjection/Compiler/TaskPassTest.php +++ b/Tests/DependencyInjection/Compiler/TaskPassTest.php @@ -19,6 +19,10 @@ public function isDue($currentTime): bool { return true; } + public function getNextRunDates($counter): array { + return []; + } + public function run() { self::$runCount++; } diff --git a/Tests/Task/SchedulerTest.php b/Tests/Task/SchedulerTest.php index b57a8cb..895187b 100644 --- a/Tests/Task/SchedulerTest.php +++ b/Tests/Task/SchedulerTest.php @@ -22,6 +22,10 @@ public function isDue($currentTime) : bool { return $this->enable; } + public function getNextRunDates($counter): array { + return []; + } + public function run() { static::$runCount++; } diff --git a/Tests/TaskMock.php b/Tests/TaskMock.php index 9a8ceda..2f8a580 100644 --- a/Tests/TaskMock.php +++ b/Tests/TaskMock.php @@ -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++; } -} \ No newline at end of file +} diff --git a/composer.json b/composer.json index 8d167d5..36ff561 100644 --- a/composer.json +++ b/composer.json @@ -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" },