Skip to content

Commit 0c6d6e1

Browse files
[Messenger] Add the --all option to the messenger:failed:remove command
1 parent 2bac801 commit 0c6d6e1

File tree

3 files changed

+142
-7
lines changed

3 files changed

+142
-7
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Deprecate `StopWorkerOnSignalsListener` in favor of using the `SignalableCommandInterface`
88
* Add `HandlerDescriptor::getOptions`
9+
* Add the `--all` option to the `messenger:failed:remove` command
910

1011
6.3
1112
---

src/Symfony/Component/Messenger/Command/FailedMessagesRemoveCommand.php

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
22+
use Symfony\Component\Messenger\Envelope;
2223
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
2324
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
2425

@@ -32,7 +33,8 @@ protected function configure(): void
3233
{
3334
$this
3435
->setDefinition([
35-
new InputArgument('id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
36+
new InputArgument('id', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
37+
new InputOption('all', null, InputOption::VALUE_NONE, 'Remove all failed messages from the transport'),
3638
new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
3739
new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
3840
new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
@@ -43,6 +45,10 @@ protected function configure(): void
4345
<info>php %command.full_name% {id1} [{id2} ...]</info>
4446
4547
The specific ids can be found via the messenger:failed:show command.
48+
49+
It is possible to remove all messages from the failure transport by using the "--all" option.
50+
51+
<info>php %command.full_name% --all --force</info>
4652
EOF
4753
)
4854
;
@@ -60,19 +66,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6066
$receiver = $this->getReceiver($failureTransportName);
6167

6268
$shouldForce = $input->getOption('force');
69+
6370
$ids = (array) $input->getArgument('id');
64-
$shouldDisplayMessages = $input->getOption('show-messages') || 1 === \count($ids);
65-
$this->removeMessages($failureTransportName, $ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
71+
$shouldDeleteAllMessages = $input->getOption('all');
6672

67-
return 0;
68-
}
73+
$idsCount = \count($ids);
74+
if (false === $shouldDeleteAllMessages && 0 === $idsCount) {
75+
throw new RuntimeException('Message ids must be specified. If you want to remove all messages, use the "--all" option.');
76+
} elseif (true === $shouldDeleteAllMessages && 0 !== $idsCount) {
77+
throw new RuntimeException('You cannot specify message ids when using the "--all" option.');
78+
}
79+
80+
if ($shouldDeleteAllMessages && !$shouldForce) {
81+
throw new RuntimeException('You must use the "--force" option when using "--all" to confirm the removal of all failed messages in the transport.');
82+
}
83+
84+
$shouldDisplayMessages = $input->getOption('show-messages') || 1 === $idsCount;
6985

70-
private function removeMessages(string $failureTransportName, array $ids, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
71-
{
7286
if (!$receiver instanceof ListableReceiverInterface) {
7387
throw new RuntimeException(sprintf('The "%s" receiver does not support removing specific messages.', $failureTransportName));
7488
}
7589

90+
if ($shouldDeleteAllMessages) {
91+
$this->removeEnvelopes($receiver->all(), $receiver, $io, $shouldDisplayMessages);
92+
} else {
93+
$this->removeMessagesById($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
94+
}
95+
96+
return 0;
97+
}
98+
99+
private function removeMessagesById(array $ids, ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
100+
{
76101
foreach ($ids as $id) {
77102
$this->phpSerializer?->acceptPhpIncompleteClass();
78103
try {
@@ -99,4 +124,22 @@ private function removeMessages(string $failureTransportName, array $ids, Receiv
99124
}
100125
}
101126
}
127+
128+
/**
129+
* @param iterable<array-key, Envelope> $envelopes
130+
*/
131+
private function removeEnvelopes(iterable $envelopes, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldDisplayMessages): void
132+
{
133+
$count = 0;
134+
foreach ($envelopes as $envelope) {
135+
if ($shouldDisplayMessages) {
136+
$this->displaySingleMessage($envelope, $io);
137+
}
138+
139+
$receiver->reject($envelope);
140+
++$count;
141+
}
142+
143+
$io->note(sprintf('%d messages were removed.', $count));
144+
}
102145
}

src/Symfony/Component/Messenger/Tests/Command/FailedMessagesRemoveCommandTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Tests\Command;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Exception\RuntimeException;
1516
use Symfony\Component\Console\Tester\CommandCompletionTester;
1617
use Symfony\Component\Console\Tester\CommandTester;
1718
use Symfony\Component\DependencyInjection\ServiceLocator;
@@ -253,4 +254,94 @@ public function testCompleteIdWithSpecifiedTransport()
253254

254255
$this->assertSame(['2ab50dfa1fbf', '78c2da843723'], $suggestions);
255256
}
257+
258+
public function testOptionAllIsSetWithIdsThrows()
259+
{
260+
$globalFailureReceiverName = 'failure_receiver';
261+
262+
$serviceLocator = $this->createMock(ServiceLocator::class);
263+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
264+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($this->createMock(ListableReceiverInterface::class));
265+
266+
$command = new FailedMessagesRemoveCommand(
267+
'failure_receiver',
268+
$serviceLocator
269+
);
270+
271+
$tester = new CommandTester($command);
272+
273+
$this->expectException(RuntimeException::class);
274+
$this->expectExceptionMessage('You cannot specify message ids when using the "--all" option.');
275+
$tester->execute(['id' => [20], '--all' => true]);
276+
}
277+
278+
public function testOptionAllIsSetWithoutForceThrows()
279+
{
280+
$globalFailureReceiverName = 'failure_receiver';
281+
282+
$serviceLocator = $this->createMock(ServiceLocator::class);
283+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
284+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($this->createMock(ListableReceiverInterface::class));
285+
286+
$command = new FailedMessagesRemoveCommand(
287+
'failure_receiver',
288+
$serviceLocator
289+
);
290+
291+
$tester = new CommandTester($command);
292+
293+
$this->expectException(RuntimeException::class);
294+
$this->expectExceptionMessage('You must use the "--force" option when using "--all" to confirm the removal of all failed messages in the transport.');
295+
$tester->execute(['--all' => true]);
296+
}
297+
298+
public function testOptionAllIsNotSetNorIdsThrows()
299+
{
300+
$globalFailureReceiverName = 'failure_receiver';
301+
302+
$serviceLocator = $this->createMock(ServiceLocator::class);
303+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
304+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($this->createMock(ListableReceiverInterface::class));
305+
306+
$command = new FailedMessagesRemoveCommand(
307+
'failure_receiver',
308+
$serviceLocator
309+
);
310+
311+
$tester = new CommandTester($command);
312+
313+
$this->expectException(RuntimeException::class);
314+
$this->expectExceptionMessage('Message ids must be specified. If you want to remove all messages, use the "--all" option.');
315+
$tester->execute([]);
316+
}
317+
318+
public function testRemoveAllMessages()
319+
{
320+
$globalFailureReceiverName = 'failure_receiver';
321+
$receiver = $this->createMock(ListableReceiverInterface::class);
322+
323+
$series = [
324+
new Envelope(new \stdClass()),
325+
new Envelope(new \stdClass()),
326+
new Envelope(new \stdClass()),
327+
new Envelope(new \stdClass()),
328+
];
329+
330+
$receiver->expects($this->once())->method('all')->willReturn($series);
331+
332+
$serviceLocator = $this->createMock(ServiceLocator::class);
333+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
334+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($receiver);
335+
336+
$command = new FailedMessagesRemoveCommand(
337+
$globalFailureReceiverName,
338+
$serviceLocator
339+
);
340+
341+
$tester = new CommandTester($command);
342+
$tester->execute(['--all' => true, '--force' => true, '--show-messages' => true]);
343+
344+
$this->assertStringContainsString('Failed Message Details', $tester->getDisplay());
345+
$this->assertStringContainsString('4 messages were removed.', $tester->getDisplay());
346+
}
256347
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy