Skip to content

[Messenger] Allow to skip message in FailedMessagesRetryCommand #57270

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
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 @@ -9,6 +9,7 @@ CHANGELOG
* Add `#[AsMessage]` attribute with `$transport` parameter for message routing
* Add `--format` option to the `messenger:stats` command
* Add `getRetryDelay()` method to `RecoverableExceptionInterface`
* Add `skip` option to `messenger:failed:retry` command when run interactively to skip message and requeue it

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageSkipEvent;
use Symfony\Component\Messenger\EventListener\StopWorkerOnMessageLimitListener;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\MessageDecodingFailedStamp;
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Messenger\Transport\Receiver\SingleMessageReceiver;
Expand Down Expand Up @@ -68,8 +70,8 @@ protected function configure(): void

<info>php %command.full_name%</info>

The command will interactively ask if each message should be retried
or discarded.
The command will interactively ask if each message should be retried,
discarded or skipped.

Some transports support retrying a specific message id, which comes
from the <info>messenger:failed:show</info> command.
Expand Down Expand Up @@ -204,7 +206,8 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece

$this->forceExit = true;
try {
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry');
$choice = $io->choice('Please select an action', ['retry', 'delete', 'skip'], 'retry');
$shouldHandle = $shouldForce || 'retry' === $choice;
Comment on lines +209 to +210
Copy link
Contributor

@W0rma W0rma Apr 28, 2025

Choose a reason for hiding this comment

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

With this change the user is always asked to select an option even if --force was used.

I'm going to prepare a fix
EDIT see #60293

} finally {
$this->forceExit = false;
}
Expand All @@ -213,6 +216,10 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece
return;
}

if ('skip' === $choice) {
$this->eventDispatcher->dispatch(new WorkerMessageSkipEvent($envelope, $envelope->last(SentToFailureTransportStamp::class)->getOriginalReceiverName()));
}

$messageReceivedEvent->shouldHandle(false);
$receiver->reject($envelope);
};
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Messenger/Event/WorkerMessageSkipEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Event;

/**
* Dispatched when a message was skip.
*
* The event name is the class name.
*/
final class WorkerMessageSkipEvent extends AbstractWorkerMessageEvent
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageSkipEvent;
use Symfony\Component\Messenger\Stamp\DelayStamp;
use Symfony\Component\Messenger\Stamp\RedeliveryStamp;
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
Expand Down Expand Up @@ -65,10 +66,26 @@ public function onMessageFailed(WorkerMessageFailedEvent $event): void
$failureSender->send($envelope);
}

public function onMessageSkip(WorkerMessageSkipEvent $event): void
{
if (!$this->failureSenders->has($event->getReceiverName())) {
return;
}

$failureSender = $this->failureSenders->get($event->getReceiverName());
$envelope = $event->getEnvelope()->with(
new SentToFailureTransportStamp($event->getReceiverName()),
new DelayStamp(0),
);

$failureSender->send($envelope);
}

public static function getSubscribedEvents(): array
{
return [
WorkerMessageFailedEvent::class => ['onMessageFailed', -100],
WorkerMessageSkipEvent::class => ['onMessageSkip', -100],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Messenger\Command\FailedMessagesRetryCommand;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp;
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp;
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;

Expand Down Expand Up @@ -223,4 +224,40 @@ public function testCompleteIdWithSpecifiedTransport()

$this->assertSame(['2ab50dfa1fbf', '78c2da843723'], $suggestions);
}

public function testSkipRunWithServiceLocator()
{
$failureTransportName = 'failure_receiver';
$originalTransportName = 'original_receiver';

$serviceLocator = $this->createMock(ServiceLocator::class);
$receiver = $this->createMock(ListableReceiverInterface::class);

$dispatcher = new EventDispatcher();
$bus = $this->createMock(MessageBusInterface::class);

$serviceLocator->method('has')->willReturn(true);
$serviceLocator->method('get')->with($failureTransportName)->willReturn($receiver);

$receiver->expects($this->once())->method('find')
->willReturn(Envelope::wrap(new \stdClass(), [
new SentToFailureTransportStamp($originalTransportName)
]));

$receiver->expects($this->never())->method('ack');
$receiver->expects($this->once())->method('reject');

$command = new FailedMessagesRetryCommand(
$failureTransportName,
$serviceLocator,
$bus,
$dispatcher
);

$tester = new CommandTester($command);
$tester->setInputs(['skip']);

$tester->execute(['id' => ['10']]);
$this->assertStringContainsString('[OK]', $tester->getDisplay());
}
}
Loading