Skip to content

Commit 8a1fba8

Browse files
committed
bug #52080 [Messenger] Fix graceful exit (HypeMC)
This PR was merged into the 6.3 branch. Discussion ---------- [Messenger] Fix graceful exit | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #52077 | License | MIT My previous PR #50787 accidentally broke the behavior of the `messenger:consume` command. It no longer waits for the handler to finish, instead it exists immediately. Commits ------- b270382 [Messenger] Fix graceful exit
2 parents 969d39a + b270382 commit 8a1fba8

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
271271

272272
$this->worker->stop();
273273

274-
return 0;
274+
return false;
275275
}
276276

277277
private function convertToBytes(string $memoryLimit): int

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class FailedMessagesRetryCommand extends AbstractFailedMessagesCommand implement
4343
private MessageBusInterface $messageBus;
4444
private ?LoggerInterface $logger;
4545
private ?array $signals;
46+
private bool $shouldStop = false;
47+
private bool $forceExit = false;
4648
private ?Worker $worker = null;
4749

4850
public function __construct(?string $globalReceiverName, ServiceProviderInterface $failureTransports, MessageBusInterface $messageBus, EventDispatcherInterface $eventDispatcher, LoggerInterface $logger = null, PhpSerializer $phpSerializer = null, array $signals = null)
@@ -141,8 +143,9 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|
141143
$this->logger?->info('Received signal {signal}.', ['signal' => $signal, 'transport_names' => $this->worker->getMetadata()->getTransportNames()]);
142144

143145
$this->worker->stop();
146+
$this->shouldStop = true;
144147

145-
return 0;
148+
return $this->forceExit ? 0 : false;
146149
}
147150

148151
private function runInteractive(string $failureTransportName, SymfonyStyle $io, bool $shouldForce): void
@@ -156,6 +159,10 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io,
156159
// to be temporarily "acked", even if the user aborts
157160
// handling the message
158161
while (true) {
162+
if ($this->shouldStop) {
163+
break;
164+
}
165+
159166
$envelopes = [];
160167
$this->phpSerializer?->acceptPhpIncompleteClass();
161168
try {
@@ -180,7 +187,7 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io,
180187
}
181188

182189
// avoid success message if nothing was processed
183-
if (1 <= $count) {
190+
if (1 <= $count && !$this->shouldStop) {
184191
$io->success('All failed messages have been handled or removed!');
185192
}
186193
}
@@ -198,7 +205,12 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece
198205
throw new \RuntimeException(sprintf('The message with id "%s" could not decoded, it can only be shown or removed.', $this->getMessageId($envelope) ?? '?'));
199206
}
200207

201-
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry');
208+
$this->forceExit = true;
209+
try {
210+
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry');
211+
} finally {
212+
$this->forceExit = false;
213+
}
202214

203215
if ($shouldHandle) {
204216
return;
@@ -257,6 +269,10 @@ private function retrySpecificEnvelopes(array $envelopes, string $failureTranspo
257269
foreach ($envelopes as $envelope) {
258270
$singleReceiver = new SingleMessageReceiver($receiver, $envelope);
259271
$this->runWorker($failureTransportName, $singleReceiver, $io, $shouldForce);
272+
273+
if ($this->shouldStop) {
274+
break;
275+
}
260276
}
261277
}
262278
}

0 commit comments

Comments
 (0)