envoyer un mail en cas d'erreur sur job cron (ref #159)
This commit is contained in:
parent
3c08e2ef8f
commit
c0517eb238
|
@ -15,9 +15,13 @@ use Cadoles\CronBundle\Entity\Cron;
|
|||
|
||||
class CronCommand extends ContainerAwareCommand
|
||||
{
|
||||
private $container;
|
||||
private $em;
|
||||
private $output;
|
||||
private $filesystem;
|
||||
private $rootlog;
|
||||
|
||||
|
||||
use LockableTrait;
|
||||
|
||||
protected function configure()
|
||||
|
@ -30,8 +34,8 @@ class CronCommand extends ContainerAwareCommand
|
|||
|
||||
protected function execute(InputInterface $input, OutputInterface $output)
|
||||
{
|
||||
$entityManager = $this->getContainer()->get('doctrine')->getManager();
|
||||
|
||||
$this->container = $this->getApplication()->getKernel()->getContainer();
|
||||
$this->em = $this->container->get('doctrine')->getEntityManager();
|
||||
$this->output = $output;
|
||||
$this->filesystem = new Filesystem();
|
||||
$this->rootlog = $this->getContainer()->get('kernel')->getRootDir()."/../var/logs/";
|
||||
|
@ -48,7 +52,7 @@ class CronCommand extends ContainerAwareCommand
|
|||
return 0;
|
||||
}
|
||||
|
||||
$crons = $entityManager->getRepository('CadolesCronBundle:Cron')->toexec();
|
||||
$crons = $this->em->getRepository('CadolesCronBundle:Cron')->toexec();
|
||||
$i=0;
|
||||
|
||||
if($crons) {
|
||||
|
@ -67,7 +71,7 @@ class CronCommand extends ContainerAwareCommand
|
|||
// Dans la synchro il y a un clear du manager ce qui perturbe totalement le manager de Core:Exec
|
||||
// Il pert le lien avec la boucle sur crons
|
||||
// Alors si dans le cron il y a la synchro alors on n'execute que lui le reste sera executé lors du prochain passage
|
||||
$cronsynchro=$entityManager->getRepository('CadolesCronBundle:Cron')->find(100);
|
||||
$cronsynchro=$this->em->getRepository('CadolesCronBundle:Cron')->find(100);
|
||||
if($cronsynchro&&in_array($cronsynchro,$crons)) {
|
||||
$crons=[$cronsynchro];
|
||||
|
||||
|
@ -83,8 +87,8 @@ class CronCommand extends ContainerAwareCommand
|
|||
$now=new \DateTime();
|
||||
$cron->setStartexecdate($now);
|
||||
//$cron->setStatut(1);
|
||||
$entityManager->persist($cron);
|
||||
$entityManager->flush();
|
||||
$this->em->persist($cron);
|
||||
$this->em->flush();
|
||||
|
||||
// Récupération de la commande
|
||||
$command = $this->getApplication()->find($cron->getCommand());
|
||||
|
@ -111,7 +115,7 @@ class CronCommand extends ContainerAwareCommand
|
|||
|
||||
// Revenir sur le cron encours à cause du clear du manager présent dans la synchro
|
||||
// Sinon le manager se pomme et génère des nouveaux enregistrement plutot que mettre à jour celui en cours
|
||||
$cron=$entityManager->getRepository('CadolesCronBundle:Cron')->find($idcron);
|
||||
$cron=$this->em->getRepository('CadolesCronBundle:Cron')->find($idcron);
|
||||
}
|
||||
catch(\Exception $e) {
|
||||
$this->writelnred("JOB EN ERREUR");
|
||||
|
@ -139,11 +143,14 @@ class CronCommand extends ContainerAwareCommand
|
|||
if($returnCode!=1) {
|
||||
$cron->setStatut(3);
|
||||
if($cron->getRepeatcall()>0) $cron->setRepeatexec($cron->getRepeatexec()+1);
|
||||
|
||||
// Envoyer un mail à l'ensemble des administrateurs
|
||||
$this->sendMailerror();
|
||||
}
|
||||
}
|
||||
|
||||
$entityManager->persist($cron);
|
||||
$entityManager->flush();
|
||||
$this->em->persist($cron);
|
||||
$this->em->flush();
|
||||
}
|
||||
|
||||
if($crons) {
|
||||
|
@ -154,6 +161,37 @@ class CronCommand extends ContainerAwareCommand
|
|||
}
|
||||
}
|
||||
|
||||
private function sendMailerror() {
|
||||
$appname="";
|
||||
$config = $this->em->getRepository("CadolesCoreBundle:Config")->findOneBy(["id"=>"appname"]);
|
||||
if($config) $appname = $config->getValue();
|
||||
$noreply = $this->container->getParameter('noreply');
|
||||
|
||||
// Email à l'ensemble administrateurs pour les prévenir qu'il y a une personne à valider
|
||||
$emailadmins= $this->em->createQueryBuilder()
|
||||
->select('table.email')
|
||||
->from("CadolesCoreBundle:User",'table')
|
||||
->where('table.role = :value')
|
||||
->setParameter("value", "ROLE_ADMIN")
|
||||
->getQuery()
|
||||
->getResult(\Doctrine\ORM\Query::HYDRATE_SCALAR);
|
||||
$to=array();
|
||||
$from = $noreply;
|
||||
$fromName = $appname;
|
||||
foreach($emailadmins as $emailadmin) {
|
||||
array_push($to,$emailadmin["email"]);
|
||||
}
|
||||
|
||||
$mail_params=array(
|
||||
"subject" => $appname." : ERREUR SUR EXECUTION JOB",
|
||||
"body_html"=> "ATTENTION UN JOB EST EN ERREUR. MERCI DE VERIFIER LES LOGS.",
|
||||
"body_text"=> "ATTENTION UN JOB EST EN ERREUR. MERCI DE VERIFIER LES LOGS."
|
||||
);
|
||||
|
||||
$message = $this->container->get('cadoles.core.service.mail');
|
||||
$message->sendEmail("template", $mail_params, $to, $from, $fromName);
|
||||
}
|
||||
|
||||
private function writelnred($string) {
|
||||
$this->output->writeln('<fg=red>'.$string.'</>');
|
||||
$this->filesystem->appendToFile($this->rootlog.'cron.log', $string."\n");
|
||||
|
|
Loading…
Reference in New Issue