Skip to content

Commit 1ed3fea

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

File tree

3 files changed

+133
-10
lines changed

3 files changed

+133
-10
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 `--all` option to the `messenger:failed:remove` command
910

1011
6.3
1112
---

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

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\Console\Output\OutputInterface;
2121
use Symfony\Component\Console\Style\SymfonyStyle;
2222
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
23-
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
2423

2524
/**
2625
* @author Ryan Weaver <ryan@symfonycasts.com>
@@ -32,7 +31,8 @@ protected function configure(): void
3231
{
3332
$this
3433
->setDefinition([
35-
new InputArgument('id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
34+
new InputArgument('id', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
35+
new InputOption('all', null, InputOption::VALUE_NONE, 'Remove all failed messages from the transport'),
3636
new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
3737
new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
3838
new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
@@ -43,6 +43,10 @@ protected function configure(): void
4343
<info>php %command.full_name% {id1} [{id2} ...]</info>
4444
4545
The specific ids can be found via the messenger:failed:show command.
46+
47+
You can remove all messages from the failure transport by using the "--all" option (note that you also need the "--force" option):
48+
49+
<info>php %command.full_name% --all --force</info>
4650
EOF
4751
)
4852
;
@@ -61,18 +65,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6165

6266
$shouldForce = $input->getOption('force');
6367
$ids = (array) $input->getArgument('id');
64-
$shouldDisplayMessages = $input->getOption('show-messages') || 1 === \count($ids);
65-
$this->removeMessages($failureTransportName, $ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
68+
$shouldDeleteAllMessages = $input->getOption('all');
6669

67-
return 0;
68-
}
70+
$idsCount = \count($ids);
71+
if (!$shouldDeleteAllMessages && !$idsCount) {
72+
throw new RuntimeException('Please specify at least one message id. If you want to remove all messages, use the "--all" option.');
73+
} elseif ($shouldDeleteAllMessages && $idsCount) {
74+
throw new RuntimeException('You cannot specify message ids when using the "--all" option.');
75+
}
76+
77+
$shouldDisplayMessages = $input->getOption('show-messages') || 1 === $idsCount;
6978

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

83+
if ($shouldDeleteAllMessages) {
84+
$this->removeAllMessages($receiver, $io, $shouldForce, $shouldDisplayMessages, $failureTransportName);
85+
} else {
86+
$this->removeMessagesById($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages, $failureTransportName);
87+
}
88+
89+
return 0;
90+
}
91+
92+
private function removeMessagesById(array $ids, ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages, string $failureTransportName): void
93+
{
7694
foreach ($ids as $id) {
7795
$this->phpSerializer?->acceptPhpIncompleteClass();
7896
try {
@@ -99,4 +117,26 @@ private function removeMessages(string $failureTransportName, array $ids, Receiv
99117
}
100118
}
101119
}
120+
121+
private function removeAllMessages(ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages, string $failureTransportName): void
122+
{
123+
$count = 0;
124+
foreach ($receiver->all() as $envelope) {
125+
if ($shouldDisplayMessages) {
126+
$this->displaySingleMessage($envelope, $io);
127+
}
128+
129+
if ($shouldForce) {
130+
$receiver->reject($envelope);
131+
}
132+
133+
++$count;
134+
}
135+
136+
if ($shouldForce) {
137+
$io->note(sprintf('%d messages were removed.', $count));
138+
} else {
139+
$io->note(sprintf('%d messages found in the "%s" transport, use the "--force" option to remove them.', $count, $failureTransportName));
140+
}
141+
}
102142
}

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

Lines changed: 84 additions & 2 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;
@@ -207,7 +208,7 @@ public function testCompleteId()
207208
$globalFailureReceiverName = 'failure_receiver';
208209

209210
$receiver = $this->createMock(ListableReceiverInterface::class);
210-
$receiver->expects($this->once())->method('all')->with(50)->willReturn([
211+
$receiver->expects($this->once())->method('all')->willReturn([
211212
Envelope::wrap(new \stdClass(), [new TransportMessageIdStamp('2ab50dfa1fbf')]),
212213
Envelope::wrap(new \stdClass(), [new TransportMessageIdStamp('78c2da843723')]),
213214
]);
@@ -233,7 +234,7 @@ public function testCompleteIdWithSpecifiedTransport()
233234
$anotherFailureReceiverName = 'another_receiver';
234235

235236
$receiver = $this->createMock(ListableReceiverInterface::class);
236-
$receiver->expects($this->once())->method('all')->with(50)->willReturn([
237+
$receiver->expects($this->once())->method('all')->willReturn([
237238
Envelope::wrap(new \stdClass(), [new TransportMessageIdStamp('2ab50dfa1fbf')]),
238239
Envelope::wrap(new \stdClass(), [new TransportMessageIdStamp('78c2da843723')]),
239240
]);
@@ -253,4 +254,85 @@ 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('failure_receiver', $serviceLocator);
267+
$tester = new CommandTester($command);
268+
269+
$this->expectException(RuntimeException::class);
270+
$this->expectExceptionMessage('You cannot specify message ids when using the "--all" option.');
271+
$tester->execute(['id' => [20], '--all' => true]);
272+
}
273+
274+
public function testOptionAllIsSetWithoutForceThrows()
275+
{
276+
$globalFailureReceiverName = 'failure_receiver';
277+
278+
$receiver = $this->createMock(ListableReceiverInterface::class);
279+
$receiver->expects($this->once())->method('all')->willReturn([
280+
Envelope::wrap(new \stdClass(), [new TransportMessageIdStamp('2ab50dfa1fbf')]),
281+
Envelope::wrap(new \stdClass(), [new TransportMessageIdStamp('78c2da843723')]),
282+
]);
283+
284+
$serviceLocator = $this->createMock(ServiceLocator::class);
285+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
286+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($receiver);
287+
288+
$command = new FailedMessagesRemoveCommand('failure_receiver', $serviceLocator);
289+
$tester = new CommandTester($command);
290+
291+
$tester->execute(['--all' => true]);
292+
293+
$this->assertSame(0, $tester->getStatusCode());
294+
$this->assertStringContainsString('2 messages found in the "failure_receiver" transport, use the "--force" option to remove them.', $tester->getDisplay());
295+
}
296+
297+
public function testOptionAllIsNotSetNorIdsThrows()
298+
{
299+
$globalFailureReceiverName = 'failure_receiver';
300+
301+
$serviceLocator = $this->createMock(ServiceLocator::class);
302+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
303+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($this->createMock(ListableReceiverInterface::class));
304+
305+
$command = new FailedMessagesRemoveCommand('failure_receiver', $serviceLocator);
306+
$tester = new CommandTester($command);
307+
308+
$this->expectException(RuntimeException::class);
309+
$this->expectExceptionMessage('Please specify at least one message id. If you want to remove all messages, use the "--all" option.');
310+
$tester->execute([]);
311+
}
312+
313+
public function testRemoveAllMessages()
314+
{
315+
$globalFailureReceiverName = 'failure_receiver';
316+
$receiver = $this->createMock(ListableReceiverInterface::class);
317+
318+
$series = [
319+
new Envelope(new \stdClass()),
320+
new Envelope(new \stdClass()),
321+
new Envelope(new \stdClass()),
322+
new Envelope(new \stdClass()),
323+
];
324+
325+
$receiver->expects($this->once())->method('all')->willReturn($series);
326+
327+
$serviceLocator = $this->createMock(ServiceLocator::class);
328+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
329+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($receiver);
330+
331+
$command = new FailedMessagesRemoveCommand($globalFailureReceiverName, $serviceLocator);
332+
$tester = new CommandTester($command);
333+
$tester->execute(['--all' => true, '--force' => true, '--show-messages' => true]);
334+
335+
$this->assertStringContainsString('Failed Message Details', $tester->getDisplay());
336+
$this->assertStringContainsString('4 messages were removed.', $tester->getDisplay());
337+
}
256338
}

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