vendor/symfony/messenger/EventListener/StopWorkerOnSigtermSignalListener.php line 30

Open in your IDE?
  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\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\Messenger\Event\WorkerStartedEvent;
  14. /**
  15. * @author Tobias Schultze <http://tobion.de>
  16. */
  17. class StopWorkerOnSigtermSignalListener implements EventSubscriberInterface
  18. {
  19. private $logger;
  20. public function __construct(?LoggerInterface $logger = null)
  21. {
  22. $this->logger = $logger;
  23. }
  24. public function onWorkerStarted(WorkerStartedEvent $event): void
  25. {
  26. pcntl_signal(\SIGTERM, function () use ($event) {
  27. if (null !== $this->logger) {
  28. $this->logger->info('Received SIGTERM signal.', ['transport_names' => $event->getWorker()->getMetadata()->getTransportNames()]);
  29. }
  30. $event->getWorker()->stop();
  31. });
  32. }
  33. public static function getSubscribedEvents()
  34. {
  35. if (!\function_exists('pcntl_signal')) {
  36. return [];
  37. }
  38. return [
  39. WorkerStartedEvent::class => ['onWorkerStarted', 100],
  40. ];
  41. }
  42. }