src/Messenger/EventListener/WorkersRegistryEventListener.php line 44

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2011-present Qualiteam software Ltd. All rights reserved.
  4.  * See https://www.x-cart.com/license-agreement.html for license details.
  5.  */
  6. namespace XCart\Messenger\EventListener;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Symfony\Component\Filesystem\Filesystem;
  9. use Symfony\Component\Messenger\Event\WorkerRunningEvent;
  10. use Symfony\Component\Messenger\Event\WorkerStartedEvent;
  11. use Symfony\Component\Messenger\Event\WorkerStoppedEvent;
  12. final class WorkersRegistryEventListener implements EventSubscriberInterface
  13. {
  14.     private Filesystem $filesystem;
  15.     private string $workersPoolPath;
  16.     /**
  17.      * @var false|int
  18.      */
  19.     private $pid;
  20.     public function __construct(Filesystem $filesystemstring $workersPoolPath)
  21.     {
  22.         $this->filesystem $filesystem;
  23.         $this->workersPoolPath $workersPoolPath;
  24.         $this->pid getmypid();
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             WorkerStartedEvent::class => 'onWorkerStarted',
  30.             WorkerStoppedEvent::class => 'onWorkerStopped',
  31.             WorkerRunningEvent::class => 'onWorkerRunning',
  32.         ];
  33.     }
  34.     public function onWorkerStarted(): void
  35.     {
  36.         $this->filesystem->dumpFile($this->workersPoolPath $this->pidmicrotime(true));
  37.         if (function_exists('pcntl_signal')) {
  38.             pcntl_signal(\SIGINT, function () {
  39.                 $this->onWorkerStopped();
  40.                 // We should only write remove our flag and exit, should not resume execution or wait for graceful stop
  41.                 exit();
  42.             });
  43.         }
  44.     }
  45.     public function onWorkerRunning(): void
  46.     {
  47.         $this->filesystem->dumpFile($this->workersPoolPath $this->pidmicrotime(true));
  48.     }
  49.     public function onWorkerStopped(): void
  50.     {
  51.         $this->filesystem->remove($this->workersPoolPath $this->pid);
  52.     }
  53. }