Skip to content

Commit dbed1a9

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

File tree

3 files changed

+126
-7
lines changed

3 files changed

+126
-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: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
namespace Symfony\Component\Messenger\Command;
1313

1414
use Symfony\Component\Console\Attribute\AsCommand;
15+
use Symfony\Component\Console\Command\Command;
1516
use Symfony\Component\Console\Exception\RuntimeException;
1617
use Symfony\Component\Console\Input\InputArgument;
1718
use Symfony\Component\Console\Input\InputInterface;
1819
use Symfony\Component\Console\Input\InputOption;
1920
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2021
use Symfony\Component\Console\Output\OutputInterface;
2122
use Symfony\Component\Console\Style\SymfonyStyle;
23+
use Symfony\Component\Messenger\Envelope;
2224
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
2325
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
2426

@@ -32,7 +34,8 @@ protected function configure(): void
3234
{
3335
$this
3436
->setDefinition([
35-
new InputArgument('id', InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
37+
new InputArgument('id', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Specific message id(s) to remove'),
38+
new InputOption('all', null, InputOption::VALUE_NONE, 'Remove all failed messages from the transport'),
3639
new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'),
3740
new InputOption('transport', null, InputOption::VALUE_OPTIONAL, 'Use a specific failure transport', self::DEFAULT_TRANSPORT_OPTION),
3841
new InputOption('show-messages', null, InputOption::VALUE_NONE, 'Display messages before removing it (if multiple ids are given)'),
@@ -43,6 +46,10 @@ protected function configure(): void
4346
<info>php %command.full_name% {id1} [{id2} ...]</info>
4447
4548
The specific ids can be found via the messenger:failed:show command.
49+
50+
You can remove all messages from the failure transport by using the "--all" option (note that you also need the "--force" option):
51+
52+
<info>php %command.full_name% --all --force</info>
4653
EOF
4754
)
4855
;
@@ -61,18 +68,38 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6168

6269
$shouldForce = $input->getOption('force');
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 (!$shouldDeleteAllMessages && !$idsCount) {
75+
throw new RuntimeException('Please specify at least one message id. If you want to remove all messages, use the "--all" option.');
76+
} elseif ($shouldDeleteAllMessages && $idsCount) {
77+
throw new RuntimeException('You cannot specify message ids when using the "--all" option.');
78+
}
79+
80+
if ($shouldDeleteAllMessages && !$shouldForce) {
81+
$io->error('Please provide the "--force" option when using "--all" to confirm the removal of all failed messages in the transport.');
82+
83+
return Command::FAILURE;
84+
}
85+
86+
$shouldDisplayMessages = $input->getOption('show-messages') || 1 === $idsCount;
6987

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

92+
if ($shouldDeleteAllMessages) {
93+
$this->removeAllMessages($receiver, $io, $shouldDisplayMessages);
94+
} else {
95+
$this->removeMessagesById($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
96+
}
97+
98+
return 0;
99+
}
100+
101+
private function removeMessagesById(array $ids, ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
102+
{
76103
foreach ($ids as $id) {
77104
$this->phpSerializer?->acceptPhpIncompleteClass();
78105
try {
@@ -99,4 +126,19 @@ private function removeMessages(string $failureTransportName, array $ids, Receiv
99126
}
100127
}
101128
}
129+
130+
private function removeAllMessages(ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldDisplayMessages): void
131+
{
132+
$count = 0;
133+
foreach ($receiver->all() as $envelope) {
134+
if ($shouldDisplayMessages) {
135+
$this->displaySingleMessage($envelope, $io);
136+
}
137+
138+
$receiver->reject($envelope);
139+
++$count;
140+
}
141+
142+
$io->note(sprintf('%d messages were removed.', $count));
143+
}
102144
}

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

Lines changed: 76 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,79 @@ 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+
$serviceLocator = $this->createMock(ServiceLocator::class);
279+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
280+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($this->createMock(ListableReceiverInterface::class));
281+
282+
$command = new FailedMessagesRemoveCommand('failure_receiver', $serviceLocator);
283+
$tester = new CommandTester($command);
284+
285+
$tester->execute(['--all' => true]);
286+
287+
$this->assertSame(1, $tester->getStatusCode());
288+
$this->assertStringContainsString('Please provide the "--force" option when using "--all" to confirm the removal', $tester->getDisplay());
289+
}
290+
291+
public function testOptionAllIsNotSetNorIdsThrows()
292+
{
293+
$globalFailureReceiverName = 'failure_receiver';
294+
295+
$serviceLocator = $this->createMock(ServiceLocator::class);
296+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
297+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($this->createMock(ListableReceiverInterface::class));
298+
299+
$command = new FailedMessagesRemoveCommand('failure_receiver', $serviceLocator);
300+
$tester = new CommandTester($command);
301+
302+
$this->expectException(RuntimeException::class);
303+
$this->expectExceptionMessage('Please specify at least one message id. If you want to remove all messages, use the "--all" option.');
304+
$tester->execute([]);
305+
}
306+
307+
public function testRemoveAllMessages()
308+
{
309+
$globalFailureReceiverName = 'failure_receiver';
310+
$receiver = $this->createMock(ListableReceiverInterface::class);
311+
312+
$series = [
313+
new Envelope(new \stdClass()),
314+
new Envelope(new \stdClass()),
315+
new Envelope(new \stdClass()),
316+
new Envelope(new \stdClass()),
317+
];
318+
319+
$receiver->expects($this->once())->method('all')->willReturn($series);
320+
321+
$serviceLocator = $this->createMock(ServiceLocator::class);
322+
$serviceLocator->expects($this->once())->method('has')->with($globalFailureReceiverName)->willReturn(true);
323+
$serviceLocator->expects($this->any())->method('get')->with($globalFailureReceiverName)->willReturn($receiver);
324+
325+
$command = new FailedMessagesRemoveCommand($globalFailureReceiverName, $serviceLocator);
326+
$tester = new CommandTester($command);
327+
$tester->execute(['--all' => true, '--force' => true, '--show-messages' => true]);
328+
329+
$this->assertStringContainsString('Failed Message Details', $tester->getDisplay());
330+
$this->assertStringContainsString('4 messages were removed.', $tester->getDisplay());
331+
}
256332
}

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