Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #30020 [Messenger] Ensure message is handled only once per ha…
…ndler (keulinho, sroze) This PR was merged into the 4.3-dev branch. Discussion ---------- [Messenger] Ensure message is handled only once per handler Add check to ensure that a message is only handled once per handler Add try...catch to run all handlers before throwing exception | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? |no | Tests pass? | yes | Fixed tickets | #27215 | License | MIT | Doc PR | Todo This would make error handling and retrying of messages much more easier. As statet here #27008 (comment) there is currently no way to retry a for all failed handlers if there are mutliple handlers and just some throw an exception. Also if an Exception in an handler occurs the execution chain is disrupted and the other handlers are never invoked. With this change it is easily possible to create an userland middleware that catches the `ChainedHandlerFailedException` and does some custom retry logic. If you ensure that the `HandledStamps` on the `Envelope` are preserved the message will be handled just by the failed handlers Commits ------- 2e5e910 Rename exception, add change log and a few other things e6e4cde Ensure message is handled only once per handler
- Loading branch information
Showing
8 changed files
with
236 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Symfony/Component/Messenger/Exception/HandlerFailedException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Exception; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
|
||
class HandlerFailedException extends RuntimeException | ||
{ | ||
private $exceptions; | ||
private $envelope; | ||
|
||
/** | ||
* @param \Throwable[] $exceptions | ||
*/ | ||
public function __construct(Envelope $envelope, array $exceptions) | ||
{ | ||
$firstFailure = current($exceptions); | ||
|
||
parent::__construct( | ||
1 === \count($exceptions) | ||
? $firstFailure->getMessage() | ||
: sprintf('%d handlers failed. First failure is: "%s"', \count($exceptions), $firstFailure->getMessage()), | ||
$firstFailure->getCode(), | ||
$firstFailure | ||
); | ||
|
||
$this->envelope = $envelope; | ||
$this->exceptions = $exceptions; | ||
} | ||
|
||
public function getEnvelope(): Envelope | ||
{ | ||
return $this->envelope; | ||
} | ||
|
||
/** | ||
* @return \Throwable[] | ||
*/ | ||
public function getNestedExceptions(): array | ||
{ | ||
return $this->exceptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/Messenger/Tests/Fixtures/DummyMessageHandlerFailingFirstTimes.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests\Fixtures; | ||
|
||
class DummyMessageHandlerFailingFirstTimes | ||
{ | ||
private $remainingFailures; | ||
|
||
private $called = 0; | ||
|
||
public function __construct(int $throwExceptionOnFirstTries = 0) | ||
{ | ||
$this->remainingFailures = $throwExceptionOnFirstTries; | ||
} | ||
|
||
public function __invoke(DummyMessage $message) | ||
{ | ||
if ($this->remainingFailures > 0) { | ||
--$this->remainingFailures; | ||
throw new \Exception('Handler should throw Exception.'); | ||
} | ||
|
||
++$this->called; | ||
} | ||
|
||
public function getTimesCalledWithoutThrowing(): int | ||
{ | ||
return $this->called; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/Symfony/Component/Messenger/Tests/RetryIntegrationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Handler\HandlersLocator; | ||
use Symfony\Component\Messenger\MessageBus; | ||
use Symfony\Component\Messenger\Middleware\HandleMessageMiddleware; | ||
use Symfony\Component\Messenger\Middleware\SendMessageMiddleware; | ||
use Symfony\Component\Messenger\Retry\MultiplierRetryStrategy; | ||
use Symfony\Component\Messenger\Stamp\SentStamp; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageHandlerFailingFirstTimes; | ||
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface; | ||
use Symfony\Component\Messenger\Transport\Sender\SendersLocator; | ||
use Symfony\Component\Messenger\Worker; | ||
|
||
class RetryIntegrationTest extends TestCase | ||
{ | ||
public function testRetryMechanism() | ||
{ | ||
$apiMessage = new DummyMessage('API'); | ||
|
||
$receiver = $this->createMock(ReceiverInterface::class); | ||
$receiver->method('get') | ||
->willReturn([ | ||
new Envelope($apiMessage, [ | ||
new SentStamp('Some\Sender', 'sender_alias'), | ||
]), | ||
]); | ||
|
||
$senderLocator = new SendersLocator([], ['*' => true]); | ||
|
||
$handler = new DummyMessageHandlerFailingFirstTimes(); | ||
$throwingHandler = new DummyMessageHandlerFailingFirstTimes(1); | ||
$handlerLocator = new HandlersLocator([ | ||
DummyMessage::class => [ | ||
'handler' => $handler, | ||
'throwing' => $throwingHandler, | ||
], | ||
]); | ||
|
||
$bus = new MessageBus([new SendMessageMiddleware($senderLocator), new HandleMessageMiddleware($handlerLocator)]); | ||
|
||
$worker = new Worker(['receiverName' => $receiver], $bus, ['receiverName' => new MultiplierRetryStrategy()]); | ||
$worker->run([], function () use ($worker) { | ||
$worker->stop(); | ||
}); | ||
|
||
$this->assertSame(1, $handler->getTimesCalledWithoutThrowing()); | ||
$this->assertSame(1, $throwingHandler->getTimesCalledWithoutThrowing()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters