41 lines
1.1 KiB
Python
41 lines
1.1 KiB
Python
"""健康检查脚本。
|
|
|
|
用法:python scripts/health_check.py [--host HOST] [--port PORT]
|
|
"""
|
|
import argparse
|
|
import sys
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
SRC = Path(__file__).resolve().parent.parent / "src"
|
|
sys.path.insert(0, str(SRC))
|
|
|
|
from config.app import TORNADO_HOST, TORNADO_PORT
|
|
|
|
|
|
def check(host: str, port: int) -> None:
|
|
url = f"http://{host}:{port}/api/v1/health"
|
|
try:
|
|
with urllib.request.urlopen(url, timeout=10) as resp:
|
|
body = resp.read().decode("utf-8")
|
|
if resp.status == 200:
|
|
print(f"[OK] {url} -> {resp.status} {body}")
|
|
else:
|
|
print(f"[WARN] {url} -> {resp.status} {body}")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(f"[FAIL] {url} 不可达: {e}")
|
|
sys.exit(1)
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser(description="健康检查")
|
|
parser.add_argument("--host", default=TORNADO_HOST)
|
|
parser.add_argument("--port", type=int, default=TORNADO_PORT)
|
|
args = parser.parse_args()
|
|
check(args.host, args.port)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|