8000 Avoid dispatching SendMessageToTransportsEvent on redeliver · symfony/symfony@dfd8584 · GitHub
[go: up one dir, main page]

Skip to content

Commit dfd8584

Browse files
committed
Avoid dispatching SendMessageToTransportsEvent on redeliver
This purpose of this event is to be a hook when a message is sent to a transport. If that message is redelivered later, that's not the purpose of this hook (there are Worker events for that) and could cause problems if the user unknowingly tries to modify the Envelope in some way, not thinking about how this might be a redelivery message.
1 parent 041f60f commit dfd8584

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

src/Symfony/Component/Messenger/Event/SendMessageToTransportsEvent.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
* The event is *only* dispatched if the message will actually
2121
* be sent to at least one transport. If the message is sent
2222
* to multiple transports, the message is dispatched only one time.
23+
* This message is only dispatched the first time a message
24+
* is sent to a transport, not also if it is retried.
2325
*
2426
* @author Ryan Weaver <ryan@symfonycasts.com>
2527
*/

src/Symfony/Component/Messenger/Middleware/SendMessageMiddleware.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,9 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
6161
} else {
6262
/** @var RedeliveryStamp|null $redeliveryStamp */
6363
$redeliveryStamp = $envelope->last(RedeliveryStamp::class);
64-
6564
$senders = \iterator_to_array($this->sendersLocator->getSenders($envelope, $handle));
6665

67-
if (null !== $this->eventDispatcher && \count($senders) > 0) {
66+
if (null !== $this->eventDispatcher && \count($senders) > 0 && null === $redeliveryStamp) {
6867
$event = new SendMessageToTransportsEvent($envelope);
6968
$this->eventDispatcher->dispatch($event);
7069
$envelope = $event->getEnvelope();

src/Symfony/Component/Messenger/Tests/Middleware/SendMessageMiddlewareTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function testItSkipsReceivedMessages()
205205
$this->assertNull($envelope->last(SentStamp::class), 'it does not add sent stamp for received messages');
206206
}
207207

208-
public function testItDispatchesTheEventOnceTime()
208+
public function testItDispatchesTheEventOneTime()
209209
{
210210
$envelope = new Envelope(new DummyMessage('original envelope'));
211211

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

225225
$middleware->handle($envelope, $this->getStackMock(false));
226226
}
227+
228+
public function testItDoesNotDispatchWithNoSenders()
229+
{
230+
$envelope = new Envelope(new DummyMessage('original envelope'));
231+
232+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
233+
$dispatcher->expects($this->never())->method('dispatch');
234+
235+
$middleware = new SendMessageMiddleware(new SendersLocator([]), $dispatcher);
236+
237+
$middleware->handle($envelope, $this->getStackMock());
238+
}
239+
240+
public function testItDoesNotDispatchOnRetry()
241+
{
242+
$envelope = new Envelope(new DummyMessage('original envelope'));
243+
$envelope = $envelope->with(new RedeliveryStamp(3, 'foo_sender'));
244+
245+
$dispatcher = $this->createMock(EventDispatcherInterface::class);
246+
$dispatcher->expects($this->never())->method('dispatch');
247+
248+
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
249+
250+
$middleware = new SendMessageMiddleware(new SendersLocator([DummyMessage::class => [$sender]]), $dispatcher);
251+
252+
$middleware->handle($envelope, $this->getStackMock(false));
253+
}
227254
}

0 commit comments

Comments
 (0)
0