Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Merged
merged 1 commit into from
Oct 16, 2023
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
[Messenger] Fix graceful exit
  • Loading branch information
HypeMC committed Oct 16, 2023
commit b270382f1f599efa0f833ab01693a29c60551bd8
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function handleSignal(int $signal, int|false $previousExitCode = 0): int|

$this->worker->stop();

return 0;
return false;
Copy link
Member Author

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.

}

private function convertToBytes(string $memoryLimit): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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!');
}
}
Expand All @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

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

With the messenger:failed:retry it's a little different, the $io->choice() method has an infinitive loop, so we need to exit if the command is waiting for an answer, otherwise we're stuck in the loop, which is the original issue that I tried to fix in #50787.


if ($shouldHandle) {
return;
Expand Down Expand Up @@ -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;
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.