FailedMessagesShowCommand.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Exception\RuntimeException;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\ConsoleOutputInterface;
  16. use Symfony\Component\Console\Output\OutputInterface;
  17. use Symfony\Component\Console\Style\SymfonyStyle;
  18. use Symfony\Component\Messenger\Stamp\ErrorDetailsStamp;
  19. use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
  20. use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
  21. /**
  22. * @author Ryan Weaver <ryan@symfonycasts.com>
  23. */
  24. class FailedMessagesShowCommand extends AbstractFailedMessagesCommand
  25. {
  26. protected static $defaultName = 'messenger:failed:show';
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function configure(): void
  31. {
  32. $this
  33. ->setDefinition([
  34. new InputArgument('id', InputArgument::OPTIONAL, 'Specific message id to show'),
  35. new InputOption('max', null, InputOption::VALUE_REQUIRED, 'Maximum number of messages to list', 50),
  36. ])
  37. ->setDescription('Show one or more messages from the failure transport')
  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. /**
  48. * {@inheritdoc}
  49. */
  50. protected function execute(InputInterface $input, OutputInterface $output)
  51. {
  52. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  53. $receiver = $this->getReceiver();
  54. $this->printPendingMessagesMessage($receiver, $io);
  55. if (!$receiver instanceof ListableReceiverInterface) {
  56. throw new RuntimeException(sprintf('The "%s" receiver does not support listing or showing specific messages.', $this->getReceiverName()));
  57. }
  58. if (null === $id = $input->getArgument('id')) {
  59. $this->listMessages($io, $input->getOption('max'));
  60. } else {
  61. $this->showMessage($id, $io);
  62. }
  63. return 0;
  64. }
  65. private function listMessages(SymfonyStyle $io, int $max)
  66. {
  67. /** @var ListableReceiverInterface $receiver */
  68. $receiver = $this->getReceiver();
  69. $envelopes = $receiver->all($max);
  70. $rows = [];
  71. foreach ($envelopes as $envelope) {
  72. /** @var RedeliveryStamp|null $lastRedeliveryStamp */
  73. $lastRedeliveryStamp = $envelope->last(RedeliveryStamp::class);
  74. /** @var ErrorDetailsStamp|null $lastErrorDetailsStamp */
  75. $lastErrorDetailsStamp = $envelope->last(ErrorDetailsStamp::class);
  76. $lastRedeliveryStampWithException = $this->getLastRedeliveryStampWithException($envelope, true);
  77. $errorMessage = '';
  78. if (null !== $lastErrorDetailsStamp) {
  79. $errorMessage = $lastErrorDetailsStamp->getExceptionMessage();
  80. } elseif (null !== $lastRedeliveryStampWithException) {
  81. // Try reading the errorMessage for messages that are still in the queue without the new ErrorDetailStamps.
  82. $errorMessage = $lastRedeliveryStampWithException->getExceptionMessage();
  83. }
  84. $rows[] = [
  85. $this->getMessageId($envelope),
  86. \get_class($envelope->getMessage()),
  87. null === $lastRedeliveryStamp ? '' : $lastRedeliveryStamp->getRedeliveredAt()->format('Y-m-d H:i:s'),
  88. $errorMessage,
  89. ];
  90. }
  91. if (0 === \count($rows)) {
  92. $io->success('No failed messages were found.');
  93. return;
  94. }
  95. $io->table(['Id', 'Class', 'Failed at', 'Error'], $rows);
  96. if (\count($rows) === $max) {
  97. $io->comment(sprintf('Showing first %d messages.', $max));
  98. }
  99. $io->comment('Run <comment>messenger:failed:show {id} -vv</comment> to see message details.');
  100. }
  101. private function showMessage(string $id, SymfonyStyle $io)
  102. {
  103. /** @var ListableReceiverInterface $receiver */
  104. $receiver = $this->getReceiver();
  105. $envelope = $receiver->find($id);
  106. if (null === $envelope) {
  107. throw new RuntimeException(sprintf('The message "%s" was not found.', $id));
  108. }
  109. $this->displaySingleMessage($envelope, $io);
  110. $io->writeln([
  111. '',
  112. sprintf(' Run <comment>messenger:failed:retry %s</comment> to retry this message.', $id),
  113. sprintf(' Run <comment>messenger:failed:remove %s</comment> to delete it.', $id),
  114. ]);
  115. }
  116. }