-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger] Ensure message is handled only once per handler #30020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
use Psr\Log\LoggerAwareTrait; | ||
use Psr\Log\NullLogger; | ||
use Symfony\Component\Messenger\Envelope; | ||
use Symfony\Component\Messenger\Exception\HandlerFailedException; | ||
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException; | ||
use Symfony\Component\Messenger\Handler\HandlersLocatorInterface; | ||
use Symfony\Component\Messenger\Stamp\HandledStamp; | ||
|
@@ -52,10 +53,21 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope | |
'class' => \get_class($message), | ||
]; | ||
|
||
$exceptions = []; | ||
foreach ($this->handlersLocator->getHandlers($envelope) as $alias => $handler) { | ||
$handledStamp = HandledStamp::fromCallable($handler, $handler($message), \is_string($alias) ? $alias : null); | ||
$envelope = $envelope->with($handledStamp); | ||
$this->logger->info('Message "{class}" handled by "{handler}"', $context + ['handler' => $handledStamp->getCallableName()]); | ||
$alias = \is_string($alias) ? $alias : null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They would not appear as handled because we track the handler name. Alias is just an optional thing we actually don't use in core. (I think we could even remove it, it's been introduced - #29166 - on the assumption that it might be useful later, while it complexifies reading this code). |
||
|
||
if ($this->messageHasAlreadyBeenHandled($envelope, $handler, $alias)) { | ||
continue; | ||
} | ||
|
||
try { | ||
$handledStamp = HandledStamp::fromCallable($handler, $handler($message), $alias); | ||
$envelope = $envelope->with($handledStamp); | ||
$this->logger->info('Message "{class}" handled by "{handler}"', $context + ['handler' => $handledStamp->getCallableName()]); | ||
} catch (\Throwable $e) { | ||
$exceptions[] = $e; | ||
} | ||
} | ||
|
||
if (null === $handler) { | ||
|
@@ -66,6 +78,21 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope | |
$this->logger->info('No handler for message "{class}"', $context); | ||
} | ||
|
||
if (\count($exceptions)) { | ||
throw new HandlerFailedException($envelope, $exceptions); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just thinking out loud: one practical implication is that, if someone listens on the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, exactly. I was thinking whether it would make sense or not to throw the original exception if there is only one but it would mean you need to catch your own exception plus |
||
|
||
return $stack->next()->handle($envelope, $stack); | ||
} | ||
|
||
private function messageHasAlreadyBeenHandled(Envelope $envelope, callable $handler, ?string $alias): bool | ||
{ | ||
$some = array_filter($envelope | ||
sroze marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->all(HandledStamp::class), function (HandledStamp $stamp) use ($handler, $alias) { | ||
return $stamp->getCallableName() === HandledStamp::getNameFromCallable($handler) && | ||
$stamp->getHandlerAlias() === $alias; | ||
}); | ||
|
||
return \count($some) > 0; | ||
} | ||
} |
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; | ||
} | ||
} |
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()); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.