12
12
from amarillo .models .Carpool import Carpool
13
13
from amarillo .routers .agencyconf import verify_api_key , verify_permission_for_same_agency_or_admin
14
14
from amarillo .tests .sampledata import examples
15
-
15
+ from amarillo . services . hooks import run_on_create , run_on_delete
16
16
from amarillo .services .config import config
17
17
from amarillo .utils .utils import assert_folder_exists
18
18
@@ -51,6 +51,8 @@ async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Bod
51
51
requesting_agency_id : str = Depends (verify_api_key )) -> Carpool :
52
52
await verify_permission_for_same_agency_or_admin (carpool .agency , requesting_agency_id )
53
53
54
+ background_tasks .add_task (run_on_create , carpool )
55
+
54
56
logger .info (f"POST trip { carpool .agency } :{ carpool .id } ." )
55
57
await assert_agency_exists (carpool .agency )
56
58
@@ -90,12 +92,14 @@ async def get_carpool(agency_id: str, carpool_id: str, api_key: str = Depends(ve
90
92
"description" : "Carpool or agency not found" },
91
93
},
92
94
)
93
- async def delete_carpool (agency_id : str , carpool_id : str , requesting_agency_id : str = Depends (verify_api_key )):
95
+ async def delete_carpool (background_tasks : BackgroundTasks , agency_id : str , carpool_id : str , requesting_agency_id : str = Depends (verify_api_key )):
94
96
await verify_permission_for_same_agency_or_admin (agency_id , requesting_agency_id )
95
97
96
98
logger .info (f"Delete trip { agency_id } :{ carpool_id } ." )
97
99
await assert_agency_exists (agency_id )
98
100
await assert_carpool_exists (agency_id , carpool_id )
101
+ cp = await load_carpool (agency_id , carpool_id )
102
+ background_tasks .add_task (run_on_delete , cp )
99
103
100
104
return await _delete_carpool (agency_id , carpool_id )
101
105
@@ -111,12 +115,6 @@ async def _delete_carpool(agency_id: str, carpool_id: str):
111
115
os .remove (f"data/enhanced/{ agency_id } /{ carpool_id } .json" , )
112
116
except FileNotFoundError :
113
117
pass
114
-
115
- try :
116
- from amarillo .plugins .metrics import trips_deleted_counter
117
- trips_deleted_counter .inc ()
118
- except ImportError :
119
- pass
120
118
121
119
122
120
async def store_carpool (carpool : Carpool ) -> Carpool :
@@ -125,17 +123,6 @@ async def store_carpool(carpool: Carpool) -> Carpool:
125
123
await set_lastUpdated_if_unset (carpool )
126
124
await save_carpool (carpool )
127
125
128
- try :
129
- from amarillo .plugins .metrics import trips_created_counter , trips_updated_counter
130
- if (carpool_exists ):
131
- # logger.info("Incrementing trips updated")
132
- trips_updated_counter .inc ()
133
- else :
134
- # logger.info("Incrementing trips created")
135
- trips_created_counter .inc ()
136
- except ImportError :
137
- pass
138
-
139
126
return carpool
140
127
141
128
async def set_lastUpdated_if_unset (carpool ):
@@ -176,4 +163,6 @@ async def delete_agency_carpools_older_than(agency_id, timestamp):
176
163
if os .path .getmtime (carpool_file_name ) < timestamp :
177
164
m = re .search (r'([a-zA-Z0-9_-]+)\.json$' , carpool_file_name )
178
165
# TODO log deletion
166
+ cp = await load_carpool (agency_id , m [1 ])
167
+ run_on_delete (cp )
179
168
await _delete_carpool (agency_id , m [1 ])
0 commit comments