healthcheck.py 1004 B

1234567891011121314151617181920212223242526272829303132
  1. """Bounded container health probe for the edge runtime."""
  2. from __future__ import annotations
  3. import json
  4. import os
  5. from datetime import UTC, datetime, timedelta
  6. from pathlib import Path
  7. def main() -> int:
  8. path = Path(os.environ.get("EDGE_HEALTH_PATH", "/run/edge/tmp/health.json"))
  9. try:
  10. value = json.loads(path.read_text(encoding="utf-8"))
  11. checked = datetime.fromisoformat(str(value["checked_at"]).replace("Z", "+00:00"))
  12. if (
  13. set(value) - {
  14. "checked_at", "crl_next_update", "gateway_id_digest", "last_cycle",
  15. "queue", "status",
  16. }
  17. or value["status"] not in {"healthy", "degraded"}
  18. or checked.tzinfo is None
  19. or datetime.now(UTC) - checked.astimezone(UTC) > timedelta(seconds=90)
  20. ):
  21. return 1
  22. except (OSError, ValueError, KeyError, TypeError, json.JSONDecodeError):
  23. return 1
  24. return 0
  25. if __name__ == "__main__":
  26. raise SystemExit(main())