Skip to content

Commit

Permalink
bug #32154 [Messenger] fix retrying handlers using DoctrineTransactio…
Browse files Browse the repository at this point in the history
…nMiddleware (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
-------

66c2e84 [Messenger] fix retrying handlers using DoctrineTransactionMiddleware
  • Loading branch information
fabpot committed Jun 25, 2019
2 parents 4e6951b + 66c2e84 commit b68a6b3
Showing 1 changed file with 8 additions and 0 deletions.
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
Expand Down

0 comments on commit b68a6b3

Please sign in to comment.