#!/usr/bin/env python3 # -*- coding: utf-8 -*- """作业 7 · 故障自愈 HTTP 工具(FastAPI)。""" from __future__ import annotations import _bootstrap # noqa: F401 from fastapi import FastAPI from pydantic import BaseModel from 作业7_self_heal import ( detect_vllm_anomaly, restart_vllm_container, send_recovery_notification, ) app = FastAPI(title="vLLM Self Heal API", version="1.0.0") class NotifyBody(BaseModel): message: str detail: str = "" @app.get("/vllm/detect_anomaly", operation_id="detect_vllm_anomaly", summary="检测 vLLM OOM 或容器异常") def api_detect() -> dict: return detect_vllm_anomaly() @app.post("/vllm/restart", operation_id="restart_vllm_container", summary="重启 vLLM 容器") def api_restart() -> dict: return restart_vllm_container() @app.post("/notify/recovery", operation_id="send_recovery_notification", summary="发送恢复通知到钉钉") def api_notify(body: NotifyBody) -> dict: return send_recovery_notification(body.message, body.detail) @app.get("/health") def health() -> dict[str, str]: return {"status": "ok"} if __name__ == "__main__": import uvicorn import _config as C uvicorn.run("作业7_self_heal_api:app", host="0.0.0.0", port=C.SELF_HEAL_PORT, reload=False)