-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Fix graceful exit #52080
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ class FailedMessagesRetryCommand extends AbstractFailedMessagesCommand implement | |
private MessageBusInterface $messageBus; | ||
private ?LoggerInterface $logger; | ||
private ?array $signals; | ||
private bool $shouldStop = false; | ||
private bool $forceExit = false; | ||
private ?Worker $worker = null; | ||
|
||
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| | |
$this->logger?->info('Received signal {signal}.', ['signal' => $signal, 'transport_names' => $this->worker->getMetadata()->getTransportNames()]); | ||
|
||
$this->worker->stop(); | ||
$this->shouldStop = true; | ||
|
||
return 0; | ||
return $this->forceExit ? 0 : false; | ||
} | ||
|
||
private function runInteractive(string $failureTransportName, SymfonyStyle $io, bool $shouldForce): void | ||
|
@@ -156,6 +159,10 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io, | |
// to be temporarily "acked", even if the user aborts | ||
// handling the message | ||
while (true) { | ||
if ($this->shouldStop) { | ||
break; | ||
} | ||
Comment on lines
+162
to
+164
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is required because the worker is instantiated in loops as well. |
||
|
||
$envelopes = []; | ||
$this->phpSerializer?->acceptPhpIncompleteClass(); | ||
try { | ||
|
@@ -180,7 +187,7 @@ private function runInteractive(string $failureTransportName, SymfonyStyle $io, | |
} | ||
|
||
// avoid success message if nothing was processed | ||
if (1 <= $count) { | ||
if (1 <= $count && !$this->shouldStop) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To avoid printing the message if the command has been terminated. |
||
$io->success('All failed messages have been handled or removed!'); | ||
} | ||
} | ||
|
@@ -198,7 +205,12 @@ private function runWorker(string $failureTransportName, ReceiverInterface $rece | |
throw new \RuntimeException(sprintf('The message with id "%s" could not decoded, it can only be shown or removed.', $this->getMessageId($envelope) ?? '?')); | ||
} | ||
|
||
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry'); | ||
$this->forceExit = true; | ||
try { | ||
$shouldHandle = $shouldForce || 'retry' === $io->choice('Please select an action', ['retry', 'delete'], 'retry'); | ||
} finally { | ||
$this->forceExit = false; | ||
} | ||
Comment on lines
-201
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the |
||
|
||
if ($shouldHandle) { | ||
return; | ||
|
@@ -257,6 +269,10 @@ private function retrySpecificEnvelopes(array $envelopes, string $failureTranspo | |
foreach ($envelopes as $envelope) { | ||
$singleReceiver = new SingleMessageReceiver($receiver, $envelope); | ||
$this->runWorker($failureTransportName, $singleReceiver, $io, $shouldForce); | ||
|
||
if ($this->shouldStop) { | ||
break; | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to exit here, we just have to call
$this->worker->stop()
and the command will finish gracefully once the handler is done.