10000 [Messenger] Add `--class-filter` option to the `messenger:failed:remove` command by arnaud-deabreu · Pull Request #59978 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] Add --class-filter option to the messenger:failed:remove command #59978

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

Merged
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 @@ -7,6 +7,7 @@ CHANGELOG
* Add `CloseableTransportInterface` to allow closing the transport
* Add `SentForRetryStamp` that identifies whether a failed message was sent for retry
* Add `Symfony\Component\Messenger\Middleware\DeduplicateMiddleware` and `Symfony\Component\Messenger\Stamp\DeduplicateStamp`
* Add `--class-filter` option to the `messenger:failed:remove` command

7.2
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function configure(): void
new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
new InputOption('class-filter', null, InputOption::VALUE_REQUIRED, 'Filter by a specific class name'),
])
->setHelp(<<<'EOF'
The <info>%command.name%</info> removes given messages that are pending in the failure transport.
Expand Down Expand Up @@ -69,6 +70,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$shouldDeleteAllMessages = $input->getOption('all');

$idsCount = \count($ids);

if (!$receiver instanceof ListableReceiverInterface) {
throw new RuntimeException(\sprintf('The "%s" receiver does not support removing specific messages.', $failureTransportName));
}

if (!$idsCount && null !== $input->getOption('class-filter')) {
$ids = $this->getMessageIdsByClassFilter($input->getOption('class-filter'), $receiver);
$idsCount = \count($ids);

if (!$idsCount) {
throw new RuntimeException('No failed messages were found with this filter.');
}

if (!$io->confirm(\sprintf('Can you confirm you want to remove %d message%s?', $idsCount, 1 === $idsCount ? '' : 's'))) {
return 0;
}
}

if (!$shouldDeleteAllMessages && !$idsCount) {
throw new RuntimeException('Please specify at least one message id. If you want to remove all failed messages, use the "--all" option.');
} elseif ($shouldDeleteAllMessages && $idsCount) {
Expand All @@ -77,10 +96,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$shouldDisplayMessages = $input->getOption('show-messages') || 1 === $idsCount;

if (!$receiver instanceof ListableReceiverInterface) {
throw new RuntimeException(\sprintf('The "%s" receiver does not support removing specific messages.', $failureTransportName));
}

if ($shouldDeleteAllMessages) {
$this->removeAllMessages($receiver, $io, $shouldForce, $shouldDisplayMessages);
} else {
Expand Down Expand Up @@ -119,6 +134,26 @@ private function removeMessagesById(array $ids, ListableReceiverInterface $recei
}
}

private function getMessageIdsByClassFilter(string $classFilter, ListableReceiverInterface $receiver): array
{
$ids = [];

$this->phpSerializer?->acceptPhpIncompleteClass();
try {
foreach ($receiver->all() as $envelope) {
if ($classFilter !== $envelope->getMessage()::class) {
continue;
}

$ids[] = $this->getMessageId($envelope);
};
} finally {
$this->phpSerializer?->rejectPhpIncompleteClass();
}

return $ids;
}

private function removeAllMessages(ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
{
if (!$shouldForce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,53 @@ public function testRemoveMultipleMessagesAndDisplayMessagesWithServiceLocator()
$this->assertStringContainsString('Message with id 30 removed.', $tester->getDisplay());
}

public function testRemoveMessagesFilteredByClassMessage()
{
$globalFailureReceiverName = 'failure_receiver';
$receiver = $this->createMock(ListableReceiverInterface::class);

$anotherClass = new class extends \stdClass {};

$series = [
new Envelope(new \stdClass(), [new TransportMessageIdStamp(10)]),
new Envelope(new $anotherClass(), [new TransportMessageIdStamp(20)]),
new Envelope(new \stdClass(), [new TransportMessageIdStamp(30)]),
];
$receiver->expects($this->once())->method('all')->willReturn($series);

$expectedRemovedIds = [10, 30];
$receiver->expects($this->exactly(2))->method('find')
->willReturnCallback(function (...$args) use ($series, &$expectedRemovedIds) {
$expectedArgs = array_shift($expectedRemovedIds);
$this->assertSame([$expectedArgs], $args);

$return = array_filter(
$series,
static fn (Envelope $envelope) => [$envelope->last(TransportMessageIdStamp::class)->getId()] === $args,
);

return current($return);
})
;

$serviceLocator = $this->createMock(ServiceLocator::class);
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($receiver);

$command = new FailedMessagesRemoveCommand(
$globalFailureReceiverName,
$serviceLocator
);

$tester = new CommandTester($command);
$tester->execute(['--class-filter' => "stdClass", '--force' => true, '--show-messages' => true]);

$this->assertStringContainsString('There is 2 messages to remove. Do you want to continue? (yes/no)', $tester->getDisplay());
$this->assertStringContainsString('Failed Message Details', $tester->getDisplay());
$this->assertStringContainsString('Message with id 10 removed.', $tester->getDisplay());
$this->assertStringContainsString('Message with id 30 removed.', $tester->getDisplay());
}

public function testCompletingTransport()
{
$globalFailureReceiverName = 'failure_receiver';
Expand Down
Loading
0