vendor/symfony/messenger/Command/FailedMessagesShowCommand.php line 87

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Messenger\Command;
  11. use Symfony\Component\Console\Attribute\AsCommand;
  12. use Symfony\Component\Console\Exception\RuntimeException;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  17. use Symfony\Component\Console\Output\OutputInterface;
  18. use Symfony\Component\Console\Style\SymfonyStyle;
  19. use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
  20. use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
  21. use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
  22. /**
  23.  * @author Ryan Weaver <ryan@symfonycasts.com>
  24.  */
  25. #[AsCommand(name: 'messenger:failed:show', description: 'Show one or more messages from the failure transport')]
  26. class FailedMessagesShowCommand extends AbstractFailedMessagesCommand
  27. {
  28.     protected function configure(): void
  29.     {
  30.         $this
  31.             ->setDefinition([
  32.                 new InputArgument('id', InputArgument::OPTIONAL, 'Specific message id to show'),
  33.                 new InputOption('max', null, InputOption::VALUE_REQUIRED, 'Maximum number of messages to list', 50),
  34.                 new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
  35.                 new InputOption('stats', null, InputOption::VALUE_NONE, 'Display the message count by class'),
  36.                 new InputOption('class-filter', null, InputOption::VALUE_REQUIRED, 'Filter by a specific class name'),
  37.             ])
  38.             ->setHelp(<<<'EOF'
  39. The <info>%command.name%</info> shows message that are pending in the failure transport.
  40.     <info>php %command.full_name%</info>
  41. Or look at a specific message by its id:
  42.     <info>php %command.full_name% {id}</info>
  43. EOF
  44.             )
  45.         ;
  46.     }
  47.     protected function execute(InputInterface $input, OutputInterface $output): int
  48.     {
  49.         $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  50.         $failureTransportName = $input->getOption('transport');
  51.         if (self::DEFAULT_TRANSPORT_OPTION === $failureTransportName) {
  52.             $this->printWarningAvailableFailureTransports($io, $this->getGlobalFailureReceiverName());
  53.         }
  54.         if ('' === $failureTransportName || null === $failureTransportName) {
  55.             $failureTransportName = $this->interactiveChooseFailureTransport($io);
  56.         }
  57.         $failureTransportName = self::DEFAULT_TRANSPORT_OPTION === $failureTransportName ? $this->getGlobalFailureReceiverName() : $failureTransportName;
  58.         $receiver = $this->getReceiver($failureTransportName);
  59.         $this->printPendingMessagesMessage($receiver, $io);
  60.         if (!$receiver instanceof ListableReceiverInterface) {
  61.             throw new RuntimeException(sprintf('The "%s" receiver does not support listing or showing specific messages.', $failureTransportName));
  62.         }
  63.         if ($input->getOption('stats')) {
  64.             $this->listMessagesPerClass($failureTransportName, $io, $input->getOption('max'));
  65.         } elseif (null === $id = $input->getArgument('id')) {
  66.             $this->listMessages($failureTransportName, $io, $input->getOption('max'), $input->getOption('class-filter'));
  67.         } else {
  68.             $this->showMessage($failureTransportName, $id, $io);
  69.         }
  70.         return 0;
  71.     }
  72.     private function listMessages(?string $failedTransportName, SymfonyStyle $io, int $max, string $classFilter = null)
  73.     {
  74.         /** @var ListableReceiverInterface $receiver */
  75.         $receiver = $this->getReceiver($failedTransportName);
  76.         $envelopes = $receiver->all($max);
  77.         $rows = [];
  78.         if ($classFilter) {
  79.             $io->comment(sprintf('Displaying only \'%s\' messages', $classFilter));
  80.         }
  81.         $this->phpSerializer?->acceptPhpIncompleteClass();
  82.         try {
  83.             foreach ($envelopes as $envelope) {
  84.                 $currentClassName = \get_class($envelope->getMessage());
  85.                 if ($classFilter && $classFilter !== $currentClassName) {
  86.                     continue;
  87.                 }
  88.                 /** @var RedeliveryStamp|null $lastRedeliveryStamp */
  89.                 $lastRedeliveryStamp = $envelope->last(RedeliveryStamp::class);
  90.                 /** @var ErrorDetailsStamp|null $lastErrorDetailsStamp */
  91.                 $lastErrorDetailsStamp = $envelope->last(ErrorDetailsStamp::class);
  92.                 $rows[] = [
  93.                     $this->getMessageId($envelope),
  94.                     $currentClassName,
  95.                     null === $lastRedeliveryStamp ? '' : $lastRedeliveryStamp->getRedeliveredAt()->format('Y-m-d H:i:s'),
  96.                     $lastErrorDetailsStamp?->getExceptionMessage() ?? '',
  97.                 ];
  98.             }
  99.         } finally {
  100.             $this->phpSerializer?->rejectPhpIncompleteClass();
  101.         }
  102.         $rowsCount = \count($rows);
  103.         if (0 === $rowsCount) {
  104.             $io->success('No failed messages were found.');
  105.             return;
  106.         }
  107.         $io->table(['Id', 'Class', 'Failed at', 'Error'], $rows);
  108.         if ($rowsCount === $max) {
  109.             $io->comment(sprintf('Showing first %d messages.', $max));
  110.         } elseif ($classFilter) {
  111.             $io->comment(sprintf('Showing %d message(s).', $rowsCount));
  112.         }
  113.         $io->comment(sprintf('Run <comment>messenger:failed:show {id} --transport=%s -vv</comment> to see message details.', $failedTransportName));
  114.     }
  115.     private function listMessagesPerClass(?string $failedTransportName, SymfonyStyle $io, int $max)
  116.     {
  117.         /** @var ListableReceiverInterface $receiver */
  118.         $receiver = $this->getReceiver($failedTransportName);
  119.         $envelopes = $receiver->all($max);
  120.         $countPerClass = [];
  121.         $this->phpSerializer?->acceptPhpIncompleteClass();
  122.         try {
  123.             foreach ($envelopes as $envelope) {
  124.                 $c = \get_class($envelope->getMessage());
  125.                 if (!isset($countPerClass[$c])) {
  126.                     $countPerClass[$c] = [$c, 0];
  127.                 }
  128.                 ++$countPerClass[$c][1];
  129.             }
  130.         } finally {
  131.             $this->phpSerializer?->rejectPhpIncompleteClass();
  132.         }
  133.         if (0 === \count($countPerClass)) {
  134.             $io->success('No failed messages were found.');
  135.             return;
  136.         }
  137.         $io->table(['Class', 'Count'], $countPerClass);
  138.     }
  139.     private function showMessage(?string $failedTransportName, string $id, SymfonyStyle $io)
  140.     {
  141.         /** @var ListableReceiverInterface $receiver */
  142.         $receiver = $this->getReceiver($failedTransportName);
  143.         $this->phpSerializer?->acceptPhpIncompleteClass();
  144.         try {
  145.             $envelope = $receiver->find($id);
  146.         } finally {
  147.             $this->phpSerializer?->rejectPhpIncompleteClass();
  148.         }
  149.         if (null === $envelope) {
  150.             throw new RuntimeException(sprintf('The message "%s" was not found.', $id));
  151.         }
  152.         $this->displaySingleMessage($envelope, $io);
  153.         $io->writeln([
  154.             '',
  155.             sprintf(' Run <comment>messenger:failed:retry %s --transport=%s</comment> to retry this message.', $id, $failedTransportName),
  156.             sprintf(' Run <comment>messenger:failed:remove %s --transport=%s</comment> to delete it.', $id, $failedTransportName),
  157.         ]);
  158.     }
  159. }