FailedMessagesShowCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. protected static $defaultDescription = 'Show one or more messages from the failure transport';
  28. /**
  29. * {@inheritdoc}
  30. */
  31. protected function configure(): void
  32. {
  33. $this
  34. ->setDefinition([
  35. new InputArgument('id', InputArgument::OPTIONAL, 'Specific message id to show'),
  36. new InputOption('max', null, InputOption::VALUE_REQUIRED, 'Maximum number of messages to list', 50),
  37. new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
  38. ])
  39. ->setDescription(self::$defaultDescription)
  40. ->setHelp(<<<'EOF'
  41. The <info>%command.name%</info> shows message that are pending in the failure transport.
  42. <info>php %command.full_name%</info>
  43. Or look at a specific message by its id:
  44. <info>php %command.full_name% {id}</info>
  45. EOF
  46. )
  47. ;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. protected function execute(InputInterface $input, OutputInterface $output)
  53. {
  54. $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
  55. $failureTransportName = $input->getOption('transport');
  56. if (self::DEFAULT_TRANSPORT_OPTION === $failureTransportName) {
  57. $this->printWarningAvailableFailureTransports($io, $this->getGlobalFailureReceiverName());
  58. }
  59. if ('' === $failureTransportName || null === $failureTransportName) {
  60. $failureTransportName = $this->interactiveChooseFailureTransport($io);
  61. }
  62. $failureTransportName = self::DEFAULT_TRANSPORT_OPTION === $failureTransportName ? $this->getGlobalFailureReceiverName() : $failureTransportName;
  63. $receiver = $this->getReceiver($failureTransportName);
  64. $this->printPendingMessagesMessage($receiver, $io);
  65. if (!$receiver instanceof ListableReceiverInterface) {
  66. throw new RuntimeException(sprintf('The "%s" receiver does not support listing or showing specific messages.', $failureTransportName));
  67. }
  68. if (null === $id = $input->getArgument('id')) {
  69. $this->listMessages($failureTransportName, $io, $input->getOption('max'));
  70. } else {
  71. $this->showMessage($failureTransportName, $id, $io);
  72. }
  73. return 0;
  74. }
  75. private function listMessages(?string $failedTransportName, SymfonyStyle $io, int $max)
  76. {
  77. /** @var ListableReceiverInterface $receiver */
  78. $receiver = $this->getReceiver($failedTransportName);
  79. $envelopes = $receiver->all($max);
  80. $rows = [];
  81. foreach ($envelopes as $envelope) {
  82. /** @var RedeliveryStamp|null $lastRedeliveryStamp */
  83. $lastRedeliveryStamp = $envelope->last(RedeliveryStamp::class);
  84. /** @var ErrorDetailsStamp|null $lastErrorDetailsStamp */
  85. $lastErrorDetailsStamp = $envelope->last(ErrorDetailsStamp::class);
  86. $lastRedeliveryStampWithException = $this->getLastRedeliveryStampWithException($envelope, true);
  87. $errorMessage = '';
  88. if (null !== $lastErrorDetailsStamp) {
  89. $errorMessage = $lastErrorDetailsStamp->getExceptionMessage();
  90. } elseif (null !== $lastRedeliveryStampWithException) {
  91. // Try reading the errorMessage for messages that are still in the queue without the new ErrorDetailStamps.
  92. $errorMessage = $lastRedeliveryStampWithException->getExceptionMessage();
  93. }
  94. $rows[] = [
  95. $this->getMessageId($envelope),
  96. \get_class($envelope->getMessage()),
  97. null === $lastRedeliveryStamp ? '' : $lastRedeliveryStamp->getRedeliveredAt()->format('Y-m-d H:i:s'),
  98. $errorMessage,
  99. ];
  100. }
  101. if (0 === \count($rows)) {
  102. $io->success('No failed messages were found.');
  103. return;
  104. }
  105. $io->table(['Id', 'Class', 'Failed at', 'Error'], $rows);
  106. if (\count($rows) === $max) {
  107. $io->comment(sprintf('Showing first %d messages.', $max));
  108. }
  109. $io->comment(sprintf('Run <comment>messenger:failed:show {id} --transport=%s -vv</comment> to see message details.', $failedTransportName));
  110. }
  111. private function showMessage(?string $failedTransportName, string $id, SymfonyStyle $io)
  112. {
  113. /** @var ListableReceiverInterface $receiver */
  114. $receiver = $this->getReceiver($failedTransportName);
  115. $envelope = $receiver->find($id);
  116. if (null === $envelope) {
  117. throw new RuntimeException(sprintf('The message "%s" was not found.', $id));
  118. }
  119. $this->displaySingleMessage($envelope, $io);
  120. $io->writeln([
  121. '',
  122. sprintf(' Run <comment>messenger:failed:retry %s --transport=%s</comment> to retry this message.', $id, $failedTransportName),
  123. sprintf(' Run <comment>messenger:failed:remove %s --transport=%s</comment> to delete it.', $id, $failedTransportName),
  124. ]);
  125. }
  126. }