Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #49539 [Messenger] make StopWorkerOnSignalsListener listen by…
… default on SIGTERM and SIGINT (lyrixx) This PR was merged into the 6.3 branch. Discussion ---------- [Messenger] make StopWorkerOnSignalsListener listen by default on SIGTERM and SIGINT | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | Fix #49156 | License | MIT | Doc PR | no need I guess Commits ------- 9415b43 [Messenger] make StopWorkerOnSignalsListener listen by default on SIGTERM and SIGINT
- Loading branch information
Showing
5 changed files
with
70 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/Symfony/Component/Messenger/EventListener/StopWorkerOnSignalsListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Messenger\EventListener; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Messenger\Event\WorkerStartedEvent; | ||
|
||
/** | ||
* @author Tobias Schultze <http://tobion.de> | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class StopWorkerOnSignalsListener implements EventSubscriberInterface | ||
{ | ||
private array $signals; | ||
private ?LoggerInterface $logger; | ||
|
||
public function __construct(array $signals = null, LoggerInterface $logger = null) | ||
{ | ||
if (null === $signals && \defined('SIGTERM')) { | ||
$signals = [SIGTERM, SIGINT]; | ||
} | ||
$this->signals = $signals; | ||
$this->logger = $logger; | ||
} | ||
|
||
public function onWorkerStarted(WorkerStartedEvent $event): void | ||
{ | ||
foreach ($this->signals as $signal) { | ||
pcntl_signal($signal, function () use ($event, $signal) { | ||
$this->logger?->info('Received signal {signal}.', ['signal' => $signal, 'transport_names' => $event->getWorker()->getMetadata()->getTransportNames()]); | ||
|
||
$event->getWorker()->stop(); | ||
}); | ||
} | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
if (!\function_exists('pcntl_signal')) { | ||
return []; | ||
} | ||
|
||
return [ | ||
WorkerStartedEvent::class => ['onWorkerStarted', 100], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters