From f7995a3b3abb29bc0e592cd1798bb6a29fb95cad Mon Sep 17 00:00:00 2001 From: anor Date: Wed, 10 Jun 2026 17:42:29 +1000 Subject: [PATCH] =?UTF-8?q?feat(3.1):=20/health=20endpoint=20(503=20=D0=BF?= =?UTF-8?q?=D1=80=D0=B8=20=D0=BD=D0=B5=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF?= =?UTF-8?q?=D0=BD=D0=BE=D1=81=D1=82=D0=B8=20=D0=91=D0=94)=20+=20db.ping=20?= =?UTF-8?q?=D0=B4=D0=BB=D1=8F=20=D0=BC=D0=BE=D0=BD=D0=B8=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=B8=D0=BD=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5 --- db.py | 6 ++++++ main.py | 23 +++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/db.py b/db.py index 20f9f97..419fbb8 100644 --- a/db.py +++ b/db.py @@ -70,6 +70,12 @@ def _conn() -> sqlite3.Connection: return conn +def ping(): + """Лёгкая проверка доступности БД для /health.""" + with _conn() as conn: + conn.execute("SELECT 1").fetchone() + + def init_db(): with _conn() as conn: conn.executescript(_CREATE) diff --git a/main.py b/main.py index bea4ffe..70710bc 100644 --- a/main.py +++ b/main.py @@ -9,11 +9,11 @@ import time from pathlib import Path from fastapi import FastAPI, HTTPException, Request, UploadFile -from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse +from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, StreamingResponse from fastapi.staticfiles import StaticFiles from auth import authenticate, verify_token, make_download_ticket, verify_download_ticket -from db import clear_history, get_audit_log, get_history, get_history_any_user, init_db, list_sessions, log_action, save_message +from db import clear_history, get_audit_log, get_history, get_history_any_user, init_db, list_sessions, log_action, ping as db_ping, save_message from tus_upload import register_tus # Backend: "claude" (Docker sandbox), "glm" (GLM-5 + CAG), "auto" (route by task) @@ -341,6 +341,25 @@ def _compute_data_periods(): return result +@app.get("/health") +async def health(): + """Public health-check для мониторинга: 503 если БД недоступна (критично), + иначе 200 с состоянием знаниевой базы. Чтобы владелец узнавал о падении не + от пользователя.""" + checks = {} + healthy = True + try: + await asyncio.to_thread(db_ping) + checks["db"] = "ok" + except Exception as e: # noqa: BLE001 + checks["db"] = f"error: {e}" + healthy = False + proj = os.environ.get("SMETA_PROJECT_DIR", "") + checks["knowledge_base"] = "ok" if (proj and Path(proj).exists()) else "missing" + payload = {"status": "ok" if healthy else "down", "checks": checks, "version": APP_VERSION} + return JSONResponse(payload, status_code=200 if healthy else 503) + + @app.get("/api/data-version") async def data_version(): """Public — на какой ценовой период актуальна корпусная база (свежесть данных)."""