truenas-burnin/app/notifier.py
Brandon Walter b73b5251ae Initial commit — TrueNAS Burn-In Dashboard v0.5.0
Full-stack burn-in orchestration dashboard (Stages 1–6d complete):
FastAPI backend, SQLite/WAL, SSE live dashboard, mock TrueNAS server,
SMTP/webhook notifications, batch burn-in, settings UI, audit log,
stats page, cancel SMART/burn-in, drag-to-reorder stages.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-02-24 00:08:29 -05:00

80 lines
2.2 KiB
Python

"""
Notification dispatcher — webhooks and immediate email alerts.
Called from burnin.py when a job reaches a terminal state (passed/failed).
Webhook fires unconditionally when WEBHOOK_URL is set.
Email alerts fire based on smtp_alert_on_fail / smtp_alert_on_pass settings.
"""
import asyncio
import logging
from app.config import settings
log = logging.getLogger(__name__)
async def notify_job_complete(
job_id: int,
devname: str,
serial: str | None,
model: str | None,
state: str,
profile: str,
operator: str,
error_text: str | None,
) -> None:
"""Fire all configured notifications for a completed burn-in job."""
tasks = []
if settings.webhook_url:
tasks.append(_send_webhook({
"event": f"burnin_{state}",
"job_id": job_id,
"devname": devname,
"serial": serial,
"model": model,
"state": state,
"profile": profile,
"operator": operator,
"error_text": error_text,
}))
if settings.smtp_host:
should_alert = (
(state == "failed" and settings.smtp_alert_on_fail) or
(state == "passed" and settings.smtp_alert_on_pass)
)
if should_alert:
tasks.append(_send_alert_email(job_id, devname, serial, model, state, error_text))
if not tasks:
return
results = await asyncio.gather(*tasks, return_exceptions=True)
for r in results:
if isinstance(r, Exception):
log.error("Notification failed: %s", r, extra={"job_id": job_id, "devname": devname})
async def _send_webhook(payload: dict) -> None:
import httpx
async with httpx.AsyncClient(timeout=10.0) as client:
r = await client.post(settings.webhook_url, json=payload)
r.raise_for_status()
log.info(
"Webhook sent",
extra={"event": payload.get("event"), "job_id": payload.get("job_id"), "url": settings.webhook_url},
)
async def _send_alert_email(
job_id: int,
devname: str,
serial: str | None,
model: str | None,
state: str,
error_text: str | None,
) -> None:
from app import mailer
await mailer.send_job_alert(job_id, devname, serial, model, state, error_text)