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

Skip to content

Commit bebd354

Browse files
author
Robin Chalas
committed
[Messenger] Add WorkerStoppedEvent
1 parent 77f642e commit bebd354

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
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: 10 additions & 0 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;
@@ -203,6 +204,15 @@ public function testWorkerDispatchesEventsOnSuccess()
203204
});
204205
}
205206

207+
public function testWorkerStoppedEvent()
208+
{
209+
$eventDispatcher = $this->getMockBuilder(EventDispatcherInterface::class)->getMock();
210+
$eventDispatcher->expects($this->once())->method('dispatch')->with($this->isInstanceOf(WorkerStoppedEvent::class));
211+
212+
$worker = new Worker([], $this->getMockBuilder(MessageBusInterface::class)->getMock(), [], $eventDispatcher);
213+
$worker->stop();
214+
}
215+
206216
public function testWorkerDispatchesEventsOnError()
207217
{
208218
$envelope = new Envelope(new DummyMessage('Hello'));

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)