8000 Avoid dispatching SendMessageToTransportsEvent on redeliver by weaverryan · Pull Request #30676 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Avoid dispatching SendMessageToTransportsEvent on redeliver #30676

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
Mar 26, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
* The event is *only* dispatched if the message will actually
* be sent to at least one transport. If the message is sent
* to multiple transports, the message is dispatched only one time.
* This message is only dispatched the first time a message
* is sent to a transport, not also if it is retried.
*
* @author Ryan Weaver <ryan@symfonycasts.com>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,21 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
/** @var RedeliveryStamp|null $redeliveryStamp */
$redeliveryStamp = $envelope->last(RedeliveryStamp::class);

$senders = \iterator_to_array($this->sendersLocator->getSenders($envelope, $handle));

if (null !== $this->eventDispatcher && \count($senders) > 0) {
$event = new SendMessageToTransportsEvent($envelope);
$this->eventDispatcher->dispatch($event);
$envelope = $event->getEnvelope();
}

foreach ($senders as $alias => $sender) {
// dispatch event unless this is a redelivery
$shouldDispatchEvent = null === $redeliveryStamp;
foreach ($this->sendersLocator->getSenders($envelope, $handle) as $alias => $sender) {
// on redelivery, only deliver to the given sender
if (null !== $redeliveryStamp && !$redeliveryStamp->shouldRedeliverToSender(\get_class($sender), $alias)) {
continue;
}

if (null !== $this->eventDispatcher && $shouldDispatchEvent) {
$event = new SendMessageToTransportsEvent($envelope);
$this->eventDispatcher->dispatch($event);
$envelope = $event->getEnvelope();
$shouldDispatchEvent = false;
}

$this->logger->info('Sending message "{class}" with "{sender}"', $context + ['sender' => \get_class($sender)]);
$envelope = $sender->send($envelope->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias : null)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function testItSkipsReceivedMessages()
$this->assertNull($envelope->last(SentStamp::class), 'it does not add sent stamp for received messages');
}

public function testItDispatchesTheEventOnceTime()
public function testItDispatchesTheEventOneTime()
{
$envelope = new Envelope(new DummyMessage('original envelope'));

Expand All @@ -224,4 +224,31 @@ public function testItDispatchesTheEventOnceTime()

$middleware->handle($envelope, $this->getStackMock(false));
}

public function testItDoesNotDispatchWithNoSenders()
{
$envelope = new Envelope(new DummyMessage('original envelope'));

$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->never())->method('dispatch');

$middleware = new SendMessageMiddleware(new SendersLocator([]), $dispatcher);

$middleware->handle($envelope, $this->getStackMock());
}

public function testItDoesNotDispatchOnRetry()
{
$envelope = new Envelope(new DummyMessage('original envelope'));
$envelope = $envelope->with(new RedeliveryStamp(3, 'foo_sender'));

$dispatcher = $this->createMock(EventDispatcherInterface::class);
$dispatcher->expects($this->never())->method('dispatch');

$sender = $this->getMockBuilder(SenderInterface::class)->getMock();

$middleware = new SendMessageMiddleware(new SendersLocator([DummyMessage::class => [$sender]]), $dispatcher);

$middleware->handle($envelope, $this->getStackMock(false));
}
}
0