vendor/symfony/messenger/EventListener/StopWorkerOnSignalsListener.php line 39

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Messenger\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Console\Command\SignalableCommandInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Messenger\Event\WorkerStartedEvent;
  15. /**
  16.  * @author Tobias Schultze <http://tobion.de>
  17.  * @author GrĂ©goire Pineau <lyrixx@lyrixx.info>
  18.  *
  19.  * @deprecated since Symfony 6.4, use the {@see SignalableCommandInterface} instead
  20.  */
  21. class StopWorkerOnSignalsListener implements EventSubscriberInterface
  22. {
  23.     private array $signals;
  24.     private ?LoggerInterface $logger;
  25.     public function __construct(?array $signals null, ?LoggerInterface $logger null)
  26.     {
  27.         if (null === $signals && \extension_loaded('pcntl')) {
  28.             $signals = [\SIGTERM\SIGINT];
  29.         }
  30.         $this->signals $signals ?? [];
  31.         $this->logger $logger;
  32.     }
  33.     public function onWorkerStarted(WorkerStartedEvent $event): void
  34.     {
  35.         foreach ($this->signals as $signal) {
  36.             pcntl_signal($signal, function () use ($event$signal) {
  37.                 $this->logger?->info('Received signal {signal}.', ['signal' => $signal'transport_names' => $event->getWorker()->getMetadata()->getTransportNames()]);
  38.                 $event->getWorker()->stop();
  39.             });
  40.         }
  41.     }
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         if (!\function_exists('pcntl_signal')) {
  45.             return [];
  46.         }
  47.         return [
  48.             WorkerStartedEvent::class => ['onWorkerStarted'100],
  49.         ];
  50.     }
  51. }