Skip to content

Commit

Permalink
Carpool event hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Francia Csaba committed Aug 1, 2024
1 parent 7db9706 commit 6b42501
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
27 changes: 8 additions & 19 deletions amarillo/routers/carpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from amarillo.models.Carpool import Carpool
from amarillo.routers.agencyconf import verify_api_key, verify_permission_for_same_agency_or_admin
from amarillo.tests.sampledata import examples

from amarillo.services.hooks import run_on_create, run_on_delete
from amarillo.services.config import config
from amarillo.utils.utils import assert_folder_exists

Expand Down Expand Up @@ -51,6 +51,8 @@ async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Bod
requesting_agency_id: str = Depends(verify_api_key)) -> Carpool:
await verify_permission_for_same_agency_or_admin(carpool.agency, requesting_agency_id)

background_tasks.add_task(run_on_create, carpool)

logger.info(f"POST trip {carpool.agency}:{carpool.id}.")
await assert_agency_exists(carpool.agency)

Expand Down Expand Up @@ -90,12 +92,14 @@ async def get_carpool(agency_id: str, carpool_id: str, api_key: str = Depends(ve
"description": "Carpool or agency not found"},
},
)
async def delete_carpool(agency_id: str, carpool_id: str, requesting_agency_id: str = Depends(verify_api_key)):
async def delete_carpool(background_tasks: BackgroundTasks, agency_id: str, carpool_id: str, requesting_agency_id: str = Depends(verify_api_key)):
await verify_permission_for_same_agency_or_admin(agency_id, requesting_agency_id)

logger.info(f"Delete trip {agency_id}:{carpool_id}.")
await assert_agency_exists(agency_id)
await assert_carpool_exists(agency_id, carpool_id)
cp = await load_carpool(agency_id, carpool_id)
background_tasks.add_task(run_on_delete, cp)

return await _delete_carpool(agency_id, carpool_id)

Expand All @@ -111,12 +115,6 @@ async def _delete_carpool(agency_id: str, carpool_id: str):
os.remove(f"data/enhanced/{agency_id}/{carpool_id}.json", )
except FileNotFoundError:
pass

try:
from amarillo.plugins.metrics import trips_deleted_counter
trips_deleted_counter.inc()
except ImportError:
pass


async def store_carpool(carpool: Carpool) -> Carpool:
Expand All @@ -125,17 +123,6 @@ async def store_carpool(carpool: Carpool) -> Carpool:
await set_lastUpdated_if_unset(carpool)
await save_carpool(carpool)

try:
from amarillo.plugins.metrics import trips_created_counter, trips_updated_counter
if(carpool_exists):
# logger.info("Incrementing trips updated")
trips_updated_counter.inc()
else:
# logger.info("Incrementing trips created")
trips_created_counter.inc()
except ImportError:
pass

return carpool

async def set_lastUpdated_if_unset(carpool):
Expand Down Expand Up @@ -176,4 +163,6 @@ async def delete_agency_carpools_older_than(agency_id, timestamp):
if os.path.getmtime(carpool_file_name) < timestamp:
m = re.search(r'([a-zA-Z0-9_-]+)\.json$', carpool_file_name)
# TODO log deletion
cp = await load_carpool(agency_id, m[1])
run_on_delete(cp)
await _delete_carpool(agency_id, m[1])
5 changes: 0 additions & 5 deletions amarillo/services/carpools.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ def purge_outdated_offers(self):
if cp and self.is_outdated(cp):
logger.info("Purge outdated offer %s", key)
self.delete(cp.agency, cp.id)
try:
from amarillo.plugins.metrics import trips_deleted_counter
trips_deleted_counter.inc()
except ImportError:
pass

def get(self, agency_id: str, carpool_id: str):
return self.carpools.get(f"{agency_id}:{carpool_id}")
Expand Down
27 changes: 27 additions & 0 deletions amarillo/services/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import List
from amarillo.models.Carpool import Carpool

class CarpoolEvents:
def on_create(cp : Carpool):
pass
def on_update(cp : Carpool):
pass
def on_delete(cp : Carpool):
pass

carpool_event_listeners : List[CarpoolEvents] = []

def register_carpool_event_listener(cpe : CarpoolEvents):
carpool_event_listeners.append(cpe)

def run_on_create(cp: Carpool):
for cpe in carpool_event_listeners:
cpe.on_create(cp)

def run_on_update(cp: Carpool):
for cpe in carpool_event_listeners:
cpe.on_update(cp)

def run_on_delete(cp: Carpool):
for cpe in carpool_event_listeners:
cpe.on_delete(cp)

0 comments on commit 6b42501

Please sign in to comment.