Add command to clear and repopulate the latest job order cache. #535
This commit is contained in:
parent
4231c144d0
commit
1101e025a5
3 changed files with 98 additions and 0 deletions
75
src/Command/RefreshLatestActiveJobOrderCacheCommand.php
Normal file
75
src/Command/RefreshLatestActiveJobOrderCacheCommand.php
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
<?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)
|
||||||
|
{
|
||||||
|
// remove all entries in cache
|
||||||
|
$this->jo_cache->clearLatestActiveJobOrderCache();
|
||||||
|
|
||||||
|
$date = new DateTime();
|
||||||
|
$date->modify('-1 day');
|
||||||
|
|
||||||
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -81,6 +81,9 @@ class JobOrderActiveCacheListener
|
||||||
// save in cache
|
// save in cache
|
||||||
$this->jo_cache->addActiveJobOrder($jo);
|
$this->jo_cache->addActiveJobOrder($jo);
|
||||||
|
|
||||||
|
// save in latest JO cache
|
||||||
|
$this->jo_cache->addLatestActiveJoborder($jo);
|
||||||
|
|
||||||
// publish to mqtt
|
// publish to mqtt
|
||||||
$coords = $jo->getCoordinates();
|
$coords = $jo->getCoordinates();
|
||||||
|
|
||||||
|
|
@ -116,6 +119,9 @@ class JobOrderActiveCacheListener
|
||||||
// remove from redis cache
|
// remove from redis cache
|
||||||
$this->jo_cache->removeActiveJobOrder($jo);
|
$this->jo_cache->removeActiveJobOrder($jo);
|
||||||
|
|
||||||
|
// remove from latest JO cache
|
||||||
|
$this->jo_cache->removeLatestActiveJobOrder($jo);
|
||||||
|
|
||||||
// publish to mqtt
|
// publish to mqtt
|
||||||
// send jo status
|
// send jo status
|
||||||
$this->mqtt->publish(
|
$this->mqtt->publish(
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,11 @@ class JobOrderCache
|
||||||
$coords->getLatitude(),
|
$coords->getLatitude(),
|
||||||
$jo->getID()
|
$jo->getID()
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function addLatestActiveJoborder(JobOrder $jo)
|
||||||
|
{
|
||||||
|
$coords = $jo->getCoordinates();
|
||||||
|
|
||||||
// add to JO cache with expiry date
|
// add to JO cache with expiry date
|
||||||
$key = $jo->getID();
|
$key = $jo->getID();
|
||||||
|
|
@ -108,8 +113,20 @@ class JobOrderCache
|
||||||
$this->active_jo_key,
|
$this->active_jo_key,
|
||||||
$jo->getID()
|
$jo->getID()
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeLatestActiveJobOrder(JobOrder $jo)
|
||||||
|
{
|
||||||
// remove key from latest active jo
|
// remove key from latest active jo
|
||||||
$this->redis->hdel($this->latest_active_jo_key, $jo->getID());
|
$this->redis->hdel($this->latest_active_jo_key, $jo->getID());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function clearLatestActiveJobOrderCache()
|
||||||
|
{
|
||||||
|
$keys = $this->redis->hkeys($this->latest_active_jo_key);
|
||||||
|
foreach ($keys as $key)
|
||||||
|
{
|
||||||
|
$this->redis->hdel($this->latest_active_jo_key, $key);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue