Skip to content

Commit

Permalink
Refactor launch_servers: move debug mode to ON_GAME_SERVER_RESTART=False
Browse files Browse the repository at this point in the history
  • Loading branch information
emnigma committed Aug 14, 2023
1 parent ca8ac5b commit 07231eb
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions VSharp.ML.AIAgent/launch_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ async def dequeue_instance(request):
server_info = SERVER_INSTANCES.get(block=False)
assert server_info.pid is Undefined
server_info = run_server_instance(
port=server_info.port, start_server=START_SERVERS
port=server_info.port,
start_server=FeatureConfig.ON_GAME_SERVER_RESTART.enabled,
)
logging.info(f"issued {server_info}: {psutil.Process(server_info.pid)}")
return web.json_response(server_info.to_json())
Expand Down Expand Up @@ -117,12 +118,10 @@ def run_server_instance(port: int, start_server: bool) -> ServerInstanceInfo:
return ServerInstanceInfo(port, ws_url, server_pid)


def run_servers(
num_inst: int, start_port: int, start_servers: bool
) -> list[ServerInstanceInfo]:
def run_servers(num_inst: int, start_port: int) -> list[ServerInstanceInfo]:
servers_info = []
for i in range(num_inst):
server_info = run_server_instance(start_port + i, start_server=start_servers)
server_info = run_server_instance(start_port + i, False)
servers_info.append(server_info)

return servers_info
Expand All @@ -132,23 +131,20 @@ def kill_server(server_instance: ServerInstanceInfo):
os.kill(server_instance.pid, signal.SIGKILL)
PROCS.remove(server_instance.pid)

proc_info = psutil.Process(server_instance.pid)
wait_for_reset_retries = FeatureConfig.ON_GAME_SERVER_RESTART.wait_for_reset_retries

while wait_for_reset_retries:
logging.info(
f"Waiting for {server_instance} to die, {wait_for_reset_retries} retries left"
)
if psutil.Process(server_instance.pid).status() in (
psutil.STATUS_DEAD,
psutil.STATUS_ZOMBIE,
):
if proc_info.status() in (psutil.STATUS_DEAD, psutil.STATUS_ZOMBIE):
logging.info(f"killed {proc_info}")
break
time.sleep(FeatureConfig.ON_GAME_SERVER_RESTART.wait_for_reset_time)
wait_for_reset_retries -= 1

if wait_for_reset_retries == 0:
raise RuntimeError(f"{server_instance} could not be killed")

logging.info(f"killed {psutil.Process(server_instance.pid)}")
raise RuntimeError(f"{server_instance} could not be killed")


def kill_process(pid: int):
Expand All @@ -162,7 +158,6 @@ def server_manager(server_queue: Queue[ServerInstanceInfo]):
servers_info = run_servers(
num_inst=GeneralConfig.SERVER_COUNT,
start_port=ServerConfig.VSHARP_INSTANCES_START_PORT,
start_servers=False,
)

for server_info in servers_info:
Expand All @@ -176,15 +171,13 @@ def server_manager(server_queue: Queue[ServerInstanceInfo]):


def main():
global SERVER_INSTANCES, PROCS, RESULTS, START_SERVERS
global SERVER_INSTANCES, PROCS, RESULTS
parser = argparse.ArgumentParser(description="V# instances launcher")
parser.add_argument(
"--debug",
action=argparse.BooleanOptionalAction,
help="dont launch servers if set",
)
args = parser.parse_args()
START_SERVERS = not args.debug

# Queue[ServerInstanceInfo]
SERVER_INSTANCES = Queue()
Expand Down

0 comments on commit 07231eb

Please sign in to comment.