8000 feature #42723 [Messenger] Log when worker should stop and when `SIGT… · JohJohan/symfony@7993530 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7993530

Browse files
committed
feature symfony#42723 [Messenger] Log when worker should stop and when SIGTERM is received (ruudk)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Messenger] Log when worker should stop and when `SIGTERM` is received | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | These two small log lines can help debugging crashing workers. Sometimes it's hard to tell if the worker crashed or is shutting down. Also, when working on gracefully shutdown problems, it's helpful to know when a `SIGTERM` was received and the worker is about to stop. Commits ------- 02f86c5 [Messenger] Log when worker should stop and when `SIGTERM` is received
2 parents 428434c + 02f86c5 commit 7993530

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/messenger.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@
193193
->tag('monolog.logger', ['channel' => 'messenger'])
194194

195195
->set('messenger.listener.stop_worker_on_sigterm_signal_listener', StopWorkerOnSigtermSignalListener::class)
196+
->args([
197+
service('logger')->ignoreOnInvalid(),
198+
])
196199
->tag('kernel.event_subscriber')
197200

198201
->set('messenger.listener.stop_worker_on_stop_exception_listener', StopWorkerOnCustomStopExceptionListener::class)

src/Symfony/Component/Messenger/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ CHANGELOG
99
* Added `WorkerMetadata` class which allows you to access the configuration details of a worker, like `queueNames` and `transportNames` it consumes from.
1010
* New method `getMetadata()` was added to `Worker` class which returns the `WorkerMetadata` object.
1111
* Deprecate not setting the `reset_on_message` config option, its default value will change to `true` in 6.0
12+
* Add log when worker should stop.
13+
* Add log when `SIGTERM` is received.
1214

1315
5.3
1416
---

src/Symfony/Component/Messenger/EventListener/StopWorkerOnSigtermSignalListener.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Messenger\EventListener;
1313

14+
use Psr\Log\LoggerInterface;
1415
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1516
use Symfony\Component\Messenger\Event\WorkerStartedEvent;
1617

@@ -19,9 +20,20 @@
1920
*/
2021
class StopWorkerOnSigtermSignalListener implements EventSubscriberInterface
2122
{
23+
private $logger;
24+
25+
public function __construct(LoggerInterface $logger = null)
26+
{
27+
$this->logger = $logger;
28+
}
29+
2230
public function onWorkerStarted(WorkerStartedEvent $event): void
2331
{
24-
pcntl_signal(\SIGTERM, static function () use ($event) {
32+
pcntl_signal(\SIGTERM, function () use ($event) {
33+
if (null !== $this->logger) {
34+
$this->logger->info('Received SIGTERM signal.');
35+
}
36+
2537
$event->getWorker()->stop();
2638
});
2739
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Messenger\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
1516
use Symfony\Component\EventDispatcher\EventDispatcher;
1617
use Symfony\Component\Messenger\Envelope;
1718
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
@@ -329,6 +330,16 @@ public function testWorkerMessageReceivedEventMutability()
329330
$envelope = current($receiver->getAcknowledgedEnvelopes());
330331
$this->assertCount(1, $envelope->all(\get_class($stamp)));
331332
}
333+
334+
public function testWorkerShouldLogOnStop()
335+
{
336+
$bus = $this->createMock(MessageBusInterface::class);
337+
$logger = $this->createMock(LoggerInterface::class);
338+
$logger->expects($this->once())->method('info')->with('Stopping worker.');
339+
$worker = new Worker([], $bus, new EventDispatcher(), $logger);
340+
341+
$worker->stop();
342+
}
332343
}
333344

334345
class DummyReceiver implements ReceiverInterface

src/Symfony/Component/Messenger/Worker.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver,
176176

177177
public function stop(): void
178178
{
179+
if (null !== $this->logger) {
180+
$this->logger->info('Stopping worker.');
181+
}
182+
179183
$this->shouldStop = true;
180184
}
181185

0 commit comments

Comments
 (0)
0