diff --git a/UPGRADE-4.4.md b/UPGRADE-4.4.md index d6f6cbe529b8d..d260010b0a314 100644 --- a/UPGRADE-4.4.md +++ b/UPGRADE-4.4.md @@ -168,6 +168,8 @@ Messenger * Deprecated passing a `ContainerInterface` instance as first argument of the `ConsumeMessagesCommand` constructor, pass a `RoutableMessageBus` instance instead. + * `WorkerMessageHandledEvent` is now dispatched on each handled message instead of only successful one. Use `WorkerMessageHandledSuccessfullyEvent` + to get the same behavior as before. Mime ---- diff --git a/src/Symfony/Component/Messenger/CHANGELOG.md b/src/Symfony/Component/Messenger/CHANGELOG.md index 68d180d0e7ff7..87e5cbc026fd7 100644 --- a/src/Symfony/Component/Messenger/CHANGELOG.md +++ b/src/Symfony/Component/Messenger/CHANGELOG.md @@ -12,6 +12,8 @@ CHANGELOG * Made all dispatched worker event classes final. * Added support for `from_transport` attribute on `messenger.message_handler` tag. * Added support for passing `dbindex` as a query parameter to the redis transport DSN. + * [BC BREAK] `WorkerMessageHandledEvent` is now dispatched on each handled message instead of only successful one. + * Added `WorkerMessageHandledSuccessfullyEvent` dispatched for each successful message. 4.3.0 ----- diff --git a/src/Symfony/Component/Messenger/Event/WorkerMessageHandledEvent.php b/src/Symfony/Component/Messenger/Event/WorkerMessageHandledEvent.php index 3c4a03037f8ac..4168f079f238e 100644 --- a/src/Symfony/Component/Messenger/Event/WorkerMessageHandledEvent.php +++ b/src/Symfony/Component/Messenger/Event/WorkerMessageHandledEvent.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Messenger\Event; /** - * Dispatched after a message was received from a transport and successfully handled. + * Dispatched after a message was received from a transport and handled. * * The event name is the class name. */ diff --git a/src/Symfony/Component/Messenger/Event/WorkerMessageHandledSuccessfullyEvent.php b/src/Symfony/Component/Messenger/Event/WorkerMessageHandledSuccessfullyEvent.php new file mode 100644 index 0000000000000..0941872e97788 --- /dev/null +++ b/src/Symfony/Component/Messenger/Event/WorkerMessageHandledSuccessfullyEvent.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Messenger\Event; + +/** + * Dispatched after a message was received from a transport and successfully handled. + * + * The event name is the class name. + */ +class WorkerMessageHandledSuccessfullyEvent extends AbstractWorkerMessageEvent +{ +} diff --git a/src/Symfony/Component/Messenger/Tests/WorkerTest.php b/src/Symfony/Component/Messenger/Tests/WorkerTest.php index ad7477253e86b..9de7cff887bf2 100644 --- a/src/Symfony/Component/Messenger/Tests/WorkerTest.php +++ b/src/Symfony/Component/Messenger/Tests/WorkerTest.php @@ -15,6 +15,7 @@ use Symfony\Component\Messenger\Envelope; use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; +use Symfony\Component\Messenger\Event\WorkerMessageHandledSuccessfullyEvent; use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; use Symfony\Component\Messenger\Event\WorkerStoppedEvent; use Symfony\Component\Messenger\Exception\HandlerFailedException; @@ -226,10 +227,11 @@ public function testWorkerDispatchesEventsOnSuccess() $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); - $eventDispatcher->expects($this->exactly(3)) + $eventDispatcher->expects($this->exactly(4)) ->method('dispatch') ->withConsecutive( [$this->isInstanceOf(WorkerMessageReceivedEvent::class)], + [$this->isInstanceOf(WorkerMessageHandledSuccessfullyEvent::class)], [$this->isInstanceOf(WorkerMessageHandledEvent::class)], [$this->isInstanceOf(WorkerStoppedEvent::class)] ); @@ -254,11 +256,12 @@ public function testWorkerDispatchesEventsOnError() $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); - $eventDispatcher->expects($this->exactly(3)) + $eventDispatcher->expects($this->exactly(4)) ->method('dispatch') ->withConsecutive( [$this->isInstanceOf(WorkerMessageReceivedEvent::class)], [$this->isInstanceOf(WorkerMessageFailedEvent::class)], + [$this->isInstanceOf(WorkerMessageHandledEvent::class)], [$this->isInstanceOf(WorkerStoppedEvent::class)] ); diff --git a/src/Symfony/Component/Messenger/Worker.php b/src/Symfony/Component/Messenger/Worker.php index caad767472fc9..b348bc0ad3a2e 100644 --- a/src/Symfony/Component/Messenger/Worker.php +++ b/src/Symfony/Component/Messenger/Worker.php @@ -16,6 +16,7 @@ use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy; use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; +use Symfony\Component\Messenger\Event\WorkerMessageHandledSuccessfullyEvent; use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; use Symfony\Component\Messenger\Event\WorkerStoppedEvent; use Symfony\Component\Messenger\Exception\HandlerFailedException; @@ -91,6 +92,7 @@ public function run(array $options = [], callable $onHandledCallback = null): vo $envelopeHandled = true; $this->handleMessage($envelope, $receiver, $transportName, $this->retryStrategies[$transportName] ?? null); + $this->dispatchEvent(new WorkerMessageHandledEvent($envelope, $transportName)); $onHandled($envelope); if ($this->shouldStop) { @@ -170,7 +172,7 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, return; } - $this->dispatchEvent(new WorkerMessageHandledEvent($envelope, $transportName)); + $this->dispatchEvent(new WorkerMessageHandledSuccessfullyEvent($envelope, $transportName)); if (null !== $this->logger) { $this->logger->info('{class} was handled successfully (acknowledging to transport).', $context);