"""Проверка живости для Docker HEALTHCHECK: python -m messenger.healthcheck.""" from __future__ import annotations import os import urllib.error import urllib.request DEFAULT_PORT = "8080" TIMEOUT_SECONDS = 2 def liveness_url() -> str: port = os.environ.get("APP_PORT", DEFAULT_PORT).strip() or DEFAULT_PORT return "http://127.0.0.1:%s/health/live" % port def main() -> int: try: with urllib.request.urlopen(liveness_url(), timeout=TIMEOUT_SECONDS) as response: return 0 if response.status == 200 else 1 except (urllib.error.URLError, OSError, ValueError): return 1 if __name__ == "__main__": raise SystemExit(main())