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

Skip to content

Commit 4733c22

Browse files
author
Robin Chalas
committed
[Messenger] Add WorkerStoppedEvent
1 parent 27d10a6 commit 4733c22

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
4.3.0
55
-----
66

7+
* Added `WorkerStoppedEvent` dispatched when a worker is stopped.
78
* Added optional `MessageCountAwareInterface` that receivers can implement
89
to give information about how many messages are waiting to be processed.
910
* [BC BREAK] The `Envelope::__construct()` signature changed:
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Messenger\Event;
13+
14+
/**
15+
* Dispatched when a worker has been stopped.
16+
*
17+
* @author Robin Chalas <[email protected]>
18+
*
19+
* @experimental in 4.3
20+
*/
21+
class WorkerStoppedEvent
22+
{
23+
}

src/Symfony/Component/Messenger/Tests/WorkerTest.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
1717
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
1818
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
19+
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
1920
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
2021
use Symfony\Component\Messenger\MessageBusInterface;
2122
use Symfony\Component\Messenger\Retry\RetryStrategyInterface;
@@ -187,11 +188,12 @@ public function testWorkerDispatchesEventsOnSuccess()
187188

188189
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
189190

190-
$eventDispatcher->expects($this->exactly(2))
191+
$eventDispatcher->expects($this->exactly(3))
191192
->method('dispatch')
192193
->withConsecutive(
193194
[$this->isInstanceOf(WorkerMessageReceivedEvent::class)],
194-
[$this->isInstanceOf(WorkerMessageHandledEvent::class)]
195+
[$this->isInstanceOf(WorkerMessageHandledEvent::class)],
196+
[$this->isInstanceOf(WorkerStoppedEvent::class)]
195197
);
196198

197199
$worker = new Worker([$receiver], $bus, [], $eventDispatcher);
@@ -214,11 +216,12 @@ public function testWorkerDispatchesEventsOnError()
214216

215217
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
216218

217-
$eventDispatcher->expects($this->exactly(2))
219+
$eventDispatcher->expects($this->exactly(3))
218220
->method('dispatch')
219221
->withConsecutive(
220222
[$this->isInstanceOf(WorkerMessageReceivedEvent::class)],
221-
[$this->isInstanceOf(WorkerMessageFailedEvent::class)]
223+
[$this->isInstanceOf(WorkerMessageFailedEvent::class)],
224+
[$this->isInstanceOf(WorkerStoppedEvent::class)]
222225
);
223226

224227
$worker = new Worker([$receiver], $bus, [], $eventDispatcher);

src/Symfony/Component/Messenger/Worker.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
1616
use Symfony\Component\Messenger\Event\WorkerMessageHandledEvent;
1717
use Symfony\Component\Messenger\Event\WorkerMessageReceivedEvent;
18+
use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
1819
use Symfony\Component\Messenger\Exception\HandlerFailedException;
1920
use Symfony\Component\Messenger\Exception\LogicException;
2021
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
@@ -174,6 +175,8 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver,
174175

175176
public function stop(): void
176177
{
178+
$this->dispatchEvent(new WorkerStoppedEvent());
179+
177180
$this->shouldStop = true;
178181
}
179182

0 commit comments

Comments
 (0)