bug #32154 [Messenger] fix retrying handlers using DoctrineTransactionMiddleware (Tobion)

This PR was merged into the 4.3 branch.

Discussion
----------

[Messenger] fix retrying handlers using DoctrineTransactionMiddleware

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets |
| License       | MIT
| Doc PR        |

The retry logic only executes handlers that didn't fail using the HandledStamp. But in case of using the DoctrineTransactionMiddleware using several handlers, we need to remove the HandledStamp because those handlers got rolled back again.

Commits
-------

66c2e8483a [Messenger] fix retrying handlers using DoctrineTransactionMiddleware
This commit is contained in:
Fabien Potencier 2019-06-25 15:53:13 +02:00
commit b68a6b3e16
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;
}
}