diff --git a/src/Command/RefreshLatestActiveJobOrderCacheCommand.php b/src/Command/RefreshLatestActiveJobOrderCacheCommand.php new file mode 100644 index 00000000..d37636de --- /dev/null +++ b/src/Command/RefreshLatestActiveJobOrderCacheCommand.php @@ -0,0 +1,75 @@ +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; + } +} diff --git a/src/EventListener/JobOrderActiveCacheListener.php b/src/EventListener/JobOrderActiveCacheListener.php index 05a6a612..d02bbd09 100644 --- a/src/EventListener/JobOrderActiveCacheListener.php +++ b/src/EventListener/JobOrderActiveCacheListener.php @@ -81,6 +81,9 @@ class JobOrderActiveCacheListener // save in cache $this->jo_cache->addActiveJobOrder($jo); + // save in latest JO cache + $this->jo_cache->addLatestActiveJoborder($jo); + // publish to mqtt $coords = $jo->getCoordinates(); @@ -116,6 +119,9 @@ class JobOrderActiveCacheListener // remove from redis cache $this->jo_cache->removeActiveJobOrder($jo); + // remove from latest JO cache + $this->jo_cache->removeLatestActiveJobOrder($jo); + // publish to mqtt // send jo status $this->mqtt->publish( diff --git a/src/Service/JobOrderCache.php b/src/Service/JobOrderCache.php index b506b68f..c606be73 100644 --- a/src/Service/JobOrderCache.php +++ b/src/Service/JobOrderCache.php @@ -29,6 +29,11 @@ class JobOrderCache $coords->getLatitude(), $jo->getID() ); + } + + public function addLatestActiveJoborder(JobOrder $jo) + { + $coords = $jo->getCoordinates(); // add to JO cache with expiry date $key = $jo->getID(); @@ -108,8 +113,20 @@ class JobOrderCache $this->active_jo_key, $jo->getID() ); + } + public function removeLatestActiveJobOrder(JobOrder $jo) + { // remove key from latest active jo $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); + } + } }