33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
"""Docker HEALTHCHECK probe without curl/wget in the image: `python -m messenger.healthcheck`.
|
|
|
|
(no arguments) API: GET /health/ready on the loopback health port
|
|
(APP_HEALTH_PORT, lab 7: the main port requires mTLS)
|
|
--file /tmp/heartbeat --max-age 30 relay/worker: the main loop wrote its heartbeat recently
|
|
(it writes it only while its database/broker work)
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
|
|
|
|
def main(argv: list[str]) -> int:
|
|
if len(argv) == 4 and argv[0] == "--file" and argv[2] == "--max-age":
|
|
try:
|
|
with open(argv[1], encoding="ascii") as f:
|
|
return 0 if time.time() - float(f.read().strip()) <= float(argv[3]) else 1
|
|
except (OSError, ValueError):
|
|
return 1
|
|
port = os.environ.get("APP_HEALTH_PORT", "").strip() or os.environ.get("APP_PORT", "8080").strip() or "8080"
|
|
try:
|
|
with urllib.request.urlopen(f"http://127.0.0.1:{port}/health/ready", timeout=2) as response:
|
|
return 0 if response.status == 200 else 1
|
|
except Exception:
|
|
return 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main(sys.argv[1:]))
|