Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Messenger] Don't prevent dispatch out of another bus #37976

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

Merged
merged 1 commit into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ CHANGELOG

* Added `FlattenExceptionNormalizer` to give more information about the exception on Messenger background processes. The `FlattenExceptionNormalizer` has a higher priority than `ProblemNormalizer` and it is only used when the Messenger serialization context is set.
* Added factory methods to `DelayStamp`.
* Removed the exception when dispatching a message with a `DispatchAfterCurrentBusStamp` and not in a context of another dispatch call

5.1.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ class DispatchAfterCurrentBusMiddleware implements MiddlewareInterface
public function handle(Envelope $envelope, StackInterface $stack): Envelope
{
if (null !== $envelope->last(DispatchAfterCurrentBusStamp::class)) {
if (!$this->isRootDispatchCallRunning) {
throw new \LogicException(sprintf('You can only use a "%s" stamp in the context of a message handler.', DispatchAfterCurrentBusStamp::class));
if ($this->isRootDispatchCallRunning) {
$this->queue[] = new QueuedEnvelope($envelope, $stack);

return $envelope;
}
$this->queue[] = new QueuedEnvelope($envelope, $stack);

return $envelope;
$envelope = $envelope->withoutAll(DispatchAfterCurrentBusStamp::class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing it from the envelope makes sense to me here for two reasons:

  • even if it was explicitly configured with this stamp, it was not handled "transactionally". The userland bus might add it automatically by design and won't care about the context. So the stamp being present after the dispatch in the profiler might be confusing for the user.
  • some logic could exist for messages dispatched transactionally with this stamp. Removing it in the case it was handled directly will prevent triggering such logic unexpectedly.

}

if ($this->isRootDispatchCallRunning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,28 @@ public function testHandleDelayedEventFromQueue()
$messageBus->dispatch($message);
}

public function testDispatchOutOfAnotherHandlerDispatchesAndRemoveStamp()
{
$event = new DummyEvent('First event');

$middleware = new DispatchAfterCurrentBusMiddleware();
$handlingMiddleware = $this->createMock(MiddlewareInterface::class);

$handlingMiddleware
->method('handle')
->with($this->expectHandledMessage($event))
->will($this->willHandleMessage());

$eventBus = new MessageBus([
$middleware,
$handlingMiddleware,
]);

$enveloppe = $eventBus->dispatch($event, [new DispatchAfterCurrentBusStamp()]);

self::assertNull($enveloppe->last(DispatchAfterCurrentBusStamp::class));
}

private function expectHandledMessage($message): Callback
{
return $this->callback(function (Envelope $envelope) use ($message) {
Expand Down