Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 33 additions & 21 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,17 @@ def _node_handler_lazy(args) -> None:
import os

if os.environ.get("HERMES_NODES_AUTO_START", "1") == "1":
print("hermes-node-plugin: auto-start check running", flush=True)
# Load config to get the actual port for the socket check.
from .config import load_config

cfg = load_config()
_check_host = cfg.connect_host
_check_port = cfg.port
print(
f"hermes-node-plugin: config loaded host={_check_host} port={_check_port}",
flush=True,
)

# Quick socket check: if the configured port is already bound,
# the server is already running (e.g. from a previous gateway
Expand All @@ -170,37 +175,45 @@ def _node_handler_lazy(args) -> None:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((_check_host, _check_port))
s.close()
except OSError:
print(
f"hermes-node-plugin: port {_check_port} is free, will start server",
flush=True,
)
except OSError as e:
_port_free = False
print(
f"hermes-node-plugin: port {_check_port} is in use ({e}), skipping",
flush=True,
)

if _port_free:

def _start_server() -> None:
import asyncio

log.info(
"hermes-node-plugin: starting WSS server on port %d"
" (background thread)",
_check_port,
print(
f"hermes-node-plugin: _start_server thread running on port {_check_port}",
flush=True,
)
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
from .lifecycle import _on_session_start

loop.run_until_complete(_on_session_start())
log.info(
"hermes-node-plugin: WSS server started on port %d"
" (background thread)",
_check_port,
print(
f"hermes-node-plugin: WSS server started on port {_check_port}",
flush=True,
)
# Keep the loop alive so uvicorn tasks continue running.
loop.run_forever()
except Exception as exc:
log.warning(
"hermes-node-plugin: server background thread failed: %s",
exc,
print(
f"hermes-node-plugin: server background thread failed: {exc}",
flush=True,
)
import traceback
traceback.print_exc()
finally:
loop.close()

Expand All @@ -211,17 +224,16 @@ def _start_server() -> None:
target=_start_server, daemon=True, name="hermes-node-wss"
)
t.start()
print("hermes-node-plugin: daemon thread started", flush=True)
except Exception as exc:
log.warning(
"hermes-node-plugin: could not start server thread: %s", exc
print(
f"hermes-node-plugin: could not start server thread: {exc}",
flush=True,
)
else:
log.debug(
"hermes-node-plugin: port %d already bound — "
"server likely already running. Skipping auto-start.",
_check_port,
print(
f"hermes-node-plugin: port {_check_port} already bound — skipping",
flush=True,
)
else:
log.debug(
"hermes-node-plugin: auto-start disabled (HERMES_NODES_AUTO_START=0)"
)
print("hermes-node-plugin: auto-start disabled (HERMES_NODES_AUTO_START=0)", flush=True)
Loading