nineskeletor/src/Command/CronCommand.php

140 lines
4.4 KiB
PHP
Executable File

<?php
namespace App\Command;
use App\Entity\Cron;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Filesystem\Filesystem;
class CronCommand extends Command
{
use LockableTrait;
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
public function __construct(ContainerInterface $container, EntityManagerInterface $em)
{
parent::__construct();
$this->container = $container;
$this->em = $em;
}
protected function configure()
{
$this
->setName('app:Cron')
->setDescription('Execution of the cron command')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->filesystem = new Filesystem();
$this->rootlog = $this->container->get('kernel')->getLogDir().'/';
if (!$this->lock()) {
$this->output->writeln('CRON LOCK');
return Command::FAILURE;
}
$crons = $this->em->getRepository('App\Entity\Cron')->toexec();
if ($crons) {
$now = new \DateTime();
$this->writelnred('');
$this->writelnred('');
$this->writelnred('');
$this->writelnred('');
$this->writelnred('==========================================================================================================');
$this->writelnred('== CRON ==================================================================================================');
$this->writelnred('==========================================================================================================');
$this->writeln('Date = '.$now->format('Y-m-d H:i:s'));
$this->writeln('Application = '.$this->container->getParameter('appName'));
}
foreach ($crons as $cron) {
// Id du cron
$idcron = $cron->getId();
// Flag d'execution en cours
$now = new \DateTime();
$cron->setStartexecdate($now);
// $cron->setStatut(1);
$this->em->flush();
// Récupération de la commande
$command = $this->getApplication()->find($cron->getCommand());
// Réccuépration des parametres
$jsonparameter = null;
if ($cron->getJsonargument()) {
$jsonparameter = json_decode($cron->getJsonargument(), true);
}
// Formater la chaine de parametre
if (!$jsonparameter) {
$jsonparameter = [];
}
$parameter = new ArrayInput($jsonparameter);
// Executer la commande
try {
$returnCode = $command->run($parameter, $output);
} catch (\Exception $e) {
$this->writelnred('JOB EN ERREUR .'.$e->getMessage());
$returnCode = Command::FAILURE;
}
// Flag de fin d'execution
$now = new \DateTime();
$cron->setEndexecdate($now);
// Si interval par heure
if (0 == fmod($cron->getRepeatinterval(), 3600)) {
$next = clone $cron->getNextexecdate();
} else {
$next = new \DateTime();
}
$next->add(new \DateInterval('PT'.$cron->getRepeatinterval().'S'));
$cron->setNextexecdate($next);
// Statut OK/KO
$cron->setStatut(Command::FAILURE == $returnCode ? 0 : 1);
$this->em->flush();
}
if ($crons) {
$this->writelnred('==');
$this->writelnred('FIN CRON');
$this->writelnred('==');
$this->writelnred('');
}
return Command::SUCCESS;
}
private function writelnred($string)
{
$this->output->writeln('<fg=red>'.$string.'</>');
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
private function writeln($string)
{
$this->output->writeln($string);
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
}
}