Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,10 @@ protected function getMessageId(Envelope $envelope): mixed
return $stamp?->getId();
}

protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): void
protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io, ?SymfonyStyle $errorIo = null): void
{
$errorIo ??= $io->getErrorStyle();
Copy link
Member

Choose a reason for hiding this comment

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

since $errorIo is always derived from $io->getErrorStyle(), I'd rather remove all the extra arguments added to method. That'd work, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@nicolas-grekas that would work, but creates new instances. Just tell me what you prefer, and I'll adjust the code accordingly. Thanks for the feedback.


$io->title('Failed Message Details');

/** @var SentToFailureTransportStamp|null $sentToFailureTransportStamp */
Expand All @@ -94,7 +96,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): v
}

if (null === $sentToFailureTransportStamp) {
$io->warning('Message does not appear to have been sent to this transport after failing');
$errorIo->warning('Message does not appear to have been sent to this transport after failing');
} else {
$failedAt = '';
$errorMessage = '';
Expand Down Expand Up @@ -133,7 +135,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): v
if ($io->isVeryVerbose()) {
$io->title('Message:');
if (null !== $lastMessageDecodingFailedStamp) {
$io->error('The message could not be decoded. See below an APPROXIMATIVE representation of the class.');
$errorIo->error('The message could not be decoded. See below an APPROXIMATIVE representation of the class.');
}
$dump = new Dumper($io, null, $this->createCloner());
$io->writeln($dump($envelope->getMessage()));
Expand All @@ -142,7 +144,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): v
$io->writeln(null === $flattenException ? '(no data)' : $dump($flattenException));
} else {
if (null !== $lastMessageDecodingFailedStamp) {
$io->error('The message could not be decoded.');
$errorIo->error('The message could not be decoded.');
}
$io->writeln(' Re-run command with <info>-vv</info> to see more message & error details.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
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\Question\ChoiceQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
Expand Down Expand Up @@ -133,7 +132,7 @@ protected function configure(): void
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
$io = new SymfonyStyle($input, $output);

if ($this->receiverNames && !$input->getArgument('receivers')) {
if (1 === \count($this->receiverNames)) {
Expand Down Expand Up @@ -215,19 +214,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$stopsWhen[] = 'received a stop signal via the messenger:stop-workers command';

$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();
$io->success(\sprintf('Consuming messages from transport%s "%s".', \count($receivers) > 1 ? 's' : '', implode(', ', $receiverNames)));

if ($stopsWhen) {
$last = array_pop($stopsWhen);
$stopsWhen = ($stopsWhen ? implode(', ', $stopsWhen).' or ' : '').$last;
$io->comment("The worker will automatically exit once it has {$stopsWhen}.");
$errorIo->comment("The worker will automatically exit once it has {$stopsWhen}.");
}

$io->comment('Quit the worker with CONTROL-C.');
$errorIo->comment('Quit the worker with CONTROL-C.');

if (OutputInterface::VERBOSITY_VERBOSE > $output->getVerbosity()) {
$io->comment('Re-run the command with a -vv option to see logs about consumed messages.');
$errorIo->comment('Re-run the command with a -vv option to see logs about consumed messages.');
}

$bus = $input->getOption('bus') ? $this->routableBus->getMessageBus($input->getOption('bus')) : $this->routableBus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
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;
Expand Down Expand Up @@ -55,7 +54,8 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();

$failureTransportName = $input->getOption('transport');
if (self::DEFAULT_TRANSPORT_OPTION === $failureTransportName) {
Expand All @@ -82,15 +82,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if ($shouldDeleteAllMessages) {
$this->removeAllMessages($receiver, $io, $shouldForce, $shouldDisplayMessages);
$this->removeAllMessages($receiver, $io, $errorIo, $shouldForce, $shouldDisplayMessages);
} else {
$this->removeMessagesById($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
$this->removeMessagesById($ids, $receiver, $io, $errorIo, $shouldForce, $shouldDisplayMessages);
}

return 0;
}

private function removeMessagesById(array $ids, ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
private function removeMessagesById(array $ids, ListableReceiverInterface $receiver, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce, bool $shouldDisplayMessages): void
{
foreach ($ids as $id) {
$this->phpSerializer?->acceptPhpIncompleteClass();
Expand All @@ -101,25 +101,25 @@ private function removeMessagesById(array $ids, ListableReceiverInterface $recei
}

if (null === $envelope) {
$io->error(\sprintf('The message with id "%s" was not found.', $id));
$errorIo->error(\sprintf('The message with id "%s" was not found.', $id));
continue;
}

if ($shouldDisplayMessages) {
$this->displaySingleMessage($envelope, $io);
$this->displaySingleMessage($envelope, $io, $errorIo);
}

if ($shouldForce || $io->confirm('Do you want to permanently remove this message?', false)) {
if ($shouldForce || $errorIo->confirm('Do you want to permanently remove this message?', false)) {
$receiver->reject($envelope);

$io->success(\sprintf('Message with id %s removed.', $id));
} else {
$io->note(\sprintf('Message with id %s not removed.', $id));
$errorIo->note(\sprintf('Message with id %s not removed.', $id));
}
}
}

private function removeAllMessages(ListableReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce, bool $shouldDisplayMessages): void
private function removeAllMessages(ListableReceiverInterface $receiver, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce, bool $shouldDisplayMessages): void
{
if (!$shouldForce) {
if ($receiver instanceof MessageCountAwareInterface) {
Expand All @@ -128,21 +128,21 @@ private function removeAllMessages(ListableReceiverInterface $receiver, SymfonyS
$question = 'Do you want to permanently remove all failed messages?';
}

if (!$io->confirm($question, false)) {
if (!$errorIo->confirm($question, false)) {
return;
}
}

$count = 0;
foreach ($receiver->all() as $envelope) {
if ($shouldDisplayMessages) {
$this->displaySingleMessage($envelope, $io);
$this->displaySingleMessage($envelope, $io, $errorIo);
}

$receiver->reject($envelope);
++$count;
}

$io->note(\sprintf('%d messages were removed.', $count));
$errorIo->note(\sprintf('%d messages were removed.', $count));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
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\EventDispatcher\EventDispatcherInterface;
Expand Down Expand Up @@ -91,18 +90,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->eventDispatcher->addSubscriber(new StopWorkerOnMessageLimitListener(1));

$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
$io->comment('Quit this command with CONTROL-C.');
$io = new SymfonyStyle($input, $output);
$errorIo = $io->getErrorStyle();
$errorIo->comment('Quit this command with CONTROL-C.');
if (!$output->isVeryVerbose()) {
$io->comment('Re-run the command with a -vv option to see logs about consumed messages.');
$errorIo->comment('Re-run the command with a -vv option to see logs about consumed messages.');
}

$failureTransportName = $input->getOption('transport');
if (self::DEFAULT_TRANSPORT_OPTION === $failureTransportName) {
$this->printWarningAvailableFailureTransports($io, $this->getGlobalFailureReceiverName());
$this->printWarningAvailableFailureTransports($errorIo, $this->getGlobalFailureReceiverName());
}
if ('' === $failureTransportName || null === $failureTransportName) {
$failureTransportName = $this->interactiveChooseFailureTransport($io);
$failureTransportName = $this->interactiveChooseFailureTransport($errorIo);
}
$failureTransportName = self::DEFAULT_TRANSPORT_OPTION === $failureTransportName ? $this->getGlobalFailureReceiverName() : $failureTransportName;

Expand All @@ -118,12 +118,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new RuntimeException('Message id must be passed when in non-interactive mode.');
}

$this->runInteractive($failureTransportName, $io, $shouldForce);
$this->runInteractive($failureTransportName, $io, $errorIo, $shouldForce);

return 0;
}

$this->retrySpecificIds($failureTransportName, $ids, $io, $shouldForce);
$this->retrySpecificIds($failureTransportName, $ids, $io, $errorIo, $shouldForce);

if (!$this->shouldStop) {
$io->success('All done!');
Expand Down Expand Up @@ -151,7 +151,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
return $this->forceExit ? 0 : false;
}

private function runInteractive(string $failureTransportName, SymfonyStyle $io, bool $shouldForce): void
private function runInteractive(string $failureTransportName, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce): void
{
$receiver = $this->failureTransports->get($failureTransportName);
$count = 0;
Expand All @@ -178,11 +178,11 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io,
break;
}

$this->retrySpecificEnvelopes($envelopes, $failureTransportName, $io, $shouldForce);
$this->retrySpecificEnvelopes($envelopes, $failureTransportName, $io, $errorIo, $shouldForce);
}
} else {
// get() and ask messages one-by-one
$count = $this->runWorker($failureTransportName, $receiver, $io, $shouldForce);
$count = $this->runWorker($failureTransportName, $receiver, $io, $errorIo, $shouldForce);
}

// avoid success message if nothing was processed
Expand All @@ -191,22 +191,22 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io,
}
}

private function runWorker(string $failureTransportName, ReceiverInterface $receiver, SymfonyStyle $io, bool $shouldForce): int
private function runWorker(string $failureTransportName, ReceiverInterface $receiver, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce): int
{
$count = 0;
$listener = function (WorkerMessageReceivedEvent $messageReceivedEvent) use ($io, $receiver, $shouldForce, &$count) {
$listener = function (WorkerMessageReceivedEvent $messageReceivedEvent) use ($io, $errorIo, $receiver, $shouldForce, &$count) {
++$count;
$envelope = $messageReceivedEvent->getEnvelope();

$this->displaySingleMessage($envelope, $io);
$this->displaySingleMessage($envelope, $io, $errorIo);

if ($envelope->last(MessageDecodingFailedStamp::class)) {
throw new \RuntimeException(\sprintf('The message with id "%s" could not decoded, it can only be shown or removed.', $this->getMessageId($envelope) ?? '?'));
}

$this->forceExit = true;
try {
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry');
$shouldHandle = $shouldForce || 'retry' === $errorIo->choice('Please select an action', ['retry', 'delete'], 'retry');
} finally {
$this->forceExit = false;
}
Expand Down Expand Up @@ -237,7 +237,7 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece
return $count;
}

private function retrySpecificIds(string $failureTransportName, array $ids, SymfonyStyle $io, bool $shouldForce): void
private function retrySpecificIds(string $failureTransportName, array $ids, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce): void
{
$receiver = $this->getReceiver($failureTransportName);

Expand All @@ -257,21 +257,21 @@ private function retrySpecificIds(string $failureTransportName, array $ids, Symf
}

$singleReceiver = new SingleMessageReceiver($receiver, $envelope);
$this->runWorker($failureTransportName, $singleReceiver, $io, $shouldForce);
$this->runWorker($failureTransportName, $singleReceiver, $io, $errorIo, $shouldForce);

if ($this->shouldStop) {
break;
}
}
}

private function retrySpecificEnvelopes(array $envelopes, string $failureTransportName, SymfonyStyle $io, bool $shouldForce): void
private function retrySpecificEnvelopes(array $envelopes, string $failureTransportName, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce): void
{
$receiver = $this->getReceiver($failureTransportName);

foreach ($envelopes as $envelope) {
$singleReceiver = new SingleMessageReceiver($receiver, $envelope);
$this->runWorker($failureTransportName, $singleReceiver, $io, $shouldForce);
$this->runWorker($failureTransportName, $singleReceiver, $io, $errorIo, $shouldForce);

if ($this->shouldStop) {
break;
Expand Down
Loading
Loading