Skip to content

Commit 8c52edd

Browse files
wazumnicolas-grekas
authored andcommitted
[Messenger] Fix commands writing to STDERR instead of STDOUT
Messenger commands were using getErrorOutput() for all output, causing structured data (tables, messages) to go to STDERR instead of STDOUT. Fixes #60822
1 parent 4cb717b commit 8c52edd

13 files changed

+418
-61
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,10 @@ protected function getMessageId(Envelope $envelope): mixed
7272
return $stamp?->getId();
7373
}
7474

75-
protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): void
75+
protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io, ?SymfonyStyle $errorIo = null): void
7676
{
77+
$errorIo ??= $io->getErrorStyle();
78+
7779
$io->title('Failed Message Details');
7880

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

9698
if (null === $sentToFailureTransportStamp) {
97-
$io->warning('Message does not appear to have been sent to this transport after failing');
99+
$errorIo->warning('Message does not appear to have been sent to this transport after failing');
98100
} else {
99101
$failedAt = '';
100102
$errorMessage = '';
@@ -133,7 +135,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): v
133135
if ($io->isVeryVerbose()) {
134136
$io->title('Message:');
135137
if (null !== $lastMessageDecodingFailedStamp) {
136-
$io->error('The message could not be decoded. See below an APPROXIMATIVE representation of the class.');
138+
$errorIo->error('The message could not be decoded. See below an APPROXIMATIVE representation of the class.');
137139
}
138140
$dump = new Dumper($io, null, $this->createCloner());
139141
$io->writeln($dump($envelope->getMessage()));
@@ -142,7 +144,7 @@ protected function displaySingleMessage(Envelope $envelope, SymfonyStyle $io): v
142144
$io->writeln(null === $flattenException ? '(no data)' : $dump($flattenException));
143145
} else {
144146
if (null !== $lastMessageDecodingFailedStamp) {
145-
$io->error('The message could not be decoded.');
147+
$errorIo->error('The message could not be decoded.');
146148
}
147149
$io->writeln(' Re-run command with <info>-vv</info> to see more message & error details.');
148150
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Symfony\Component\Console\Input\InputArgument;
2424
use Symfony\Component\Console\Input\InputInterface;
2525
use Symfony\Component\Console\Input\InputOption;
26-
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2726
use Symfony\Component\Console\Output\OutputInterface;
2827
use Symfony\Component\Console\Question\ChoiceQuestion;
2928
use Symfony\Component\Console\Style\SymfonyStyle;
@@ -133,7 +132,7 @@ protected function configure(): void
133132
*/
134133
protected function interact(InputInterface $input, OutputInterface $output)
135134
{
136-
$io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output);
135+
$io = new SymfonyStyle($input, $output);
137136

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

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

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

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

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

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

233233
$bus = $input->getOption('bus') ? $this->routableBus->getMessageBus($input->getOption('bus')) : $this->routableBus;

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Input\InputOption;
19-
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2019
use Symfony\Component\Console\Output\OutputInterface;
2120
use Symfony\Component\Console\Style\SymfonyStyle;
2221
use Symfony\Component\Messenger\Transport\Receiver\ListableReceiverInterface;
@@ -55,7 +54,8 @@ protected function configure(): void
5554

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

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

8484
if ($shouldDeleteAllMessages) {
85-
$this->removeAllMessages($receiver, $io, $shouldForce, $shouldDisplayMessages);
85+
$this->removeAllMessages($receiver, $io, $errorIo, $shouldForce, $shouldDisplayMessages);
8686
} else {
87-
$this->removeMessagesById($ids, $receiver, $io, $shouldForce, $shouldDisplayMessages);
87+
$this->removeMessagesById($ids, $receiver, $io, $errorIo, $shouldForce, $shouldDisplayMessages);
8888
}
8989

9090
return 0;
9191
}
9292

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

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

108108
if ($shouldDisplayMessages) {
109-
$this->displaySingleMessage($envelope, $io);
109+
$this->displaySingleMessage($envelope, $io, $errorIo);
110110
}
111111

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

115115
$io->success(\sprintf('Message with id %s removed.', $id));
116116
} else {
117-
$io->note(\sprintf('Message with id %s not removed.', $id));
117+
$errorIo->note(\sprintf('Message with id %s not removed.', $id));
118118
}
119119
}
120120
}
121121

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

131-
if (!$io->confirm($question, false)) {
131+
if (!$errorIo->confirm($question, false)) {
132132
return;
133133
}
134134
}
135135

136136
$count = 0;
137137
foreach ($receiver->all() as $envelope) {
138138
if ($shouldDisplayMessages) {
139-
$this->displaySingleMessage($envelope, $io);
139+
$this->displaySingleMessage($envelope, $io, $errorIo);
140140
}
141141

142142
$receiver->reject($envelope);
143143
++$count;
144144
}
145145

146-
$io->note(\sprintf('%d messages were removed.', $count));
146+
$errorIo->note(\sprintf('%d messages were removed.', $count));
147147
}
148148
}

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Symfony\Component\Console\Input\InputArgument;
1919
use Symfony\Component\Console\Input\InputInterface;
2020
use Symfony\Component\Console\Input\InputOption;
21-
use Symfony\Component\Console\Output\ConsoleOutputInterface;
2221
use Symfony\Component\Console\Output\OutputInterface;
2322
use Symfony\Component\Console\Style\SymfonyStyle;
2423
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -91,18 +90,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9190
{
9291
$this->eventDispatcher->addSubscriber(new StopWorkerOnMessageLimitListener(1));
9392

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

100100
$failureTransportName = $input->getOption('transport');
101101
if (self::DEFAULT_TRANSPORT_OPTION === $failureTransportName) {
102-
$this->printWarningAvailableFailureTransports($io, $this->getGlobalFailureReceiverName());
102+
$this->printWarningAvailableFailureTransports($errorIo, $this->getGlobalFailureReceiverName());
103103
}
104104
if ('' === $failureTransportName || null === $failureTransportName) {
105-
$failureTransportName = $this->interactiveChooseFailureTransport($io);
105+
$failureTransportName = $this->interactiveChooseFailureTransport($errorIo);
106106
}
107107
$failureTransportName = self::DEFAULT_TRANSPORT_OPTION === $failureTransportName ? $this->getGlobalFailureReceiverName() : $failureTransportName;
108108

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

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

123123
return 0;
124124
}
125125

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

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

154-
private function runInteractive(string $failureTransportName, SymfonyStyle $io, bool $shouldForce): void
154+
private function runInteractive(string $failureTransportName, SymfonyStyle $io, SymfonyStyle $errorIo, bool $shouldForce): void
155155
{
156156
$receiver = $this->failureTransports->get($failureTransportName);
157157
$count = 0;
@@ -178,11 +178,11 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io,
178178
break;
179179
}
180180

181-
$this->retrySpecificEnvelopes($envelopes, $failureTransportName, $io, $shouldForce);
181+
$this->retrySpecificEnvelopes($envelopes, $failureTransportName, $io, $errorIo, $shouldForce);
182182
}
183183
} else {
184184
// get() and ask messages one-by-one
185-
$count = $this->runWorker($failureTransportName, $receiver, $io, $shouldForce);
185+
$count = $this->runWorker($failureTransportName, $receiver, $io, $errorIo, $shouldForce);
186186
}
187187

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

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

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

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

207207
$this->forceExit = true;
208208
try {
209-
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry');
209+
$shouldHandle = $shouldForce || 'retry' === $errorIo->choice('Please select an action', ['retry', 'delete'], 'retry');
210210
} finally {
211211
$this->forceExit = false;
212212
}
@@ -237,7 +237,7 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece
237237
return $count;
238238
}
239239

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

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

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

262262
if ($this->shouldStop) {
263263
break;
264264
}
265265
}
266266
}
267267

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

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

276276
if ($this->shouldStop) {
277277
break;

0 commit comments

Comments
 (0)