_health.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import asyncio
  2. import logging
  3. import os
  4. import random
  5. from dataclasses import dataclass
  6. from datetime import datetime, timezone
  7. from room_cal.store import Calendar, DEFAULT_CALENDAR_NAME
  8. from room_cal.service_api import ServiceAPI
  9. from room_cal.service_api.types import ServiceAuthError
  10. from room_cal._utils import services
  11. _log = logging.getLogger(__name__)
  12. _POLL_MIN = 200
  13. _POLL_MAX = 400
  14. _CACHE_TTL = 2 * _POLL_MAX # 800s — stale after two max-intervals with no update
  15. _cached: dict | None = None
  16. @dataclass
  17. class HealthResult:
  18. ok: bool
  19. reason: str | None = None
  20. def to_dict(self) -> dict:
  21. d: dict = {'ok': self.ok}
  22. if self.reason is not None:
  23. d['reason'] = self.reason
  24. return d
  25. def get_cached() -> tuple[dict | None, bool]:
  26. """Return (result, stale) where stale=True means the cache has expired."""
  27. if _cached is None:
  28. return None, False
  29. age = (datetime.now(timezone.utc) - datetime.fromisoformat(_cached['checked_at'])).total_seconds()
  30. return _cached, age > _CACHE_TTL
  31. async def caldav_checks(calendar_name: str = DEFAULT_CALENDAR_NAME) -> tuple[HealthResult, HealthResult]:
  32. """Open one CalDAV connection and run both the connection and calendar-exists checks."""
  33. from room_cal._utils import get_config
  34. cfg = get_config()
  35. if cfg.store.active != 'caldav':
  36. return HealthResult(ok=True), HealthResult(ok=True)
  37. cal_cfg = cfg.store.caldav
  38. cal = Calendar(calendar_name, url=cal_cfg.url, username=cal_cfg.username, password=cal_cfg.password)
  39. try:
  40. async with await cal._client() as client:
  41. conn = HealthResult(ok=True)
  42. try:
  43. await cal._get_calendar(client)
  44. cal_result = HealthResult(ok=True)
  45. except Exception as e:
  46. cal_result = HealthResult(ok=False, reason=str(e))
  47. except Exception as e:
  48. conn = HealthResult(ok=False, reason=str(e))
  49. cal_result = HealthResult(ok=False, reason='connection failed')
  50. return conn, cal_result
  51. async def _probe_service(key: str, api: ServiceAPI) -> HealthResult:
  52. try:
  53. await api._clients[key].probe()
  54. return HealthResult(ok=True)
  55. except ServiceAuthError as e:
  56. return HealthResult(ok=False, reason=str(e))
  57. except Exception as e:
  58. return HealthResult(ok=False, reason=f'probe error: {e}')
  59. async def credentials_checks(api: ServiceAPI | None = None) -> dict[str, HealthResult]:
  60. """Probe each configured service's live session."""
  61. if api is None:
  62. api = ServiceAPI()
  63. out: dict[str, HealthResult] = {
  64. key: HealthResult(ok=False, reason=f'disabled at startup: {reason}')
  65. for key, reason in api._disabled.items()
  66. }
  67. keys = [s.key for s in services() if s.key in api._clients]
  68. if keys:
  69. results = await asyncio.gather(*[_probe_service(k, api) for k in keys])
  70. out.update(zip(keys, results))
  71. return out
  72. async def probe_once() -> None:
  73. """Run one health probe, update the cache, and dispatch periodic work if all-green."""
  74. global _cached
  75. periodic_disabled = bool(os.getenv('DISABLE_PERIODIC'))
  76. try:
  77. api = ServiceAPI()
  78. conn, cal_result = await caldav_checks(DEFAULT_CALENDAR_NAME)
  79. creds = {} if periodic_disabled else await credentials_checks(api)
  80. caldav_ok = conn.ok and cal_result.ok
  81. creds_ok = all(v.ok for v in creds.values())
  82. if caldav_ok and creds_ok:
  83. overall = 'ok'
  84. elif caldav_ok:
  85. overall = 'degraded'
  86. else:
  87. overall = 'error'
  88. _cached = {
  89. 'status': overall,
  90. 'checks': {
  91. 'caldav_connection': conn.to_dict(),
  92. 'calendar': cal_result.to_dict(),
  93. 'credentials': {k: v.to_dict() for k, v in creds.items()},
  94. },
  95. 'checked_at': datetime.now(timezone.utc).isoformat(),
  96. }
  97. if caldav_ok and creds_ok:
  98. from room_cal._periodic import run_due, _register_all_periodics
  99. _register_all_periodics()
  100. await run_due(api)
  101. except Exception as e:
  102. _log.error('health probe error: %s', e, exc_info=True)
  103. async def run_health_loop() -> None:
  104. """Background task: probe health periodically, cache the result, and dispatch periodic work."""
  105. while True:
  106. await probe_once()
  107. await asyncio.sleep(random.uniform(_POLL_MIN, _POLL_MAX))