diff --git a/vivarium/utils/handle_server_interface.py b/vivarium/utils/handle_server_interface.py index ad38e2a..530f46a 100644 --- a/vivarium/utils/handle_server_interface.py +++ b/vivarium/utils/handle_server_interface.py @@ -20,9 +20,10 @@ def get_process_pid(process_name: str): out, err = process.communicate() for line in out.splitlines(): if process_name.encode('utf-8') in line: - pid = line.split()[1] - lg.info(f"{process_name} PID: {pid.decode()}") - return pid.decode() + pid_str = line.split()[1] + pid = pid_str.decode() + lg.warning(f" Found the process {process_name} running with this PID: {pid}") + return pid def kill_process(pid): """Kill a process by its ID @@ -30,6 +31,7 @@ def kill_process(pid): :param pid: process ID """ os.kill(int(pid), signal.SIGTERM) + lg.warning(f"Killed process with PID: {pid}") # Define parameters of the simulator def start_server_and_interface(scene_name: str, notebook_mode: bool = True, wait_time: int = 6): @@ -39,7 +41,12 @@ def start_server_and_interface(scene_name: str, notebook_mode: bool = True, wait :param notebook_mode: notebook_mode to adapt the interface, defaults to True """ # first ensure no interface or server is running - stop_server_and_interface() + processes_running = stop_server_and_interface(auto_kill=False) + if processes_running: + lg.warning("\nServer and Interface processes are still running, please stop them before starting new ones") + lg.warning("ERROR: New processes will not be started") + return + # find the path to the server and interface scripts print(f"{os.path.dirname(__file__)}") project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) @@ -55,7 +62,7 @@ def start_server_and_interface(scene_name: str, notebook_mode: bool = True, wait def start_server_process(): subprocess.run(server_command) - print("STARTING SERVER") + print("\nSTARTING SERVER") server_process = multiprocessing.Process(target=start_server_process) server_process.start() time.sleep(wait_time) @@ -76,11 +83,43 @@ def start_interface_process(): interface_process = multiprocessing.Process(target=start_interface_process) interface_process.start() -def stop_server_and_interface(): +def terminate_process(pid): + """Terminate the process if the PID is not None""" + if pid is not None: + kill_process(pid) + +def stop_server_and_interface(auto_kill=False): """Stop the server and interface """ - stopped = False - # interface_process_name = "vivarium/scripts/run_interface.py" + processes_running = False + interface_pid = get_process_pid(INTERFACE_PROCESS_NAME) + server_pid = get_process_pid(SERVER_PROCESS_NAME) + + if interface_pid is not None or server_pid is not None: + processes_running = True + if auto_kill: + terminate_process(interface_pid) + terminate_process(server_pid) + processes_running = False + else: + message = "\nThe following processes are running:\n" + if interface_pid is not None: + message += f" - Interface (PID: {interface_pid})\n" + if server_pid is not None: + message += f" - Server (PID: {server_pid})\n" + message += "Do you want to stop them? (y/n): " + user_input = input(message) + + if user_input.lower() == "y": + terminate_process(interface_pid) + terminate_process(server_pid) + processes_running = False + + return processes_running + +# TODO : older version of the function, see if we remove it +def stop_server_and_interface_unsafe(): + """Stop the server and interface processes if they are running""" interface_pid = get_process_pid(INTERFACE_PROCESS_NAME) if interface_pid is not None: kill_process(interface_pid) @@ -88,7 +127,6 @@ def stop_server_and_interface(): else: lg.info("Interface process not found") - # server_process_name = "vivarium/scripts/run_server.py" server_pid = get_process_pid(SERVER_PROCESS_NAME) if server_pid is not None: kill_process(server_pid) @@ -97,4 +135,4 @@ def stop_server_and_interface(): lg.info("Server process not found") if stopped: - print("Server and Interface Stopped") \ No newline at end of file + print("\nServer and Interface Stopped") \ No newline at end of file