fix(realtime): tolerate missing scoreboard channel layer
All checks were successful
CI / test-and-quality (pull_request) Successful in 2m58s
CI / test-and-quality (push) Successful in 2m59s

This commit is contained in:
2026-03-15 09:08:13 +00:00
parent 8fa39adc2b
commit be38fe6ac2
2 changed files with 50 additions and 11 deletions

View File

@@ -1,20 +1,35 @@
from asgiref.sync import async_to_sync
from channels.exceptions import InvalidChannelLayerError
from channels.layers import get_channel_layer
from redis.exceptions import ConnectionError as RedisConnectionError
async def broadcast_phase_event(session_code: str, event_type: str, payload: dict) -> None:
"""Send a phase event to all WebSocket clients connected to a game session."""
channel_layer = get_channel_layer()
try:
channel_layer = get_channel_layer()
except InvalidChannelLayerError:
return
if channel_layer is None:
return
group_name = f"game_{session_code.upper()}"
await channel_layer.group_send(
group_name,
{
"type": "phase_event",
"payload": {"type": event_type, **payload},
},
)
try:
await channel_layer.group_send(
group_name,
{
"type": "phase_event",
"payload": {"type": event_type, **payload},
},
)
except (InvalidChannelLayerError, RedisConnectionError):
return
def sync_broadcast_phase_event(session_code: str, event_type: str, payload: dict) -> None:
"""Sync wrapper for calling broadcast_phase_event from synchronous Django views."""
async_to_sync(broadcast_phase_event)(session_code, event_type, payload)
try:
async_to_sync(broadcast_phase_event)(session_code, event_type, payload)
except (InvalidChannelLayerError, RedisConnectionError):
return