src/Diplix/KMGBundle/Console/Command/CronCommand.php line 38

Open in your IDE?
  1. <?php
  2. namespace Diplix\KMGBundle\Console\Command;
  3. use Diplix\Commons\DataHandlingBundle\Entity\SysLogEntry;
  4. use Diplix\Commons\DataHandlingBundle\Repository\SysLogRepository;
  5. use Diplix\KMGBundle\Entity\Order;
  6. use Diplix\KMGBundle\Entity\OrderStatus;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\Console\Exception\CommandNotFoundException;
  10. use Symfony\Component\Console\Input\ArrayInput;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. /**
  15.  * Wrap all commands to be run with a single cron call in one command
  16.  */
  17. class CronCommand extends BaseCommand
  18. {
  19.     public function __construct(
  20.         protected EntityManagerInterface $em,
  21.         protected LoggerInterface $log,
  22.         protected ParameterBagInterface $params,
  23.         ?string $name null)
  24.     {
  25.         parent::__construct($name);
  26.     }
  27.     protected function configure()
  28.     {
  29.         $this
  30.             ->setName('kmg:cron')
  31.         ;
  32.     }
  33.     protected function runCommand($commandName,$arguments = array(),OutputInterface $output)
  34.     {
  35.         try
  36.         {
  37.             $command $this->getApplication()->find($commandName);
  38.             $cmdIn = new ArrayInput($arguments);
  39.             $command->run($cmdIn,$output);
  40.             return true;
  41.         }
  42.         catch (CommandNotFoundException $ex)
  43.         {
  44.             $output->writeln("<error>Command not found: ".$commandName."</error>");
  45.             return false;
  46.         }
  47.     }
  48.     protected function actuallyExecute(InputInterface $inputOutputInterface $output)
  49.     {
  50.         // $this->runCommand("tami:updatestatus",array(),$output);
  51.         $this->runCommand("notifications:send",[],$output);
  52.         $this->runCommand("telegram:updates",[],$output);
  53.         $this->runCommand("kmg:runallocator",[],$output);
  54.         $this->confirmRides($this->log);
  55.     }
  56.     protected function confirmRides(LoggerInterface  $log)
  57.     {
  58.         $repo $this->em->getRepository(Order::class);
  59.         $timeThreshold = (new \DateTime())->modify("- 120 minutes"); // fahrt schon mind. 2h alt
  60.         $oldThreshold = (new \DateTime())->modify("- 180 minutes");   // aber fahrten nicht älter als 3h
  61.         $qb $repo
  62.             ->createQueryBuilder('A')
  63.             ->leftJoin('A.customer','C')
  64.             ->andWhere('A.assignedTo is not null')
  65.             ->andWhere('A.assignmentConfirmed = 1')
  66.             ->andWhere('A.orderStatus = :status')->setParameter('status',OrderStatus::STATUS_VERMITTELT)
  67.             ->andWhere('A.orderTime <= :time')->setParameter('time',$timeThreshold->format('Y-m-d H:i:s'))
  68.             ->andWhere('A.orderTime >= :old')->setParameter('old',$oldThreshold->format('Y-m-d H:i:s'));
  69.         $orders $qb->getQuery()->getResult();
  70.         $lmm sprintf('%d Bestellungen im Status vermittelt werden aktualisiert',count($orders));
  71.         $log->info($lmm);
  72.         if (count($orders)>0)
  73.         {
  74.             SysLogRepository::logMessage($this->em->getConnection(),SysLogEntry::SYS_INFO,$lmm);
  75.         }
  76.         $stat $this->em->getReference(OrderStatus::class, OrderStatus::STATUS_FINISHED);
  77.         /** @var Order $order */
  78.         foreach ($orders as $order)
  79.         {
  80.             $order->setOrderStatus($stat);
  81.         }
  82.         $this->em->flush();
  83.     }
  84.     protected function execute(InputInterface $inputOutputInterface $output): int
  85.     {
  86.         // todo: refactor so that only error messages get printed.
  87.         // todo: by that we can make use of the fact that console output can be automatically mailed to the admin
  88.         $output->writeln(date("[Y-m-d H:i]")." >> CRON START ");
  89.         // check for execution lock
  90.         $lockFile $this->params->get('dx.temp_dir')."cron.lock";
  91.         $lastRunFile $this->params->get('dx.temp_dir')."cron.lastrun";
  92.         if (file_exists($lockFile))
  93.         {
  94.             $lockinfo file_get_contents($lockFile);
  95.             $output->writeln(sprintf("There is already a running instance of CronCommand (Lock: %s). Aborting.",$lockinfo));
  96.             return \Symfony\Component\Console\Command\Command::FAILURE;
  97.         }
  98.         try
  99.         {
  100.             $r file_put_contents($lockFile,date("d.m.Y H:i.s"),LOCK_EX);
  101.             if ($r === false)
  102.             {
  103.                 $output->writeln("Unable to aquire a lock. Aborting.");
  104.                 return \Symfony\Component\Console\Command\Command::FAILURE;
  105.             }
  106.             // Run the actual commands
  107.             $this->actuallyExecute($input,$output);
  108.             file_put_contents($lastRunFile,date("U"));
  109.         }
  110.         finally
  111.         {
  112.             // remove lock
  113.             $r unlink($lockFile);
  114.             if (!$r)
  115.             {
  116.                 $output->writeln("Warning: Unable to remove lock.");
  117.             }
  118.         }
  119.         // ... and we're done
  120.         $output->writeln(date("[Y-m-d H:i]")." >> CRON END ");
  121.         return \Symfony\Component\Console\Command\Command::SUCCESS;
  122.     }
  123. }