| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import asyncio
- import logging
- import os
- import random
- from dataclasses import dataclass
- from datetime import datetime, timezone
- from room_cal.store import Calendar, DEFAULT_CALENDAR_NAME
- from room_cal.service_api import ServiceAPI
- from room_cal.service_api.types import ServiceAuthError
- from room_cal._utils import services
- _log = logging.getLogger(__name__)
- _POLL_MIN = 200
- _POLL_MAX = 400
- _CACHE_TTL = 2 * _POLL_MAX # 800s — stale after two max-intervals with no update
- _cached: dict | None = None
- @dataclass
- class HealthResult:
- ok: bool
- reason: str | None = None
- def to_dict(self) -> dict:
- d: dict = {'ok': self.ok}
- if self.reason is not None:
- d['reason'] = self.reason
- return d
- def get_cached() -> tuple[dict | None, bool]:
- """Return (result, stale) where stale=True means the cache has expired."""
- if _cached is None:
- return None, False
- age = (datetime.now(timezone.utc) - datetime.fromisoformat(_cached['checked_at'])).total_seconds()
- return _cached, age > _CACHE_TTL
- async def caldav_checks(calendar_name: str = DEFAULT_CALENDAR_NAME) -> tuple[HealthResult, HealthResult]:
- """Open one CalDAV connection and run both the connection and calendar-exists checks."""
- from room_cal._utils import get_config
- cfg = get_config()
- if cfg.store.active != 'caldav':
- return HealthResult(ok=True), HealthResult(ok=True)
- cal_cfg = cfg.store.caldav
- cal = Calendar(calendar_name, url=cal_cfg.url, username=cal_cfg.username, password=cal_cfg.password)
- try:
- async with await cal._client() as client:
- conn = HealthResult(ok=True)
- try:
- await cal._get_calendar(client)
- cal_result = HealthResult(ok=True)
- except Exception as e:
- cal_result = HealthResult(ok=False, reason=str(e))
- except Exception as e:
- conn = HealthResult(ok=False, reason=str(e))
- cal_result = HealthResult(ok=False, reason='connection failed')
- return conn, cal_result
- async def _probe_service(key: str, api: ServiceAPI) -> HealthResult:
- try:
- await api._clients[key].probe()
- return HealthResult(ok=True)
- except ServiceAuthError as e:
- return HealthResult(ok=False, reason=str(e))
- except Exception as e:
- return HealthResult(ok=False, reason=f'probe error: {e}')
- async def credentials_checks(api: ServiceAPI | None = None) -> dict[str, HealthResult]:
- """Probe each configured service's live session."""
- if api is None:
- api = ServiceAPI()
- out: dict[str, HealthResult] = {
- key: HealthResult(ok=False, reason=f'disabled at startup: {reason}')
- for key, reason in api._disabled.items()
- }
- keys = [s.key for s in services() if s.key in api._clients]
- if keys:
- results = await asyncio.gather(*[_probe_service(k, api) for k in keys])
- out.update(zip(keys, results))
- return out
- async def probe_once() -> None:
- """Run one health probe, update the cache, and dispatch periodic work if all-green."""
- global _cached
- periodic_disabled = bool(os.getenv('DISABLE_PERIODIC'))
- try:
- api = ServiceAPI()
- conn, cal_result = await caldav_checks(DEFAULT_CALENDAR_NAME)
- creds = {} if periodic_disabled else await credentials_checks(api)
- caldav_ok = conn.ok and cal_result.ok
- creds_ok = all(v.ok for v in creds.values())
- if caldav_ok and creds_ok:
- overall = 'ok'
- elif caldav_ok:
- overall = 'degraded'
- else:
- overall = 'error'
- _cached = {
- 'status': overall,
- 'checks': {
- 'caldav_connection': conn.to_dict(),
- 'calendar': cal_result.to_dict(),
- 'credentials': {k: v.to_dict() for k, v in creds.items()},
- },
- 'checked_at': datetime.now(timezone.utc).isoformat(),
- }
- if caldav_ok and creds_ok:
- from room_cal._periodic import run_due, _register_all_periodics
- _register_all_periodics()
- await run_due(api)
- except Exception as e:
- _log.error('health probe error: %s', e, exc_info=True)
- async def run_health_loop() -> None:
- """Background task: probe health periodically, cache the result, and dispatch periodic work."""
- while True:
- await probe_once()
- await asyncio.sleep(random.uniform(_POLL_MIN, _POLL_MAX))
|