nineskeletor/src/Command/CronCommand.php

135 lines
4.5 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Command\LockableTrait;
use App\Entity\Cron;
class CronCommand extends Command
{
private $container;
private $em;
private $output;
private $filesystem;
private $rootlog;
use LockableTrait;
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->persist($cron);
$this->em->flush();
// Récupération de la commande
$command = $this->getApplication()->find($cron->getCommand());
// Réccuépration des parametres
$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(fmod($cron->getRepeatinterval(),3600)==0)
$next=clone $cron->getNextexecdate();
else
$next=new \DateTime();
$next->add(new \DateInterval('PT'.$cron->getRepeatinterval().'S'));
$cron->setNextexecdate($next);
// Statut OK/KO
$cron->setStatut(($returnCode==Command::FAILURE?0:1));
$this->em->persist($cron);
$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");
}
}