Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
8 changed files
with
238 additions
and
16 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/Symfony/Component/Messenger/Exception/ChainedHandlerFailedException.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,49 @@ | ||
<?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. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Symfony\Component\Messenger\Exception; | ||
|
||
use Symfony\Component\Messenger\Envelope; | ||
|
||
class ChainedHandlerFailedException extends \RuntimeException implements ExceptionInterface | ||
{ | ||
/** | ||
* @var \Throwable[] | ||
*/ | ||
private $nested; | ||
|
||
/** | ||
* @var Envelope | ||
*/ | ||
private $envelope; | ||
|
||
public function __construct(Envelope $envelope, \Throwable ...$nested) | ||
{ | ||
$this->envelope = $envelope; | ||
$this->nested = $nested; | ||
parent::__construct(); | ||
} | ||
|
||
public function getEnvelope(): Envelope | ||
{ | ||
return $this->envelope; | ||
} | ||
|
||
/** | ||
* @return \Throwable[] | ||
*/ | ||
public function getNestedExceptions(): array | ||
{ | ||
return $this->nested; | ||
} | ||
} |
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
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
45 changes: 45 additions & 0 deletions
45
src/Symfony/Component/Messenger/Tests/Fixtures/DummyMessageHandler.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,45 @@ | ||
<?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 DummyMessageHandler | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $remainingExceptionCount; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $called = 0; | ||
|
||
public function __construct(int $throwExceptionOnFirstTries = 0) | ||
{ | ||
$this->remainingExceptionCount = $throwExceptionOnFirstTries; | ||
} | ||
|
||
public function __invoke(DummyMessage $message) | ||
{ | ||
if ($this->remainingExceptionCount > 0) { | ||
--$this->remainingExceptionCount; | ||
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
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* 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\CallbackReceiver; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage; | ||
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageHandler; | ||
use Symfony\Component\Messenger\Transport\Sender\SendersLocator; | ||
use Symfony\Component\Messenger\Worker; | ||
|
||
class RetryIntegrationTest extends TestCase | ||
{ | ||
public function testRetryMechanism() | ||
{ | ||
$apiMessage = new DummyMessage('API'); | ||
|
||
$receiver = new CallbackReceiver(function ($handler) use ($apiMessage) { | ||
$handler(new Envelope($apiMessage, new SentStamp('Some\Sender', 'sender_alias'))); | ||
}); | ||
|
||
$senderLocator = new SendersLocator([], ['*' => true]); | ||
|
||
$handler = new DummyMessageHandler(); | ||
$throwingHandler = new DummyMessageHandler(1); | ||
$handlerLocator = new HandlersLocator([ | ||
DummyMessage::class => [ | ||
'handler' => $handler, | ||
'throwing' => $throwingHandler | ||
], | ||
]); | ||
|
||
$bus = new MessageBus([new SendMessageMiddleware($senderLocator), new HandleMessageMiddleware($handlerLocator)]); | ||
|
||
$worker = new Worker($receiver, $bus, 'receiver name', new MultiplierRetryStrategy()); | ||
$worker->run(); | ||
|
||
$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