Skip to content

Commit bdf52ee

Browse files
Add Cleanup Stage For Simulation Requests
1 parent 4214b0e commit bdf52ee

1 file changed

Lines changed: 27 additions & 21 deletions

File tree

compose_api/simulation/handlers.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os.path
44
import random
5+
import shutil
56
import string
67
import tempfile
78
import zipfile
@@ -105,7 +106,12 @@ async def perform_job() -> None:
105106
experiment_id=experiment_id,
106107
)
107108

109+
def remove_temp_dir() -> None:
110+
shutil.rmtree(tmp_dir)
111+
112+
# Tasks are executed in order, https://www.starlette.dev/background/
108113
background_tasks.add_task(perform_job)
114+
background_tasks.add_task(remove_temp_dir)
109115

110116
return SimulationExperiment(
111117
experiment_id=experiment_id,
@@ -122,7 +128,7 @@ async def run_pbif(
122128
use_interesting: bool = True,
123129
) -> SimulationExperiment:
124130
# Create OMEX with all necessary files
125-
with tempfile.TemporaryDirectory(delete=False) as tmp_dir:
131+
with tempfile.TemporaryDirectory() as tmp_dir:
126132
with zipfile.ZipFile(tmp_dir + "/input.omex", "w") as omex:
127133
omex.writestr(data=templated_pbif, zinfo_or_arcname=f"{simulator_name}.pbif")
128134
if use_interesting:
@@ -133,26 +139,26 @@ async def run_pbif(
133139
raise HTTPException(500, "Can't create omex file.")
134140
simulator_request = SimulationRequest(omex_archive=Path(omex.filename))
135141

136-
try:
137-
sim_service = get_required_simulation_service()
138-
db_service = get_required_database_service()
139-
job_monitor = get_required_job_monitor()
140-
except ValueError as e:
141-
logger.exception(msg=f"Failed to initialize {simulator_name} run.", exc_info=e)
142-
raise HTTPException(status_code=500, detail=str(e))
143-
144-
try:
145-
return await run_simulation(
146-
simulation_request=simulator_request,
147-
database_service=db_service,
148-
simulation_service_slurm=sim_service,
149-
job_monitor=job_monitor,
150-
pb_allow_list=PBAllowList(allow_list=allow_list),
151-
background_tasks=background_tasks,
152-
)
153-
except Exception as e:
154-
logger.exception(msg=f"Failed to start {simulator_name} run", exc_info=e)
155-
raise HTTPException(500)
142+
try:
143+
sim_service = get_required_simulation_service()
144+
db_service = get_required_database_service()
145+
job_monitor = get_required_job_monitor()
146+
except ValueError as e:
147+
logger.exception(msg=f"Failed to initialize {simulator_name} run.", exc_info=e)
148+
raise HTTPException(status_code=500, detail=str(e))
149+
150+
try:
151+
return await run_simulation(
152+
simulation_request=simulator_request,
153+
database_service=db_service,
154+
simulation_service_slurm=sim_service,
155+
job_monitor=job_monitor,
156+
pb_allow_list=PBAllowList(allow_list=allow_list),
157+
background_tasks=background_tasks,
158+
)
159+
except Exception as e:
160+
logger.exception(msg=f"Failed to start {simulator_name} run", exc_info=e)
161+
raise HTTPException(500)
156162

157163

158164
async def _dispatch_job(

0 commit comments

Comments
 (0)