partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

CronLoggerService.php (3595B)


      1 <?php
      2 
      3 namespace PartKeepr\CronLoggerBundle\Services;
      4 
      5 use Doctrine\ORM\EntityManager;
      6 use PartKeepr\CronLoggerBundle\Entity\CronLogger;
      7 use Symfony\Bundle\FrameworkBundle\Console\Application;
      8 use Symfony\Component\Console\Input\ArrayInput;
      9 use Symfony\Component\Console\Output\NullOutput;
     10 use Symfony\Component\HttpKernel\Kernel;
     11 
     12 class CronLoggerService
     13 {
     14     /**
     15      * @var EntityManager
     16      */
     17     private $entityManager;
     18 
     19     /**
     20      * @var Kernel
     21      */
     22     private $kernel;
     23 
     24     public function __construct(EntityManager $entityManager, Kernel $kernel)
     25     {
     26         $this->entityManager = $entityManager;
     27         $this->kernel = $kernel;
     28     }
     29 
     30     /**
     31      * Marks a specific cronjob as ran.
     32      *
     33      * @param string $cronjob The name of the cronjob
     34      *
     35      * @return CronLogger The cron logger entity
     36      */
     37     public function markCronRun($cronjob)
     38     {
     39         $dql = "SELECT c FROM PartKeepr\CronLoggerBundle\Entity\CronLogger c WHERE c.cronjob = :cronjob";
     40         $query = $this->entityManager->createQuery($dql);
     41         $query->setParameter('cronjob', $cronjob);
     42 
     43         try {
     44             $result = $query->getSingleResult();
     45         } catch (\Exception $e) {
     46             $result = new CronLogger();
     47             $result->setCronjob($cronjob);
     48             $this->entityManager->persist($result);
     49         }
     50 
     51         $result->setLastRunDate(new \DateTime());
     52 
     53         $this->entityManager->flush();
     54 
     55         return $result;
     56     }
     57 
     58     /**
     59      * Returns a list of all inactive cronjobs.
     60      *
     61      * @param none
     62      *
     63      * @return array A string of cronjob names which aren't running
     64      */
     65     public function getInactiveCronjobs($requiredCronjobs)
     66     {
     67         $dql = "SELECT c.cronjob FROM PartKeepr\CronLoggerBundle\Entity\CronLogger c WHERE c.cronjob = :cronjob";
     68         $dql .= ' AND c.lastRunDate > :date';
     69 
     70         $query = $this->entityManager->createQuery($dql);
     71 
     72         $date = new \DateTime();
     73         $date->sub(new \DateInterval('P1D'));
     74         $query->setParameter('date', $date);
     75 
     76         $failedCronjobs = [];
     77 
     78         foreach ($requiredCronjobs as $cronjob) {
     79             $query->setParameter('cronjob', $cronjob);
     80 
     81             try {
     82                 $query->getSingleResult();
     83             } catch (\Exception $e) {
     84                 $failedCronjobs[] = $cronjob;
     85             }
     86         }
     87 
     88         return $failedCronjobs;
     89     }
     90 
     91     /**
     92      * Clears all cron logger entries.
     93      */
     94     public function clear()
     95     {
     96         $dql = "DELETE FROM PartKeepr\CronLoggerBundle\Entity\CronLogger c";
     97         $query = $this->entityManager->createQuery($dql);
     98 
     99         $query->execute();
    100     }
    101 
    102     /**
    103      * Runs all crons.
    104      *
    105      * @throws \Exception
    106      */
    107     public function runCrons()
    108     {
    109         $this->entityManager->beginTransaction();
    110         $repository = $this->entityManager->getRepository('PartKeeprCronLoggerBundle:CronLogger');
    111 
    112         $cronJobs = $repository->findAll();
    113 
    114         $application = new Application($this->kernel);
    115         $application->setAutoExit(false);
    116         $output = new NullOutput();
    117 
    118         $minRunDate = new \DateTime();
    119         $minRunDate->sub(new \DateInterval('PT6H'));
    120 
    121         foreach ($cronJobs as $cronJob) {
    122             if ($minRunDate->getTimestamp() - $cronJob->getLastRunDate()->getTimestamp() < 0) {
    123                 break;
    124             }
    125 
    126             $command = $cronJob->getCronjob();
    127 
    128             $input = new ArrayInput([
    129                 'command' => $command,
    130             ]);
    131 
    132             $application->run($input, $output);
    133         }
    134 
    135         $this->entityManager->commit();
    136     }
    137 }