Skip to content

[Messenger] Allow setting retry delay by RecoverableExceptionInterface #57915

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
Aug 13, 2024
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
5 changes: 5 additions & 0 deletions UPGRADE-7.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ FrameworkBundle

* [BC BREAK] The `secrets:decrypt-to-local` command terminates with a non-zero exit code when a secret could not be read

Messenger
---------

* Add `getRetryDelay()` method to `RecoverableExceptionInterface`

Security
--------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* `WrappedExceptionsInterface` now extends PHP's `Throwable` interface
* Add `#[AsMessage]` attribute with `$transport` parameter for message routing
* Add `--format` option to the `messenger:stats` command
* Add `getRetryDelay()` method to `RecoverableExceptionInterface`

7.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ public function onMessageFailed(WorkerMessageFailedEvent $event): void

++$retryCount;

$delay = $retryStrategy->getWaitingTime($envelope, $throwable);
$delay = null;
if ($throwable instanceof RecoverableExceptionInterface && method_exists($throwable, 'getRetryDelay')) {
$delay = $throwable->getRetryDelay();
}

$delay ??= $retryStrategy->getWaitingTime($envelope, $throwable);

$this->logger?->warning('Error thrown while handling message {class}. Sending for retry #{retryCount} using {delay} ms delay. Error: "{error}"', $context + ['retryCount' => $retryCount, 'delay' => $delay, 'error' => $throwable->getMessage(), 'exception' => $throwable]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* and the message should be retried, a handler can throw such an exception.
*
* @author Jérémy Derussé <jeremy@derusse.com>
*
* @method int|null getRetryDelay() The time to wait in milliseconds
*/
interface RecoverableExceptionInterface extends \Throwable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,13 @@
*/
class RecoverableMessageHandlingException extends RuntimeException implements RecoverableExceptionInterface
{
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null, private readonly ?int $retryDelay = null)
{
parent::__construct($message, $code, $previous);
}

public function getRetryDelay(): ?int
{
return $this->retryDelay;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ public function testRecoverableStrategyCausesRetry()
$listener->onMessageFailed($event);
}

public function testRecoverableExceptionRetryDelayOverridesStrategy()
{
$sender = $this->createMock(SenderInterface::class);
$sender->expects($this->once())->method('send')->willReturnCallback(function (Envelope $envelope) {
$delayStamp = $envelope->last(DelayStamp::class);
$redeliveryStamp = $envelope->last(RedeliveryStamp::class);

$this->assertInstanceOf(DelayStamp::class, $delayStamp);
$this->assertSame(1234, $delayStamp->getDelay());

$this->assertInstanceOf(RedeliveryStamp::class, $redeliveryStamp);
$this->assertSame(1, $redeliveryStamp->getRetryCount());

return $envelope;
});
$senderLocator = new Container();
$senderLocator->set('my_receiver', $sender);
$retryStrategy = $this->createMock(RetryStrategyInterface::class);
$retryStrategy->expects($this->never())->method('isRetryable');
$retryStrategy->expects($this->never())->method('getWaitingTime');
$retryStrategyLocator = new Container();
$retryStrategyLocator->set('my_receiver', $retryStrategy);

$listener = new SendFailedMessageForRetryListener($senderLocator, $retryStrategyLocator);

$exception = new RecoverableMessageHandlingException('retry', retryDelay: 1234);
$envelope = new Envelope(new \stdClass());
$event = new WorkerMessageFailedEvent($envelope, 'my_receiver', $exception);

$listener->onMessageFailed($event);
}

public function testEnvelopeIsSentToTransportOnRetry()
{
$exception = new \Exception('no!');
Expand Down
Loading