Skip to content

Commit 3d8dd92

Browse files
committed
remove tmp dirs after submit handling
1 parent f9372a1 commit 3d8dd92

5 files changed

Lines changed: 28 additions & 21 deletions

File tree

backend/config/paths.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import os
22

33

4-
def get_process_filepath(uid, filepath):
4+
def get_tmp_thread_files(uid):
5+
return [get_thread_filepath(uid, UPLOAD_DIR), get_thread_filepath(uid, RESULTS_DIR)]
6+
7+
8+
def get_thread_filepath(uid, filepath):
59
base_prefix = TMP_FILE_DIR + "/"
610
new_prefix = f"{TMP_FILE_DIR}/{uid}_"
711
return filepath.replace(base_prefix, new_prefix)

backend/file_utils/files.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def save_upload_file(file: UploadFile, dst: str) -> None:
1010
f.write(chunk)
1111

1212

13-
def reset_dir(path: str) -> None:
14-
if os.path.exists(path):
15-
shutil.rmtree(path)
16-
os.makedirs(path)
13+
def reset_dirs(paths: list) -> None:
14+
for path in paths:
15+
if os.path.exists(path):
16+
shutil.rmtree(path)

backend/main.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from fastapi.middleware.cors import CORSMiddleware
33
import shortuuid
44

5-
from backend.file_utils.files import reset_dir
5+
from backend.file_utils.files import reset_dirs
66
from backend.utils.docker_runner import run_pipeline
77
from backend.utils.data_uploader import handle_upload
88
from backend.utils.methods_handler import Methods
@@ -11,7 +11,8 @@
1111
RESULTS_DIR,
1212
DATASET_FILE,
1313
METHODS_TS_FILE,
14-
get_process_filepath, BASE_DIR,
14+
get_thread_filepath,
15+
get_tmp_thread_files
1516
)
1617

1718
dataset_methods = Methods(data_filepath=DATASET_FILE, output_filepath=METHODS_TS_FILE)
@@ -41,12 +42,14 @@ async def handle_submit(
4142

4243
send_folder_by_email(
4344
email,
44-
get_process_filepath(task_uid, RESULTS_DIR),
45+
get_thread_filepath(task_uid, RESULTS_DIR),
4546
experiment,
4647
file.filename,
4748
)
4849

49-
reset_dir(BASE_DIR)
50+
tmp_thread_dirs = get_tmp_thread_files(task_uid)
51+
reset_dirs(tmp_thread_dirs)
52+
5053
return {
5154
"experiment": experiment,
5255
"filename": file.filename,

backend/utils/data_uploader.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from backend.file_utils.csv_methods_writer import write_launch_info_csv
44
from backend.file_utils.files import save_upload_file
55
from backend.utils.methods_handler import Methods
6-
from backend.config.paths import LAUNCH_INFO_FILE, MODEL_ONNX_FILE, get_process_filepath
6+
from backend.config.paths import LAUNCH_INFO_FILE, MODEL_ONNX_FILE, get_thread_filepath
77

88

99
def handle_upload(
@@ -14,10 +14,10 @@ def handle_upload(
1414
) -> None:
1515
selection_methods = json.loads(methods)
1616

17-
save_upload_file(file, get_process_filepath(uid, MODEL_ONNX_FILE))
17+
save_upload_file(file, get_thread_filepath(uid, MODEL_ONNX_FILE))
1818

1919
write_launch_info_csv(
2020
methods=selection_methods,
2121
dataset_methods=dataset_methods,
22-
output_file=get_process_filepath(uid, LAUNCH_INFO_FILE),
22+
output_file=get_thread_filepath(uid, LAUNCH_INFO_FILE),
2323
)

backend/utils/docker_runner.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from abc import ABC, abstractmethod
33

44
from backend.config.paths import (
5-
get_process_filepath,
5+
get_thread_filepath,
66
RESULTS_DIR,
77
LAUNCH_INFO_FILE,
88
MODEL_ONNX_FILE,
@@ -40,9 +40,9 @@ class RunstratBaseline(DockerRunner):
4040
def _volumes(self, uid) -> list[str]:
4141
return [
4242
"-v",
43-
f"{get_process_filepath(uid, RESULTS_DIR)}:/workspace/PySymGym/tools/runstrat/results",
43+
f"{get_thread_filepath(uid, RESULTS_DIR)}:/workspace/PySymGym/tools/runstrat/results",
4444
"-v",
45-
f"{get_process_filepath(uid, LAUNCH_INFO_FILE)}"
45+
f"{get_thread_filepath(uid, LAUNCH_INFO_FILE)}"
4646
f":/workspace/PySymGym/tools/runstrat/resources/launch_info.csv",
4747
]
4848

@@ -67,12 +67,12 @@ class RunstratAI(DockerRunner):
6767
def _volumes(self, uid) -> list[str]:
6868
return [
6969
"-v",
70-
f"{get_process_filepath(uid, RESULTS_DIR)}:/workspace/PySymGym/tools/runstrat/results",
70+
f"{get_thread_filepath(uid, RESULTS_DIR)}:/workspace/PySymGym/tools/runstrat/results",
7171
"-v",
72-
f"{get_process_filepath(uid, LAUNCH_INFO_FILE)}"
72+
f"{get_thread_filepath(uid, LAUNCH_INFO_FILE)}"
7373
f":/workspace/PySymGym/tools/runstrat/resources/launch_info.csv",
7474
"-v",
75-
f"{get_process_filepath(uid, MODEL_ONNX_FILE)}"
75+
f"{get_thread_filepath(uid, MODEL_ONNX_FILE)}"
7676
f":/workspace/PySymGym/tools/runstrat/resources/model.onnx",
7777
]
7878

@@ -99,13 +99,13 @@ class Compstrat(DockerRunner):
9999
def _volumes(self, uid) -> list[str]:
100100
return [
101101
"-v",
102-
f"{get_process_filepath(uid, ARTIFACTS_AI_CSV_FILE)}"
102+
f"{get_thread_filepath(uid, ARTIFACTS_AI_CSV_FILE)}"
103103
":/workspace/PySymGym/tools/compstrat/strat/ai_strat",
104104
"-v",
105-
f"{get_process_filepath(uid, ARTIFACTS_BASELINE_CSV_FILE)}"
105+
f"{get_thread_filepath(uid, ARTIFACTS_BASELINE_CSV_FILE)}"
106106
":/workspace/PySymGym/tools/compstrat/strat/baseline_strat",
107107
"-v",
108-
f"{get_process_filepath(uid, COMPSTRAT_RESULTS_DIR)}"
108+
f"{get_thread_filepath(uid, COMPSTRAT_RESULTS_DIR)}"
109109
":/workspace/PySymGym/tools/compstrat/results",
110110
]
111111

0 commit comments

Comments
 (0)