Skip to content

[Messenger] Add options to FailedMessagesShowCommand #39330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add `StopWorkerExceptionInterface` and its implementation `StopWorkerException` to stop the worker.
* Add the options `stats` and `class-filter` to `FailedMessagesShowCommand`

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ protected function configure(): void
new InputArgument('id', InputArgument::OPTIONAL, 'Specific message id to show'),
new InputOption('max', null, InputOption::VALUE_REQUIRED, 'Maximum number of messages to list', 50),
new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
new InputOption('stats', null, InputOption::VALUE_NONE, 'Display the message count by class'),
new InputOption('class-filter', null, InputOption::VALUE_REQUIRED, 'Filter by a specific class name'),
])
->setDescription(self::$defaultDescription)
->setHelp(<<<'EOF'
Expand Down Expand Up @@ -81,23 +83,36 @@ protected function execute(InputInterface $input, OutputInterface $output)
throw new RuntimeException(sprintf('The "%s" receiver does not support listing or showing specific messages.', $failureTransportName));
}

if (null === $id = $input->getArgument('id')) {
$this->listMessages($failureTransportName, $io, $input->getOption('max'));
if ($input->getOption('stats')) {
$this->listMessagesPerClass($failureTransportName, $io, $input->getOption('max'));
} elseif (null === $id = $input->getArgument('id')) {
$this->listMessages($failureTransportName, $io, $input->getOption('max'), $input->getOption('class-filter'));
} else {
$this->showMessage($failureTransportName, $id, $io);
}

return 0;
}

private function listMessages(?string $failedTransportName, SymfonyStyle $io, int $max)
private function listMessages(?string $failedTransportName, SymfonyStyle $io, int $max, ?string $classFilter)
{
/** @var ListableReceiverInterface $receiver */
$receiver = $this->getReceiver($failedTransportName);
$envelopes = $receiver->all($max);

$rows = [];

if ($classFilter) {
$io->comment(sprintf('Displaying only \'%s\' messages', $classFilter));
}

foreach ($envelopes as $envelope) {
$currentClassName = \get_class($envelope->getMessage());

if ($classFilter && $classFilter !== $currentClassName) {
continue;
}

/** @var RedeliveryStamp|null $lastRedeliveryStamp */
$lastRedeliveryStamp = $envelope->last(RedeliveryStamp::class);
/** @var ErrorDetailsStamp|null $lastErrorDetailsStamp */
Expand All @@ -114,27 +129,58 @@ private function listMessages(?string $failedTransportName, SymfonyStyle $io, in

$rows[] = [
$this->getMessageId($envelope),
\get_class($envelope->getMessage()),
$currentClassName,
null === $lastRedeliveryStamp ? '' : $lastRedeliveryStamp->getRedeliveredAt()->format('Y-m-d H:i:s'),
$errorMessage,
];
}

if (0 === \count($rows)) {
$rowsCount = \count($rows);

if (0 === $rowsCount) {
$io->success('No failed messages were found.');

return;
}

$io->table(['Id', 'Class', 'Failed at', 'Error'], $rows);

if (\count($rows) === $max) {
if ($rowsCount === $max) {
$io->comment(sprintf('Showing first %d messages.', $max));
} elseif ($classFilter) {
$io->comment(sprintf('Showing %d message(s).', $rowsCount));
}

$io->comment(sprintf('Run <comment>messenger:failed:show {id} --transport=%s -vv</comment> to see message details.', $failedTransportName));
}

private function listMessagesPerClass(?string $failedTransportName, SymfonyStyle $io, int $max)
{
/** @var ListableReceiverInterface $receiver */
$receiver = $this->getReceiver($failedTransportName);
$envelopes = $receiver->all($max);

$countPerClass = [];

foreach ($envelopes as $envelope) {
$c = \get_class($envelope->getMessage());

if (!isset($countPerClass[$c])) {
$countPerClass[$c] = [$c, 0];
}

++$countPerClass[$c][1];
}

if (0 === \count($countPerClass)) {
$io->success('No failed messages were found.');

return;
}

$io->table(['Class', 'Count'], $countPerClass);
}

private function showMessage(?string $failedTransportName, string $id, SymfonyStyle $io)
{
/** @var ListableReceiverInterface $receiver */
Expand Down
60 changes: 58 additions & 2 deletions src/Symfony/Component/Messenger/Tests/Command/FailedMessagesShowCommandTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function testBasicRunWithServiceLocator()
Error Things are bad!
Error Code 123
Error Class Exception
Transport async
Transport async
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra spaces should be removed

EOF
,
$redeliveryStamp->getRedeliveredAt()->format('Y-m-d H:i:s')),
Expand Down Expand Up @@ -198,7 +198,7 @@ public function testMultipleRedeliveryFailsWithServiceLocator()
Error Things are bad!
Error Code 123
Error Class Exception
Transport async
Transport async
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extra spaces should be removed

EOF
,
$redeliveryStamp2->getRedeliveredAt()->format('Y-m-d H:i:s')),
Expand Down Expand Up @@ -443,6 +443,62 @@ public function testListMessagesReturnsPaginatedMessagesWithServiceLocator()
$this->assertStringContainsString('Showing first 1 messages.', $tester->getDisplay(true));
}

public function testListMessagesReturnsFilteredByClassMessage()
{
$sentToFailureStamp = new SentToFailureTransportStamp('async');
$envelope = new Envelope(new \stdClass(), [
new TransportMessageIdStamp(15),
$sentToFailureStamp,
new RedeliveryStamp(0),
ErrorDetailsStamp::create(new \RuntimeException('Things are bad!')),
]);
$receiver = $this->createMock(ListableReceiverInterface::class);
$receiver->method('all')->with()->willReturn([$envelope]);

$failureTransportName = 'failure_receiver';
$serviceLocator = $this->createMock(ServiceLocator::class);
$serviceLocator->method('has')->with($failureTransportName)->willReturn(true);
$serviceLocator->method('get')->with($failureTransportName)->willReturn($receiver);

$command = new FailedMessagesShowCommand('failure_receiver', $serviceLocator);

$tester = new CommandTester($command);
$tester->execute([]);
$this->assertStringContainsString('Things are bad!', $tester->getDisplay(true));
$tester->execute(['--class-filter' => 'stdClass']);
$this->assertStringContainsString('Things are bad!', $tester->getDisplay(true));
$this->assertStringContainsString('Showing 1 message(s).', $tester->getDisplay(true));
$this->assertStringContainsString('Displaying only \'stdClass\' messages', $tester->getDisplay(true));

$tester->execute(['--class-filter' => 'namespace\otherClass']);
$this->assertStringContainsString('[OK] No failed messages were found.', $tester->getDisplay(true));
$this->assertStringContainsString('Displaying only \'namespace\otherClass\' messages', $tester->getDisplay(true));
}

public function testListMessagesReturnsCountByClassName()
{
$sentToFailureStamp = new SentToFailureTransportStamp('async');
$envelope = new Envelope(new \stdClass(), [
new TransportMessageIdStamp(15),
$sentToFailureStamp,
new RedeliveryStamp(0),
ErrorDetailsStamp::create(new \RuntimeException('Things are bad!')),
]);
$receiver = $this->createMock(ListableReceiverInterface::class);
$receiver->method('all')->with()->willReturn([$envelope, $envelope]);

$failureTransportName = 'failure_receiver';
$serviceLocator = $this->createMock(ServiceLocator::class);
$serviceLocator->method('has')->with($failureTransportName)->willReturn(true);
$serviceLocator->method('get')->with($failureTransportName)->willReturn($receiver);

$command = new FailedMessagesShowCommand('failure_receiver', $serviceLocator);

$tester = new CommandTester($command);
$tester->execute(['--stats' => 1]);
$this->assertStringContainsString('stdClass 2', $tester->getDisplay(true));
}

/**
* @group legacy
*/
Expand Down