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.         try {
  53.             $this->runCommand("telegram:updates",[],$output);
  54.         }
  55.         catch (\Throwable $ex)
  56.         {
  57.             $output->writeln("Telegram failed: ".$ex->getMessage());
  58.         }
  59.         $this->runCommand("kmg:runallocator",[],$output);
  60.         $this->confirmRides($this->log);
  61.         try {
  62.             $this->runCommand('messenger:consume', [
  63.                 "receivers" => ['async'],
  64.                 '--time-limit' => 30,
  65.             ], $output);
  66.         }
  67.         catch (\Throwable $ex)
  68.         {
  69.             $output->writeln("Messenger failed: ".$ex->getMessage());
  70.         }
  71.     }
  72.     protected function confirmRides(LoggerInterface  $log)
  73.     {
  74.         $repo $this->em->getRepository(Order::class);
  75.         $timeThreshold = (new \DateTime())->modify("- 120 minutes"); // fahrt schon mind. 2h alt
  76.         $oldThreshold = (new \DateTime())->modify("- 180 minutes");   // aber fahrten nicht älter als 3h
  77.         $qb $repo
  78.             ->createQueryBuilder('A')
  79.             ->leftJoin('A.customer','C')
  80.             ->andWhere('A.assignedTo is not null')
  81.             ->andWhere('A.assignmentConfirmed = 1')
  82.             ->andWhere('A.orderStatus = :status')->setParameter('status',OrderStatus::STATUS_VERMITTELT)
  83.             ->andWhere('A.orderTime <= :time')->setParameter('time',$timeThreshold->format('Y-m-d H:i:s'))
  84.             ->andWhere('A.orderTime >= :old')->setParameter('old',$oldThreshold->format('Y-m-d H:i:s'));
  85.         $orders $qb->getQuery()->getResult();
  86.         $lmm sprintf('%d Bestellungen im Status vermittelt werden aktualisiert',count($orders));
  87.         $log->info($lmm);
  88.         if (count($orders)>0)
  89.         {
  90.             SysLogRepository::logMessage($this->em->getConnection(),SysLogEntry::SYS_INFO,$lmm);
  91.         }
  92.         $stat $this->em->getReference(OrderStatus::class, OrderStatus::STATUS_FINISHED);
  93.         /** @var Order $order */
  94.         foreach ($orders as $order)
  95.         {
  96.             $order->setOrderStatus($stat);
  97.         }
  98.         $this->em->flush();
  99.     }
  100.     protected function execute(InputInterface $inputOutputInterface $output): int
  101.     {
  102.         // todo: refactor so that only error messages get printed.
  103.         // todo: by that we can make use of the fact that console output can be automatically mailed to the admin
  104.         $output->writeln(date("[Y-m-d H:i]")." >> CRON START ");
  105.         // check for execution lock
  106.         $lockFile $this->params->get('dx.temp_dir')."cron.lock";
  107.         $lastRunFile $this->params->get('dx.temp_dir')."cron.lastrun";
  108.         if (file_exists($lockFile))
  109.         {
  110.             $lockinfo file_get_contents($lockFile);
  111.             $output->writeln(sprintf("There is already a running instance of CronCommand (Lock: %s). Aborting.",$lockinfo));
  112.             return \Symfony\Component\Console\Command\Command::FAILURE;
  113.         }
  114.         try
  115.         {
  116.             $r file_put_contents($lockFile,date("d.m.Y H:i.s"),LOCK_EX);
  117.             if ($r === false)
  118.             {
  119.                 $output->writeln("Unable to aquire a lock. Aborting.");
  120.                 return \Symfony\Component\Console\Command\Command::FAILURE;
  121.             }
  122.             // Run the actual commands
  123.             $this->actuallyExecute($input,$output);
  124.             file_put_contents($lastRunFile,date("U"));
  125.         }
  126.         finally
  127.         {
  128.             // remove lock
  129.             $r unlink($lockFile);
  130.             if (!$r)
  131.             {
  132.                 $output->writeln("Warning: Unable to remove lock.");
  133.             }
  134.         }
  135.         // ... and we're done
  136.         $output->writeln(date("[Y-m-d H:i]")." >> CRON END ");
  137.         return \Symfony\Component\Console\Command\Command::SUCCESS;
  138.     }
  139. }