feat(websocket): add debug flag for ping operations

Introduce `ENABLE_WEBSOCKET_PING_DEBUG` environment variable to
conditionally enable verbose logging for websocket ping sends and
pong receptions, reducing log noise in production.
This commit is contained in:
2025-10-27 07:21:16 +10:30
parent 05f9862f16
commit 3bca326b51
3 changed files with 9 additions and 6 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ PING_EXPIRY_SECONDS = 5 # How long between PING and PONG before we
ASSIGN_INTERVAL_SECONDS = 30
LIVENESS_CHECK_INTERVAL_SECONDS = 5 # How often to cross check connected_workers with ping results from Redis
ENABLE_WEBSOCKET_PING_DEBUG = os.getenv('ENABLE_WEBSOCKET_PING_DEBUG', 'false').lower() == 'true'
# Database configuration
DATABASE_URL = "mysql://root:password@172.17.0.1:3306/theapi"
+3 -2
View File
@@ -8,7 +8,7 @@ import logging
from websocket_server.config import get_redis_client
from api_client.client import IaaSClient
from websocket_server.shared_state import connected_workers, connected_sids_lock, connected_sids, worker_lock, ping_tracker
from websocket_server.config import REDIS_PING_EXPIRY_SECONDS
from websocket_server.config import REDIS_PING_EXPIRY_SECONDS, ENABLE_WEBSOCKET_PING_DEBUG
logger = logging.getLogger("websocket_server")
@@ -138,7 +138,8 @@ def register_socketio_handlers(socketio):
return
latency_ms = int((time.time() - ping_info["sent_time"]) * 1000)
logger.info(f"[{worker_id}] Pong received. Latency: {latency_ms}ms")
if ENABLE_WEBSOCKET_PING_DEBUG:
logger.debug(f"[{worker_id}] Pong received. Latency: {latency_ms}ms")
redis.set(f"ws_liveness:{worker_id}", int(time.time()), ex=REDIS_PING_EXPIRY_SECONDS)
redis.set(f"ws_latency:{worker_id}", latency_ms, ex=REDIS_PING_EXPIRY_SECONDS)
+5 -3
View File
@@ -6,7 +6,7 @@ import time
import threading
import logging
from websocket_server.shared_state import connected_workers, worker_lock, ping_tracker
from websocket_server.config import get_redis_client,PING_INTERVAL_SECONDS, ASSIGN_INTERVAL_SECONDS, LIVENESS_CHECK_INTERVAL_SECONDS, PING_EXPIRY_SECONDS
from websocket_server.config import get_redis_client,PING_INTERVAL_SECONDS, ASSIGN_INTERVAL_SECONDS, LIVENESS_CHECK_INTERVAL_SECONDS, PING_EXPIRY_SECONDS, ENABLE_WEBSOCKET_PING_DEBUG
from websocket_server.task_assigner import assign_task_to_worker
from websocket_server.events import base
@@ -91,7 +91,8 @@ def all_worker_watchdog():
# 1. Send custom ping to all workers
if now - last_ping_time >= PING_INTERVAL_SECONDS:
if worker_ids:
logger.debug("Sending liveness pings to all workers")
if ENABLE_WEBSOCKET_PING_DEBUG:
logger.debug("Sending liveness pings to all workers")
for wid in worker_ids:
ping_id = str(uuid.uuid4())
ping_tracker[wid] = {"ping_id": ping_id, "sent_time": now}
@@ -99,7 +100,8 @@ def all_worker_watchdog():
if sid:
try:
base.socketio.emit("worker_ping", {"ping_id": ping_id}, to=sid)
logger.info(f"[{wid}] Sent ping ID: {ping_id} to {wid}")
if ENABLE_WEBSOCKET_PING_DEBUG:
logger.debug(f"[{wid}] Sent ping ID: {ping_id} to {wid}")
except Exception as e:
logger.warning(f"[{wid}] Ping send failed: {e}")
last_ping_time = now