<?php
namespace App\EventSubscriber;
use App\Command\Impl\AbstractCronCommand;
use App\Entity\CronLog;
use App\Service\CronJobService;
use App\Service\Utils\CommandLocker;
use App\Service\Utils\CommandUniquenessKeyGenerator;
use App\Service\Utils\FlushService;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleSignalEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Auto flushes objects before HTTP responses
*/
class CommandSignalEventSubscriber implements EventSubscriberInterface {
/** @var CronJobService $cronJobService The logger for commands */
private CronJobService $cronJobService;
/** @var SessionInterface $session */
private SessionInterface $session;
private CommandLocker $commandLocker;
private LoggerInterface $logger;
/**
* @param CronJobService $cronJobService
* @param SessionInterface $session
* @param CommandLocker $commandLocker
*/
public function __construct(LoggerInterface $logger, CronJobService $cronJobService, SessionInterface $session, CommandLocker $commandLocker)
{
$this->cronJobService = $cronJobService;
$this->session = $session;
$this->commandLocker = $commandLocker;
$this->logger = $logger;
}
/** @inheritdoc */
public static function getSubscribedEvents() {
return [
ConsoleEvents::SIGNAL => ['commandSignal', 1024]
];
}
public function commandSignal(ConsoleSignalEvent $event): void
{
if ($event->getCommand() instanceof AbstractCronCommand) {
// gets the signal number
$signal = $event->getHandlingSignal();
if (SIGINT === $signal || SIGTERM === $signal) {
$uniquenessKey = CommandUniquenessKeyGenerator::generate();
$log = $this->cronJobService->findById($this->session->get($uniquenessKey, -1));
$this->cronJobService->closeLog($log, CronLog::INTERRUPTED);
$this->commandLocker->releaseCommand();
}
}
}
}