Source code for s365b.core.events
"""Recording notification events.
Anything admins should hear about is first written to the ``event`` table.
Delivery (email via Graph or SMTP) happens later in a separate job, so a mail
problem never fails a backup and every event is visible in the UI.
"""
from __future__ import annotations
from typing import Any
from sqlalchemy.ext.asyncio import AsyncSession
from s365b.db.models import Event
from s365b.modules.sdk import Severity
# Core event types. Modules add their own as "<module>.<event>".
[docs]
JOB_FAILED = "job_failed"
[docs]
JOB_WARNING = "job_warning"
[docs]
JOB_STOPPED = "job_stopped"
[docs]
JOB_SUCCEEDED = "job_succeeded"
[docs]
async def emit(
session: AsyncSession,
type_: str,
severity: Severity,
subject: str,
*,
body: dict[str, Any] | None = None,
dedup_key: str | None = None,
run_fk: int | None = None,
) -> Event:
"""Record an event for delivery.
Args:
session: Open session; the caller commits.
type_: Event type, e.g. ``job_failed``.
severity: Severity.
subject: One-line summary, used as the email subject.
body: Structured details for the email template and UI.
dedup_key: Events with the same key are throttled together
(e.g. ``job_failed:3``).
run_fk: Related job run.
Returns:
The new event row.
"""
event = Event(
type=type_,
severity=severity.value,
subject=subject,
body=body or {},
dedup_key=dedup_key,
run_fk=run_fk,
)
session.add(event)
await session.flush()
return event