Files
julian 5f33eedbb1 feat(backend): container readiness — BACKEND_EVENTS override, GET /api/health, smoke test in-repo
- journal.py: events.jsonl path overridable via BACKEND_EVENTS (mirrors
  BACKEND_DB) so containers journal onto the data volume
- routes.py: unauthenticated GET /api/health for Docker/Traefik/Komodo probes
- backend/smoke_test.py: the 65-check suite promoted from the scratchpad;
  temp-dir DB + journal, runs anywhere via python -m backend.smoke_test
2026-07-12 10:32:37 +02:00

42 lines
1.4 KiB
Python

"""Durable journal: events.jsonl — everything the charger tells us (and what
we told it), one JSON line per event. Append-only, crash-safe, greppable; the
raw ground truth behind the queryable store (store.py).
"""
import json
import logging
import os
from datetime import datetime, timezone
from pathlib import Path
from typing import Optional
HERE = Path(__file__).resolve().parent
# BACKEND_EVENTS: in containers the journal must land on the data volume, not
# in the ephemeral image layer. Mirrors the BACKEND_DB override in store.py.
EVENTS_PATH = Path(os.environ.get("BACKEND_EVENTS", HERE / "events.jsonl"))
log = logging.getLogger("journal")
def now() -> str:
return datetime.now(timezone.utc).isoformat()
def record_event(event: str, cp_id: str, **data) -> None:
entry = {"time": now(), "event": event, "cp": cp_id, **data}
try:
with EVENTS_PATH.open("a", encoding="utf-8") as f:
f.write(json.dumps(entry, ensure_ascii=False, default=str) + "\n")
except Exception:
log.exception("failed to journal event %r", event)
def read_events(limit: int = 100, event: Optional[str] = None) -> list:
if not EVENTS_PATH.exists():
return []
lines = EVENTS_PATH.read_text(encoding="utf-8").splitlines()
entries = [json.loads(line) for line in lines if line.strip()]
if event is not None:
entries = [e for e in entries if e.get("event") == event]
return entries[-limit:]