Add initial job order active cache that listens to JobOrder entity events #299

This commit is contained in:
Kendrick Chan 2020-01-24 03:21:14 +08:00
parent fe7cfe6c86
commit ce0d0fb29c
3 changed files with 93 additions and 2 deletions

View file

@ -86,7 +86,6 @@ services:
$host: "%env(REDIS_CLIENT_HOST)%" $host: "%env(REDIS_CLIENT_HOST)%"
$port: "%env(REDIS_CLIENT_PORT)%" $port: "%env(REDIS_CLIENT_PORT)%"
$password: "%env(REDIS_CLIENT_PASSWORD)%" $password: "%env(REDIS_CLIENT_PASSWORD)%"
$env_flag: "dev"
App\Service\GeofenceTracker: App\Service\GeofenceTracker:
arguments: arguments:
@ -204,3 +203,18 @@ services:
App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet" App\Service\GISManagerInterface: "@App\\Service\\GISManager\\OpenStreet"
#App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google" #App\Service\GISManagerInterface: "@App\\Service\\GISManager\\Google"
App\EventListener\JobOrderActiveCache:
arguments:
$redis: "@App\\Service\\RedisClientProvider"
$key: "%env(JO_ACTIVE_CACHE_KEY)%"
$mqtt: "@App\\Service\\MQTTClient"
tags:
- name: 'doctrine.orm.entity_listener'
event: 'postUpdate'
entity: 'App\Entity\JobOrder'
- name: 'doctrine.orm.entity_listener'
event: 'postRemove'
entity: 'App\Entity\JobOrder'
- name: 'doctrine.orm.entity_listener'
event: 'postPersist'
entity: 'App\Entity\JobOrder'

View file

@ -0,0 +1,77 @@
<?php
namespace App\EventListener;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use App\Service\RedisClientProvider;
use App\Ramcar\JOStatus;
use App\Entity\JobOrder;
class JobOrderActiveCache
{
protected $redis;
protected $key;
protected $mqtt;
public function __construct(RedisClientProvider $redis, $key, $mqtt)
{
$this->redis = $redis->getRedisClient();
$this->key = $key;
$this->mqtt = $mqtt;
}
// when a new job order comes in
public function postPersist(JobOrder $jo, LifecycleEventArgs $args)
{
$status = $jo->getStatus();
switch ($status)
{
// active
case JOStatus::PENDING:
case JOStatus::RIDER_ASSIGN:
case JOStatus::ASSIGNED:
case JOStatus::IN_TRANSIT:
case JOStatus::IN_PROGRESS:
$this->processActiveJO($jo);
break;
// inactive
case JOStatus::CANCELLED:
case JOStatus::FULFILLED:
$this->processInactiveJO($jo);
break;
}
}
// when a job order is updated
public function postUpdate(JobOrder $jo, LifecycleEventArgs $args)
{
}
// when a job order is deleted
public function postRemove(JobOrder $jo, LifecycleEventArgs $args)
{
}
protected function processActiveJO($jo)
{
$coords = $jo->getCoordinates();
// put in redis cache
error_log('add ' . $this->key . ' - (' . $coords->getLongitude() . ', ' . $coords->getLatitude() . ') - ' . $jo->getID());
$this->redis->geoadd($this->key, $coords->getLongitude(), $coords->getLatitude(), $jo->getID());
// TODO: publish to mqtt
}
protected function processInactiveJO($jo)
{
// TODO: remove from redis cache
// TODO: publich to mqtt
}
}

View file

@ -26,4 +26,4 @@ default_lat: 14.6091
default_long: 121.0223 default_long: 121.0223
#default_lat: 3.084216 #default_lat: 3.084216
#default_long: 101.6129996 #default_long: 101.6129996
default_region: my default_region: ph