src/Hitso/Bundle/CommonBundle/EventListener/CronListener.php line 62

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @author Maciej Kaczmarek <maciej.kaczmarek@autentika.pl>
  5.  */
  6. namespace Hitso\Bundle\CommonBundle\EventListener;
  7. use Hitso\Bundle\CommonBundle\Entity\CronLog;
  8. use Hitso\Bundle\CommonBundle\Manager\CronLogManager;
  9. use Symfony\Component\Console\ConsoleEvents;
  10. use Symfony\Component\Console\Event\ConsoleEvent;
  11. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class CronListener implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @var int
  18.      */
  19.     private $startTime;
  20.     /**
  21.      * @var CronLogManager
  22.      */
  23.     private $manager;
  24.     /**
  25.      * @var CronLog
  26.      */
  27.     private $log;
  28.     /**
  29.      * @var string
  30.      */
  31.     private $env;
  32.     /**
  33.      * @var int
  34.      */
  35.     private $ttl;
  36.     public function __construct(CronLogManager $manager$env$ttl)
  37.     {
  38.         $this->manager $manager;
  39.         $this->env     $env;
  40.         $this->ttl     $ttl;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public static function getSubscribedEvents()
  46.     {
  47.         return [
  48.             ConsoleEvents::COMMAND   => 'onCommand',
  49.             ConsoleEvents::TERMINATE => 'onCommandTerminate',
  50.         ];
  51.     }
  52.     public function onCommand(ConsoleEvent $event)
  53.     {
  54.         $event->getCommand()->addOption(
  55.             'cron',
  56.             null,
  57.             InputOption::VALUE_NONE,
  58.             'Mark as CRON command and log its execution status.'
  59.         );
  60.         $input $event->getInput();
  61.         if (!$input->hasParameterOption('--cron')) {
  62.             return;
  63.         }
  64.         $log $this->log $this->manager->initResource();
  65.         $log->setCommand($input->getFirstArgument());
  66.         $log->setFullCommand((string) $input);
  67.         $log->setEnv($this->env);
  68.         $log->setStartedAt(new \DateTime());
  69.         $this->manager->createResource($log);
  70.         $this->startTime microtime(true);
  71.     }
  72.     public function onCommandTerminate(ConsoleTerminateEvent $event)
  73.     {
  74.         if (!$event->getInput()->hasOption('cron') || !$event->getInput()->getOption('cron')) {
  75.             return;
  76.         }
  77.         $time   microtime(true) - $this->startTime;
  78.         $memory memory_get_peak_usage(true);
  79.         $this->log->setFinishedAt(new \DateTime())->setRunningTime($time)->setMemoryUsage($memory)->setExitCode(
  80.             $event->getExitCode()
  81.         );
  82.         $this->manager->updateResource($this->log);
  83.         $this->manager->getRepository()->gc($this->ttl);
  84.         $this->log       null;
  85.         $this->startTime null;
  86.     }
  87. }