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

Skip to content

[Messenger] Log when worker should stop and when SIGTERM is received #42723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@
->tag('monolog.logger', ['channel' => 'messenger'])

->set('messenger.listener.stop_worker_on_sigterm_signal_listener', StopWorkerOnSigtermSignalListener::class)
->args([
service('logger')->ignoreOnInvalid(),
])
->tag('kernel.event_subscriber')

->set('messenger.listener.stop_worker_on_stop_exception_listener', StopWorkerOnCustomStopExceptionListener::class)
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CHANGELOG
* Added `WorkerMetadata` class which allows you to access the configuration details of a worker, like `queueNames` and `transportNames` it consumes from.
* New method `getMetadata()` was added to `Worker` class which returns the `WorkerMetadata` object.
* Deprecate not setting the `reset_on_message` config option, its default value will change to `true` in 6.0
* Add log when worker should stop.
* Add log when `SIGTERM` is received.

5.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\EventListener;

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Messenger\Event\WorkerStartedEvent;

Expand All @@ -19,9 +20,20 @@
*/
class StopWorkerOnSigtermSignalListener implements EventSubscriberInterface
{
private $logger;

public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

public function onWorkerStarted(WorkerStartedEvent $event): void
{
pcntl_signal(\SIGTERM, static function () use ($event) {
pcntl_signal(\SIGTERM, function () use ($event) {
if (null !== $this->logger) {
$this->logger->info('Received SIGTERM signal.');
}

$event->getWorker()->stop();
});
}
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/Messenger/Tests/WorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Event\WorkerMessageFailedEvent;
Expand Down Expand Up @@ -329,6 +330,16 @@ public function testWorkerMessageReceivedEventMutability()
$envelope = current($receiver->getAcknowledgedEnvelopes());
$this->assertCount(1, $envelope->all(\get_class($stamp)));
}

public function testWorkerShouldLogOnStop()
{
$bus = $this->createMock(MessageBusInterface::class);
$logger = $this->createMock(LoggerInterface::class);
$logger->expects($this->once())->method('info')->with('Stopping worker.');
$worker = new Worker([], $bus, new EventDispatcher(), $logger);

$worker->stop();
}
}

class DummyReceiver implements ReceiverInterface
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Messenger/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ private function handleMessage(Envelope $envelope, ReceiverInterface $receiver,

public function stop(): void
{
if (null !== $this->logger) {
$this->logger->info('Stopping worker.');
}

$this->shouldStop = true;
}

Expand Down