diff --git a/.dockerignore b/.dockerignore index 00caa8b8..7c19f181 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,6 +1,9 @@ .git +.github .pytest_cache +**/__pycache__ .tox +.env .venv Dockerfile .dockerignore diff --git a/pyreisejl/utility/app.py b/pyreisejl/utility/app.py index a962149d..6936782a 100644 --- a/pyreisejl/utility/app.py +++ b/pyreisejl/utility/app.py @@ -1,3 +1,5 @@ +import os +import sys from pathlib import Path from subprocess import PIPE, Popen @@ -26,27 +28,44 @@ def get_script_path(): return str(path_to_script) -@app.route("/launch/", methods=["POST"]) -def launch_simulation(scenario_id): - cmd_call = ["python3", "-u", get_script_path(), str(scenario_id), "--extract-data"] - threads = request.args.get("threads", None) - solver = request.args.get("solver", None) +def launch_simulation(scenario_id, threads=None, solver=None): + cmd = [ + sys.executable, + "-u", + get_script_path(), + str(scenario_id), + "--extract-data", + ] if threads is not None: - cmd_call.extend(["--threads", str(threads)]) + cmd.extend(["--threads", str(threads)]) if solver is not None: - cmd_call.extend(["--solver", solver]) + cmd.extend(["--solver", solver]) - proc = Popen(cmd_call, stdout=PIPE, stderr=PIPE, start_new_session=True) + new_env = os.environ.copy() + new_env["PYTHONPATH"] = str(Path(__file__).parent.parent.parent.absolute()) + proc = Popen(cmd, stdout=PIPE, stderr=PIPE, start_new_session=True, env=new_env) entry = SimulationState(scenario_id, proc) state.add(entry) - return jsonify(entry.as_dict()) + return entry.as_dict() + + +def check_progress(): + return state.as_dict() + + +@app.route("/launch/", methods=["POST"]) +def handle_launch(scenario_id): + threads = request.args.get("threads", None) + solver = request.args.get("solver", None) + entry = launch_simulation(scenario_id, threads, solver) + return jsonify(entry) @app.route("/list") def list_ongoing(): - return jsonify(state.as_dict()) + return jsonify(check_progress()) @app.route("/status/") diff --git a/pyreisejl/utility/const.py b/pyreisejl/utility/const.py index 32bbb197..7e011670 100644 --- a/pyreisejl/utility/const.py +++ b/pyreisejl/utility/const.py @@ -1,9 +1,13 @@ -import posixpath +import os +from pathlib import Path -DATA_ROOT_DIR = "/mnt/bes/pcm" +if os.getenv("DEPLOYMENT_MODE") is not None: + DATA_ROOT_DIR = os.path.join(Path.home(), "ScenarioData", "") +else: + DATA_ROOT_DIR = "/mnt/bes/pcm" -SCENARIO_LIST = posixpath.join(DATA_ROOT_DIR, "ScenarioList.csv") -EXECUTE_LIST = posixpath.join(DATA_ROOT_DIR, "ExecuteList.csv") -EXECUTE_DIR = posixpath.join(DATA_ROOT_DIR, "tmp") -INPUT_DIR = posixpath.join(DATA_ROOT_DIR, "data", "input") -OUTPUT_DIR = posixpath.join(DATA_ROOT_DIR, "data", "output") +SCENARIO_LIST = os.path.join(DATA_ROOT_DIR, "ScenarioList.csv") +EXECUTE_LIST = os.path.join(DATA_ROOT_DIR, "ExecuteList.csv") +EXECUTE_DIR = os.path.join(DATA_ROOT_DIR, "tmp") +INPUT_DIR = os.path.join(DATA_ROOT_DIR, "data", "input") +OUTPUT_DIR = os.path.join(DATA_ROOT_DIR, "data", "output") diff --git a/pyreisejl/utility/helpers.py b/pyreisejl/utility/helpers.py index 7fe7b356..59774ba0 100644 --- a/pyreisejl/utility/helpers.py +++ b/pyreisejl/utility/helpers.py @@ -176,10 +176,8 @@ def get_scenario(scenario_id): scenario_info = scenario.to_dict("records", into=OrderedDict)[0] # Determine input and execute directory for data - input_dir = os.path.join(const.EXECUTE_DIR, "scenario_%s" % scenario_info["id"]) - execute_dir = os.path.join( - const.EXECUTE_DIR, f"scenario_{str(scenario_id)}", "output" - ) + input_dir = os.path.join(const.EXECUTE_DIR, f"scenario_{scenario_id}") + execute_dir = os.path.join(input_dir, "output") # Grab start and end date for scenario start_date = scenario_info["start_date"]