resq/src/Command/RefreshLatestActiveJobOrderCacheCommand.php
2021-02-09 05:26:33 +00:00

83 lines
2.2 KiB
PHP

<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\JobOrderCache;
use App\Entity\JobOrder;
use App\Ramcar\JOStatus;
use DateTime;
class RefreshLatestActiveJobOrderCacheCommand extends Command
{
protected $em;
protected $jo_cache;
public function __construct(EntityManagerInterface $om, JobOrderCache $jo_cache)
{
$this->em = $om;
$this->jo_cache = $jo_cache;
parent::__construct();
}
protected function configure()
{
$this->setName('joborder:refresh_latest_cache')
->setDescription('Refresh latest active job order cache from database.')
->setHelp('Refresh latest active job order cache from database.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// for logging purposes
$tmp_cache_log = fopen('/tmp/cache_logs.txt', 'a');
// remove all entries in cache
$this->jo_cache->clearLatestActiveJobOrderCache();
fwrite($tmp_cache_log, 'Cleared latest active jo cache...' . "\n");
$date = new DateTime();
$date->modify('-5 hour');
$status_list = [
JOStatus::PENDING,
JOStatus::RIDER_ASSIGN,
JOStatus::ASSIGNED,
JOStatus::IN_TRANSIT,
JOStatus::IN_PROGRESS,
];
$qb = $this->em->getRepository(JobOrder::class)
->createQueryBuilder('jo');
$res = $qb->select('jo')
->where('jo.status IN (:statuses)')
->andWhere('jo.date_schedule >= :date')
->setParameter('statuses', $status_list, Connection::PARAM_STR_ARRAY)
->setParameter('date', $date)
->getQuery()
->execute();
// add each to latest active cache
foreach ($res as $jo)
{
$this->jo_cache->addLatestActiveJoborder($jo);
}
fwrite($tmp_cache_log, 'Refreshed latest active jo cache...' . "\n");
fclose($tmp_cache_log);
return 0;
}
}