Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
54b1241
1
Parent(s): 097b672
Claude Code: Add /status endpoint with structured health checks (db, env, fs)
Browse files
app.py
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
| 4 |
-
from
|
|
|
|
| 5 |
from fastapi.staticfiles import StaticFiles
|
|
|
|
| 6 |
|
| 7 |
# Initialize FastAPI application
|
| 8 |
app = FastAPI()
|
|
@@ -27,6 +29,59 @@ async def health_check():
|
|
| 27 |
}
|
| 28 |
|
| 29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
@app.websocket("/ws/agents")
|
| 31 |
async def websocket_agents(websocket: WebSocket):
|
| 32 |
"""WebSocket endpoint for real-time agent status updates."""
|
|
|
|
| 1 |
import asyncio
|
| 2 |
import os
|
| 3 |
from datetime import datetime
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from fastapi import FastAPI, WebSocket, WebSocketDisconnect, HTTPException
|
| 6 |
from fastapi.staticfiles import StaticFiles
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
|
| 9 |
# Initialize FastAPI application
|
| 10 |
app = FastAPI()
|
|
|
|
| 29 |
}
|
| 30 |
|
| 31 |
|
| 32 |
+
@app.get("/status")
|
| 33 |
+
async def status_check():
|
| 34 |
+
"""Comprehensive health check with db, env, and filesystem validation."""
|
| 35 |
+
checks = {"db": "ok", "env": "ok", "fs": "ok"}
|
| 36 |
+
overall_healthy = True
|
| 37 |
+
|
| 38 |
+
# 1. Database check - lightweight dataset list call
|
| 39 |
+
try:
|
| 40 |
+
from huggingface_hub import HfApi
|
| 41 |
+
api = HfApi()
|
| 42 |
+
# Lightweight call - just check if we can reach HF
|
| 43 |
+
repo_id = os.environ.get("OPENCLAW_DATASET_REPO", "")
|
| 44 |
+
if repo_id:
|
| 45 |
+
api.repo_info(repo_id=repo_id, repo_type="dataset")
|
| 46 |
+
checks["db"] = "ok"
|
| 47 |
+
else:
|
| 48 |
+
checks["db"] = "error: OPENCLAW_DATASET_REPO not set"
|
| 49 |
+
overall_healthy = False
|
| 50 |
+
except Exception as e:
|
| 51 |
+
checks["db"] = f"error: {str(e)}"
|
| 52 |
+
overall_healthy = False
|
| 53 |
+
|
| 54 |
+
# 2. Environment check - verify critical variables
|
| 55 |
+
critical_vars = ["HF_TOKEN", "OPENCLAW_DATASET_REPO"]
|
| 56 |
+
missing_vars = [v for v in critical_vars if not os.environ.get(v)]
|
| 57 |
+
if missing_vars:
|
| 58 |
+
checks["env"] = f"error: missing {', '.join(missing_vars)}"
|
| 59 |
+
overall_healthy = False
|
| 60 |
+
else:
|
| 61 |
+
checks["env"] = "ok"
|
| 62 |
+
|
| 63 |
+
# 3. Filesystem check - touch and delete test file
|
| 64 |
+
test_file = Path("/tmp/test_write")
|
| 65 |
+
try:
|
| 66 |
+
test_file.touch()
|
| 67 |
+
test_file.unlink()
|
| 68 |
+
checks["fs"] = "ok"
|
| 69 |
+
except Exception as e:
|
| 70 |
+
checks["fs"] = f"error: {str(e)}"
|
| 71 |
+
overall_healthy = False
|
| 72 |
+
|
| 73 |
+
status = "healthy" if overall_healthy else "degraded"
|
| 74 |
+
|
| 75 |
+
# Return HTTP 503 if critical checks fail
|
| 76 |
+
if not overall_healthy:
|
| 77 |
+
raise HTTPException(status_code=503, detail={
|
| 78 |
+
"status": status,
|
| 79 |
+
"checks": checks
|
| 80 |
+
})
|
| 81 |
+
|
| 82 |
+
return {"status": status, "checks": checks}
|
| 83 |
+
|
| 84 |
+
|
| 85 |
@app.websocket("/ws/agents")
|
| 86 |
async def websocket_agents(websocket: WebSocket):
|
| 87 |
"""WebSocket endpoint for real-time agent status updates."""
|