diff --git a/src/Symfony/Component/Messenger/CHANGELOG.md b/src/Symfony/Component/Messenger/CHANGELOG.md index b16eac2d86285..cfc4403b90324 100644 --- a/src/Symfony/Component/Messenger/CHANGELOG.md +++ b/src/Symfony/Component/Messenger/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 4.3.0 ----- + * Added `WorkerStoppedEvent` dispatched when a worker is stopped. * Added optional `MessageCountAwareInterface` that receivers can implement to give information about how many messages are waiting to be processed. * [BC BREAK] The `Envelope::__construct()` signature changed: diff --git a/src/Symfony/Component/Messenger/Event/WorkerStoppedEvent.php b/src/Symfony/Component/Messenger/Event/WorkerStoppedEvent.php new file mode 100644 index 0000000000000..0d7a37305793c --- /dev/null +++ b/src/Symfony/Component/Messenger/Event/WorkerStoppedEvent.php @@ -0,0 +1,23 @@ + + * + * 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 when a worker has been stopped. + * + * @author Robin Chalas + * + * @experimental in 4.3 + */ +class WorkerStoppedEvent +{ +} diff --git a/src/Symfony/Component/Messenger/Tests/WorkerTest.php b/src/Symfony/Component/Messenger/Tests/WorkerTest.php index 6cc5632ec71c5..b7eeef5a88346 100644 --- a/src/Symfony/Component/Messenger/Tests/WorkerTest.php +++ b/src/Symfony/Component/Messenger/Tests/WorkerTest.php @@ -16,6 +16,7 @@ use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; +use Symfony\Component\Messenger\Event\WorkerStoppedEvent; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; use Symfony\Component\Messenger\MessageBusInterface; use Symfony\Component\Messenger\Retry\RetryStrategyInterface; @@ -187,11 +188,12 @@ public function testWorkerDispatchesEventsOnSuccess() $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); - $eventDispatcher->expects($this->exactly(2)) + $eventDispatcher->expects($this->exactly(3)) ->method('dispatch') ->withConsecutive( [$this->isInstanceOf(WorkerMessageReceivedEvent::class)], - [$this->isInstanceOf(WorkerMessageHandledEvent::class)] + [$this->isInstanceOf(WorkerMessageHandledEvent::class)], + [$this->isInstanceOf(WorkerStoppedEvent::class)] ); $worker = new Worker([$receiver], $bus, [], $eventDispatcher); @@ -214,11 +216,12 @@ public function testWorkerDispatchesEventsOnError() $eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock(); - $eventDispatcher->expects($this->exactly(2)) + $eventDispatcher->expects($this->exactly(3)) ->method('dispatch') ->withConsecutive( [$this->isInstanceOf(WorkerMessageReceivedEvent::class)], - [$this->isInstanceOf(WorkerMessageFailedEvent::class)] + [$this->isInstanceOf(WorkerMessageFailedEvent::class)], + [$this->isInstanceOf(WorkerStoppedEvent::class)] ); $worker = new Worker([$receiver], $bus, [], $eventDispatcher); diff --git a/src/Symfony/Component/Messenger/Worker.php b/src/Symfony/Component/Messenger/Worker.php index 39fa5a910bd8c..134b98e61aace 100644 --- a/src/Symfony/Component/Messenger/Worker.php +++ b/src/Symfony/Component/Messenger/Worker.php @@ -15,6 +15,7 @@ use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent; use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent; use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent; +use Symfony\Component\Messenger\Event\WorkerStoppedEvent; use Symfony\Component\Messenger\Exception\HandlerFailedException; use Symfony\Component\Messenger\Exception\LogicException; use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException; @@ -109,6 +110,8 @@ public function run(array $options = [], callable $onHandledCallback = null): vo usleep($options['sleep']); } } + + $this->dispatchEvent(new WorkerStoppedEvent()); } private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, string $receiverName, ?RetryStrategyInterface $retryStrategy)