Skip to content

Commit

Permalink
feature #51244 [Scheduler] Add --date to schedule:debug (fabpot)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.4 branch.

Discussion
----------

[Scheduler] Add --date to schedule:debug

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | n/a
| License       | MIT
| Doc PR        | n/a

When using `schedule:debug`, it's nice to be able to see the next run date not only based on the current date, but maybe from a future date to validate that everything is well configured.

Commits
-------

df83bf4 [Scheduler] Add --date to schedule:debug
  • Loading branch information
fabpot committed Aug 3, 2023
2 parents fa3d5d4 + df83bf4 commit f956606
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/Scheduler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.4
---

* Add `--date` to `schedule:debug`
* Allow setting timezone of next run date in CronExpressionTrigger
* Add `AbstractTriggerDecorator`
* Make `ScheduledStamp` "send-able"
Expand Down
16 changes: 13 additions & 3 deletions src/Symfony/Component/Scheduler/Command/DebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected function configure(): void
{
$this
->addArgument('schedule', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, sprintf('The schedule name (one of "%s")', implode('", "', $this->scheduleNames)), null, $this->scheduleNames)
->addOption('date', null, InputArgument::OPTIONAL, 'The date to use for the next run date', 'now')
->setHelp(<<<'EOF'
The <info>%command.name%</info> lists schedules and their recurring messages:
Expand All @@ -56,6 +57,10 @@ protected function configure(): void
<info>php %command.full_name% default</info>
You can also specify a date to use for the next run date:
<info>php %command.full_name% --date=2025-10-18</info>

EOF
)
;
Expand All @@ -72,6 +77,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 2;
}

$date = new \DateTimeImmutable($input->getOption('date'));
if ('now' !== $input->getOption('date')) {
$io->comment(sprintf('All next run dates computed from %s.', $date->format(\DateTimeInterface::ATOM)));
}

foreach ($names as $name) {
$io->section($name);

Expand All @@ -84,7 +94,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
$io->table(
['Message', 'Trigger', 'Next Run'],
array_map(self::renderRecurringMessage(...), $messages),
array_map(self::renderRecurringMessage(...), $messages, array_fill(0, count($messages), $date)),
);
}

Expand All @@ -94,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/**
* @return array{0:string,1:string,2:string}
*/
private static function renderRecurringMessage(RecurringMessage $recurringMessage): array
private static function renderRecurringMessage(RecurringMessage $recurringMessage, \DateTimeImmutable $date): array
{
$message = $recurringMessage->getMessage();
$trigger = $recurringMessage->getTrigger();
Expand All @@ -117,7 +127,7 @@ private static function renderRecurringMessage(RecurringMessage $recurringMessag
return [
$messageName,
$triggerName,
$recurringMessage->getTrigger()->getNextRunDate(now())?->format(\DateTimeInterface::ATOM) ?? '-',
$recurringMessage->getTrigger()->getNextRunDate($date)?->format(\DateTimeInterface::ATOM) ?? '-',
];
}
}

0 comments on commit f956606

Please sign in to comment.