Skip to content

Commit

Permalink
View process status when try connecting
Browse files Browse the repository at this point in the history
  • Loading branch information
emnigma committed Aug 8, 2023
1 parent 16dc283 commit 2a8f21a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
20 changes: 12 additions & 8 deletions VSharp.ML.AIAgent/connection/broker_conn/socket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
import time
from contextlib import contextmanager, suppress

import psutil
import websocket

from config import GameServerConnectorConfig
from connection.broker_conn.classes import WSUrl
from connection.broker_conn.classes import ServerInstanceInfo

from .requests import acquire_instance, return_instance


def wait_for_connection(url: WSUrl):
def wait_for_connection(server_instance: ServerInstanceInfo):
ws = websocket.WebSocket()

retries_left = GameServerConnectorConfig.WAIT_FOR_SOCKET_RECONNECTION_MAX_RETRIES
Expand All @@ -19,22 +20,25 @@ def wait_for_connection(url: WSUrl):
with suppress(ConnectionRefusedError, ConnectionResetError):
ws.settimeout(GameServerConnectorConfig.CREATE_CONNECTION_TIMEOUT_SEC)
ws.connect(
url, skip_utf8_validation=GameServerConnectorConfig.SKIP_UTF_VALIDATION
server_instance.ws_url,
skip_utf8_validation=GameServerConnectorConfig.SKIP_UTF_VALIDATION,
)
if ws.connected:
return ws
# if ws.connected:
# return ws
time.sleep(GameServerConnectorConfig.CREATE_CONNECTION_TIMEOUT_SEC)
logging.info(f"Try connecting to {url}, {retries_left} attempts left")
logging.info(
f"Try connecting to {server_instance.ws_url}, {retries_left} attempts left; {psutil.Process(server_instance.pid)}"
)
retries_left -= 1
raise RuntimeError(
f"Retries exsausted wnen trying to connect to {url}: {retries_left} left"
f"Retries exsausted wnen trying to connect to {server_instance.ws_url}: {retries_left} left"
)


@contextmanager
def game_server_socket_manager():
server_instance = acquire_instance()
socket = wait_for_connection(server_instance.ws_url)
socket = wait_for_connection(server_instance)

try:
socket.settimeout(GameServerConnectorConfig.RESPONCE_TIMEOUT_SEC)
Expand Down
4 changes: 1 addition & 3 deletions VSharp.ML.AIAgent/launch_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ async def enqueue_instance(request):
if wait_for_reset_retries == 0:
raise RuntimeError(f"{returned_instance_info} could not be killed")

logging.info(
f"killed {returned_instance_info.pid}, its status: {psutil.Process(returned_instance_info.pid).status()}"
)
logging.info(f"killed {psutil.Process(returned_instance_info.pid)}")
returned_instance_info = run_server_instance(
port=returned_instance_info.port, start_server=START_SERVERS
)
Expand Down

0 comments on commit 2a8f21a

Please sign in to comment.