src/EventSubscriber/CommandTerminateEventSubscriber.php line 58

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Command\Impl\AbstractCronCommand;
  4. use App\Entity\CronLog;
  5. use App\Exception\CommandRuntimeException;
  6. use App\Service\CronJobService;
  7. use App\Service\Utils\CommandLocker;
  8. use App\Service\Utils\CommandUniquenessKeyGenerator;
  9. use App\Service\Utils\FlushService;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Component\Console\ConsoleEvents;
  12. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  13. use Symfony\Component\Console\Style\SymfonyStyle;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. /**
  17.  * Auto flushes objects before HTTP responses
  18.  */
  19. class CommandTerminateEventSubscriber implements EventSubscriberInterface
  20. {
  21.     /** @var CronJobService $cronJobService The logger for commands */
  22.     private CronJobService $cronJobService;
  23.     /** @var FlushService $flushService */
  24.     private FlushService $flushService;
  25.     /** @var SessionInterface $session */
  26.     private SessionInterface $session;
  27.     private CommandLocker $commandLocker;
  28.     /**
  29.      * @param LoggerInterface $logger
  30.      * @param CronJobService $cronJobService
  31.      * @param FlushService $flushService
  32.      * @param SessionInterface $session
  33.      * @param CommandLocker $commandLocker
  34.      */
  35.     public function __construct(CronJobService $cronJobServiceFlushService $flushServiceSessionInterface $sessionCommandLocker $commandLocker)
  36.     {
  37.         $this->cronJobService $cronJobService;
  38.         $this->flushService $flushService;
  39.         $this->session $session;
  40.         $this->commandLocker $commandLocker;
  41.     }
  42.     /** @inheritdoc */
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             ConsoleEvents::TERMINATE => ['commandTerminate'1024]
  47.         ];
  48.     }
  49.     public function commandTerminate(ConsoleTerminateEvent $event): void
  50.     {
  51.         if ($event->getCommand() instanceof AbstractCronCommand) {
  52.             $io = new SymfonyStyle($event->getInput(), $event->getOutput());
  53.             $exitCode $event->getExitCode();
  54.             $flushed $this->flushService->flushForCommand($event);
  55.             if ($flushed === false) {
  56.                 $event->setExitCode(98);
  57.                 $io->error("Error when flushing.");
  58.             }
  59.             $uniquenessKey CommandUniquenessKeyGenerator::generate();
  60.             $log $this->cronJobService->findById($this->session->get($uniquenessKey, -1));
  61.             if ($log === null) {
  62.                 $io->error(sprintf("Cannot find log with key %s"$uniquenessKey));
  63.                 throw new CommandRuntimeException();
  64.             } else {
  65.                 $this->cronJobService->closeLog($log, match ($exitCode) {
  66.                     => CronLog::SUCCESS,
  67.                     CronLog::SKIPPED => Cronlog::SKIPPED,
  68.                     CronLog::INTERRUPTED => Cronlog::INTERRUPTED,
  69.                     default => CronLog::FAILURE
  70.                 });
  71.             }
  72.             $uniquenessKeyTime CommandUniquenessKeyGenerator::generateForStartTime();
  73.             $startTime $this->session->get($uniquenessKeyTime);
  74.             if (is_null($startTime)) {
  75.                 $io->error(sprintf("Cannot find the start time with key %s"$uniquenessKeyTime));
  76.                 throw new CommandRuntimeException();
  77.             }
  78.             $io->info('Total execution time : ' number_format((microtime(true) - $startTime), 2) . ' seconds.');
  79.             $this->commandLocker->releaseCommand();
  80.         }
  81.     }
  82. }