From 66c2e8483a80247981b5dae9748966faf4c1ada7 Mon Sep 17 00:00:00 2001 From: Tobias Schultze Date: Mon, 24 Jun 2019 15:34:54 +0100 Subject: [PATCH] [Messenger] fix retrying handlers using DoctrineTransactionMiddleware --- .../Doctrine/Messenger/DoctrineTransactionMiddleware.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php index ad0d87b97c6b..62f0bac51dd7 100644 --- a/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php +++ b/src/Symfony/Bridge/Doctrine/Messenger/DoctrineTransactionMiddleware.php @@ -13,9 +13,11 @@ use Doctrine\Common\Persistence\ManagerRegistry; use Symfony\Component\Messenger\Envelope; +use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Messenger\Middleware\MiddlewareInterface; use Symfony\Component\Messenger\Middleware\StackInterface; +use Symfony\Component\Messenger\Stamp\HandledStamp; /** * Wraps all handlers in a single doctrine transaction. @@ -56,6 +58,12 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope } catch (\Throwable $exception) { $entityManager->getConnection()->rollBack(); + if ($exception instanceof HandlerFailedException) { + // Remove all HandledStamp from the envelope so the retry will execute all handlers again. + // When a handler fails, the queries of allegedly successful previous handlers just got rolled back. + throw new HandlerFailedException($exception->getEnvelope()->withoutAll(HandledStamp::class), $exception->getNestedExceptions()); + } + throw $exception; } }