[Messenger] fix retrying handlers using DoctrineTransactionMiddleware

This commit is contained in:
Tobias Schultze 2019-06-24 15:34:54 +01:00
parent 698601140b
commit 66c2e8483a
1 changed files with 8 additions and 0 deletions

View File

@ -13,9 +13,11 @@ namespace Symfony\Bridge\Doctrine\Messenger;
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 @@ class DoctrineTransactionMiddleware implements MiddlewareInterface
} 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;
}
}