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

Skip to content

Commit 96a7907

Browse files
committed
feature #31282 [Messenger] Add WorkerStoppedEvent (chalasr)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Messenger] Add WorkerStoppedEvent | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a In 4.2, one was able to decorate a transport and hook into `TransportInterface::stop()` to perform some business tasks when the `messenger:consume` process gets killed. In 4.3 the `stop()` logic has been moved to `Worker` which cannot be decorated when used via `messenger:consume`. This PR adds a marker event dispatched when the worker is stopped to provide the same capability. Briefly discussed with @weaverryan. My use case: I have a temporary CSV file locally which grows while consuming jobs. This file is uploaded to AWS S3 and squashed once a while (every 5minutes). It is also uploaded when the `messenger:consume` process gets stopped (to store the remaining lines). For the former (time-based upload), I can listen on `WorkflowMessageHandledEvent` to make the upload happens in case the 5 minutes delay is elapsed. This solves the latter. Commits ------- 0e7898b [Messenger] Add WorkerStoppedEvent
2 parents f24e9a4 + 0e7898b commit 96a7907

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;
@@ -109,6 +110,8 @@ public function run(array $options = [], callable $onHandledCallback = null): vo
109110
usleep($options['sleep']);
110111
}
111112
}
113+
114+
$this->dispatchEvent(new WorkerStoppedEvent());
112115
}
113116

114117
private function handleMessage(Envelope $envelope, ReceiverInterface $receiver, string $receiverName, ?RetryStrategyInterface $retryStrategy)

0 commit comments

Comments
 (0)