Skip to content

Commit

Permalink
Improve stopping server and interface mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
corentinlger committed Nov 27, 2024
1 parent de4e1bd commit 9a32760
Showing 1 changed file with 48 additions and 10 deletions.
58 changes: 48 additions & 10 deletions vivarium/utils/handle_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ 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
: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):
Expand All @@ -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__), "../../"))
Expand All @@ -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)
Expand All @@ -76,19 +83,50 @@ 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)
stopped = True
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)
Expand All @@ -97,4 +135,4 @@ def stop_server_and_interface():
lg.info("Server process not found")

if stopped:
print("Server and Interface Stopped")
print("\nServer and Interface Stopped")

0 comments on commit 9a32760

Please sign in to comment.