-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Adding failure transport support #30970
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
src/Symfony/Component/Messenger/Command/AbstractFailedMessagesCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?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\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Dumper; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Stamp\SentToFailureTransportStamp; | ||
use Symfony\Component\Messenger\Stamp\TransportMessageIdStamp; | ||
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface; | ||
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; | ||
|
||
/** | ||
* @author Ryan Weaver <ryan@symfonycasts.com> | ||
* | ||
* @internal | ||
* @experimental in 4.3 | ||
*/ | ||
abstract class AbstractFailedMessagesCommand extends Command | ||
weaverryan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private $receiverName; | ||
private $receiver; | ||
|
||
public function __construct(string $receiverName, ReceiverInterface $receiver) | ||
{ | ||
$this->receiverName = $receiverName; | ||
$this->receiver = $receiver; | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function getReceiverName() | ||
{ | ||
return $this->receiverName; | ||
} | ||
|
||
/** | ||
* @return mixed|null | ||
*/ | ||
protected function getMessageId(Envelope $envelope) | ||
{ | ||
/** @var TransportMessageIdStamp $stamp */ | ||
$stamp = $envelope->last(TransportMessageIdStamp::class); | ||
|
||
return null !== $stamp ? $stamp->getId() : null; | ||
} | ||
|
||
protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io) | ||
{ | ||
$io->title('Failed Message Details'); | ||
|
||
/** @var SentToFailureTransportStamp $sentToFailureTransportStamp */ | ||
$sentToFailureTransportStamp = $envelope->last(SentToFailureTransportStamp::class); | ||
|
||
$rows = [ | ||
['Class', \get_class($envelope->getMessage())], | ||
]; | ||
|
||
if (null !== $id = $this->getMessageId($envelope)) { | ||
$rows[] = ['Message Id', $id]; | ||
} | ||
|
||
if (null === $sentToFailureTransportStamp) { | ||
$io->warning('Message does not appear to have been sent to this transport after failing'); | ||
} else { | ||
$rows = array_merge($rows, [ | ||
['Failed at', $sentToFailureTransportStamp->getSentAt()->format('Y-m-d H:i:s')], | ||
['Error', $sentToFailureTransportStamp->getExceptionMessage()], | ||
['Error Class', $sentToFailureTransportStamp->getFlattenException() ? $sentToFailureTransportStamp->getFlattenException()->getClass() : '(unknown)'], | ||
['Transport', $sentToFailureTransportStamp->getOriginalReceiverName()], | ||
]); | ||
} | ||
|
||
$io->table([], $rows); | ||
|
||
if ($io->isVeryVerbose()) { | ||
$io->title('Message:'); | ||
$dump = new Dumper($io); | ||
$io->writeln($dump($envelope->getMessage())); | ||
$io->title('Exception:'); | ||
$io->writeln($sentToFailureTransportStamp->getFlattenException()->getTraceAsString()); | ||
} else { | ||
$io->writeln(' Re-run command with <info>-vv</info> to see more message & error details.'); | ||
} | ||
} | ||
|
||
protected function printPendingMessagesMessage(ReceiverInterface $receiver, SymfonyStyle $io) | ||
{ | ||
if ($receiver instanceof MessageCountAwareInterface) { | ||
if (1 === $receiver->getMessageCount()) { | ||
$io->writeln('There is <comment>1</comment> message pending in the failure transport.'); | ||
} else { | ||
$io->writeln(sprintf('There are <comment>%d</comment> messages pending in the failure transport.', $receiver->getMessageCount())); | ||
} | ||
} | ||
} | ||
|
||
protected function getReceiver(): ReceiverInterface | ||
{ | ||
return $this->receiver; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
src/Symfony/Component/Messenger/Command/FailedMessagesRemoveCommand.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?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\Command; | ||
|
||
use Symfony\Component\Console\Exception\RuntimeException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface; | ||
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; | ||
|
||
/** | ||
* @author Ryan Weaver <ryan@symfonycasts.com> | ||
* | ||
* @experimental in 4.3 | ||
*/ | ||
class FailedMessagesRemoveCommand extends AbstractFailedMessagesCommand | ||
{ | ||
protected static $defaultName = 'messenger:failed:remove'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setDefinition([ | ||
new InputArgument('id', InputArgument::REQUIRED, 'Specific message id to remove'), | ||
new InputOption('force', null, InputOption::VALUE_NONE, 'Force the operation without confirmation'), | ||
]) | ||
->setDescription('Remove a message from the failure transport.') | ||
->setHelp(<<<'EOF' | ||
The <info>%command.name%</info> removes a message that is pending in the failure transport. | ||
|
||
<info>php %command.full_name% {id}</info> | ||
|
||
The specific id can be found via the messenger:failed:show command. | ||
EOF | ||
) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); | ||
|
||
$receiver = $this->getReceiver(); | ||
|
||
$shouldForce = $input->getOption('force'); | ||
$this->removeSingleMessage($input->getArgument('id'), $receiver, $io, $shouldForce); | ||
} | ||
|
||
private function removeSingleMessage($id, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce) | ||
{ | ||
if (!$receiver instanceof ListableReceiverInterface) { | ||
throw new RuntimeException(sprintf('The "%s" receiver does not support removing specific messages.', $this->getReceiverName())); | ||
} | ||
|
||
$envelope = $receiver->find($id); | ||
if (null === $envelope) { | ||
throw new RuntimeException(sprintf('The message with id "%s" was not found.', $id)); | ||
} | ||
$this->displaySingleMessage($envelope, $io); | ||
|
||
if ($shouldForce || $io->confirm('Do you want to permanently remove this message?', false)) { | ||
$receiver->reject($envelope); | ||
|
||
$io->success('Message removed.'); | ||
} else { | ||
$io->note('Message not removed.'); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.