src/EventSubscriber/CommandStartEventSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Command\Impl\AbstractCronCommand;
  4. use App\Entity\CronJob;
  5. use App\Entity\CronLog;
  6. use App\Exception\CommandRuntimeException;
  7. use App\Service\CronJobService;
  8. use App\Service\Utils\CommandLocker;
  9. use App\Service\Utils\CommandUniquenessKeyGenerator;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Exception;
  12. use Psr\Log\LoggerInterface;
  13. use Symfony\Component\Console\Command\LockableTrait;
  14. use Symfony\Component\Console\ConsoleEvents;
  15. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  16. use Symfony\Component\Console\Style\SymfonyStyle;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. /**
  23.  * Auto flushes objects before HTTP responses
  24.  */
  25. class CommandStartEventSubscriber implements EventSubscriberInterface
  26. {
  27.     /** @var CronJobService $cronLogger The logger for commands */
  28.     private CronJobService $cronLogger;
  29.     /** @var SessionInterface $session */
  30.     private SessionInterface $session;
  31.     /** @var CommandLocker $commandLocker */
  32.     private CommandLocker $commandLocker;
  33.     /**
  34.      * @param CronJobService $cronLogger
  35.      * @param SessionInterface $session
  36.      * @param CommandLocker $commandLocker
  37.      */
  38.     public function __construct(CronJobService $cronLoggerSessionInterface $sessionCommandLocker $commandLocker)
  39.     {
  40.         $this->cronLogger $cronLogger;
  41.         $this->session $session;
  42.         $this->commandLocker $commandLocker;
  43.     }
  44.     /** @inheritdoc */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             ConsoleEvents::COMMAND => ['commandStart'1024]
  49.         ];
  50.     }
  51.     public function commandStart(ConsoleCommandEvent $event): void
  52.     {
  53.         if ($event->getCommand() instanceof AbstractCronCommand) {
  54.             $this->session->set(CommandUniquenessKeyGenerator::generateForStartTime(), microtime(true));
  55.             $inputString $event->getInput()->__toString();
  56.             $log $this->cronLogger->openLog($event->getCommand(), $inputString);
  57.             $this->session->set(CommandUniquenessKeyGenerator::generate(), $log->getId());
  58.             if (false === $this->commandLocker->lockCommand($inputString)) {
  59.                 throw new CommandRuntimeException('Command already running !'CronLog::SKIPPED);
  60.             }
  61.             $io = new SymfonyStyle($event->getInput(), $event->getOutput());
  62.             $io->info(sprintf('Command %s at %s'$inputString, (new \DateTime())->format('y-m-d H:i:s.u')));
  63.         }
  64.     }
  65. }