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
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static function getSubscribedEvents(): array

private function shouldRetry(\Throwable $e, Envelope $envelope, RetryStrategyInterface $retryStrategy): bool
{
if ($e instanceof RecoverableExceptionInterface) {
if ($e instanceof RecoverableExceptionInterface && (!method_exists($e, 'shouldForceRetry') || $e->shouldForceRetry())) {
return true;
}

Expand All @@ -125,7 +125,9 @@ private function shouldRetry(\Throwable $e, Envelope $envelope, RetryStrategyInt
if ($e instanceof HandlerFailedException) {
$shouldNotRetry = true;
foreach ($e->getWrappedExceptions() as $nestedException) {
if ($nestedException instanceof RecoverableExceptionInterface) {
if ($nestedException instanceof RecoverableExceptionInterface
&& (!method_exists($nestedException, 'shouldForceRetry') || $nestedException->shouldForceRetry())
) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
*
* @author Jérémy Derussé <jeremy@derusse.com>
*
* @method int|null getRetryDelay() The time to wait in milliseconds
* @method int|null getRetryDelay() The time to wait in milliseconds
* @method bool shouldForceRetry() If the worker should retry even if it goes over the max retry limit
VincentLanglet marked this conversation as resolved.
Outdated
Show resolved Hide resolved
*/
interface RecoverableExceptionInterface extends \Throwable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class RecoverableMessageHandlingException extends RuntimeException implements RecoverableExceptionInterface
{
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null, private readonly ?int $retryDelay = null)
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null, private readonly ?int $retryDelay = null, private readonly bool $forceRetry = true)
VincentLanglet marked this conversation as resolved.
Outdated
Show resolved Hide resolved
{
parent::__construct($message, $code, $previous);
}
Expand All @@ -27,4 +27,9 @@ public function getRetryDelay(): ?int
{
return $this->retryDelay;
}

public function shouldForceRetry(): bool
{
return $this->forceRetry;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PHPUnit\Framework\TestCase;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
use Symfony\Component\Messenger\Event\WorkerMessageRetriedEvent;
Expand Down Expand Up @@ -201,6 +202,29 @@ public static function provideRetryDelays(): iterable
];
}

public function testRetryIsOnlyAllowedWhenPermittedByRetryStrategy()
{
$senderLocator = $this->createMock(ContainerInterface::class);
$senderLocator->expects($this->never())->method('has');
$senderLocator->expects($this->never())->method('get');

$retryStrategy = $this->createMock(RetryStrategyInterface::class);
$retryStrategy->expects($this->once())->method('isRetryable')->willReturn(false);
$retryStrategy->expects($this->never())->method('getWaitingTime');

$retryStrategyLocator = $this->createMock(ContainerInterface::class);
$retryStrategyLocator->expects($this->once())->method('has')->willReturn(true);
$retryStrategyLocator->expects($this->once())->method('get')->willReturn($retryStrategy);

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

$exception = new RecoverableMessageHandlingException('retry', forceRetry: false);
$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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.