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

Skip to content

[Messenger] add event for all handled messages even failing one #32614

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions UPGRADE-4.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
----
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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
{
}
7 changes: 5 additions & 2 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
);
Expand All @@ -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)]
);

Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down