-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #49795 add
debug:scheduler
command (kbond)
This PR was squashed before being merged into the 6.3 branch. Discussion ---------- add `debug:scheduler` command | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | todo Example output: ``` Scheduler ========= default ------- ---------------------- ------------------- --------------------------- Message Trigger Next Run ---------------------- ------------------- --------------------------- stdClass cron: 10 10 5 2 0 2024-02-04T10:10:00-05:00 generate user report cron: 0 0 * * * 2023-03-25T00:00:00-04:00 ---------------------- ------------------- --------------------------- ``` The output is helped if the trigger and message implement `\Stringable`. I've made `CronExpressionTrigger` stringable but I think we should have all the packaged triggers implement. Commits ------- 398e2b2 add `debug:scheduler` command
- Loading branch information
Showing
4 changed files
with
115 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Scheduler\Command; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Scheduler\RecurringMessage; | ||
use Symfony\Component\Scheduler\ScheduleProviderInterface; | ||
use Symfony\Contracts\Service\ServiceProviderInterface; | ||
|
||
use function Symfony\Component\Clock\now; | ||
|
||
/** | ||
* Command to list/debug schedules. | ||
* | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
#[AsCommand(name: 'debug:scheduler', description: 'List schedules and their recurring messages')] | ||
final class DebugCommand extends Command | ||
{ | ||
private array $scheduleNames; | ||
|
||
public function __construct(private ServiceProviderInterface $schedules) | ||
{ | ||
$this->scheduleNames = array_keys($this->schedules->getProvidedServices()); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
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) | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> lists schedules and their recurring messages: | ||
<info>php %command.full_name%</info> | ||
Or for a specific schedule only: | ||
<info>php %command.full_name% default</info> | ||
|
||
EOF | ||
) | ||
; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->title('Scheduler'); | ||
|
||
$names = $input->getArgument('schedule') ?: $this->scheduleNames; | ||
|
||
foreach ($names as $name) { | ||
/** @var ScheduleProviderInterface $schedule */ | ||
$schedule = $this->schedules->get($name); | ||
|
||
$io->section($name); | ||
$io->table( | ||
['Message', 'Trigger', 'Next Run'], | ||
array_map(self::renderRecurringMessage(...), $schedule->getSchedule()->getRecurringMessages()) | ||
); | ||
} | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
/** | ||
* @return array{0:string,1:string,2:string} | ||
*/ | ||
private static function renderRecurringMessage(RecurringMessage $recurringMessage): array | ||
{ | ||
$message = $recurringMessage->getMessage(); | ||
$trigger = $recurringMessage->getTrigger(); | ||
|
||
return [ | ||
$message instanceof \Stringable ? (string) $message : (new \ReflectionClass($message))->getShortName(), | ||
$trigger instanceof \Stringable ? (string) $trigger : (new \ReflectionClass($trigger))->getShortName(), | ||
$recurringMessage->getTrigger()->getNextRunDate(now())->format(\DateTimeInterface::ATOM), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters