src/EventSubscriber/CommandSignalEventSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Command\Impl\AbstractCronCommand;
  4. use App\Entity\CronLog;
  5. use App\Service\CronJobService;
  6. use App\Service\Utils\CommandLocker;
  7. use App\Service\Utils\CommandUniquenessKeyGenerator;
  8. use App\Service\Utils\FlushService;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Exception;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Console\ConsoleEvents;
  13. use Symfony\Component\Console\Event\ConsoleSignalEvent;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. /**
  20.  * Auto flushes objects before HTTP responses
  21.  */
  22. class CommandSignalEventSubscriber implements EventSubscriberInterface {
  23.     /** @var CronJobService $cronJobService The logger for commands */
  24.     private CronJobService $cronJobService;
  25.     /** @var SessionInterface $session */
  26.     private SessionInterface $session;
  27.     private CommandLocker $commandLocker;
  28.     private LoggerInterface $logger;
  29.     /**
  30.      * @param CronJobService $cronJobService
  31.      * @param SessionInterface $session
  32.      * @param CommandLocker $commandLocker
  33.      */
  34.     public function __construct(LoggerInterface $loggerCronJobService $cronJobServiceSessionInterface $sessionCommandLocker $commandLocker)
  35.     {
  36.         $this->cronJobService $cronJobService;
  37.         $this->session $session;
  38.         $this->commandLocker $commandLocker;
  39.         $this->logger $logger;
  40.     }
  41.     /** @inheritdoc */
  42.     public static function getSubscribedEvents() {
  43.         return [
  44.             ConsoleEvents::SIGNAL => ['commandSignal'1024]
  45.         ];
  46.     }
  47.     public function commandSignal(ConsoleSignalEvent $event): void
  48.     {
  49.         if ($event->getCommand() instanceof AbstractCronCommand) {
  50.             // gets the signal number
  51.             $signal $event->getHandlingSignal();
  52.             if (SIGINT === $signal || SIGTERM === $signal) {
  53.                 $uniquenessKey CommandUniquenessKeyGenerator::generate();
  54.                 $log $this->cronJobService->findById($this->session->get($uniquenessKey, -1));
  55.                 $this->cronJobService->closeLog($logCronLog::INTERRUPTED);
  56.                 $this->commandLocker->releaseCommand();
  57.             }
  58.         }
  59.     }
  60. }