Coverage for notifications/tasks.py: 100.00%
14 statements
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 20:17 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2026-11-30 20:17 +0000
1"""BackgroundTasks-friendly entry point for the dispatcher.
3FastAPI's BackgroundTasks runs these after the response is sent, so any
4exception is captured silently — we swallow and log explicitly to avoid
5surprising failures during high-traffic live matches.
6"""
8from __future__ import annotations
10import structlog
12from notifications.dispatcher import get_notifier
13from notifications.ops_events import emit_match_final_by_id
15logger = structlog.get_logger(__name__)
18def notify_event_task(
19 event_type: str,
20 match_id: int,
21 extra: dict | None = None,
22 event_key: str | None = None,
23) -> None:
24 """Fire a notification without ever raising."""
25 try:
26 get_notifier().notify(event_type=event_type, match_id=match_id, extra=extra, event_key=event_key)
27 except Exception as exc:
28 logger.warning(
29 "notifications.task_failed",
30 event_type=event_type,
31 match_id=match_id,
32 error_type=type(exc).__name__,
33 error=str(exc),
34 )
36 # Operator channel (SB-1849). Every new final score reaches the app as a
37 # "fulltime" task: the admin/skill match write, the scraper's
38 # final-score-alert call and live scoring's end_match. Hooking here, after
39 # the fan-out and independent of it, means none of the three is missed.
40 if event_type == "fulltime":
41 emit_match_final_by_id(match_id, _load_match)
44def _load_match(match_id: int) -> dict | None:
45 return get_notifier().match_dao.get_match_by_id(match_id, include_test=True)