fix: 单实例锁、应用日志和容器健康检查
This commit is contained in:
+29
-1
@@ -3,10 +3,18 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import logging
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
logging.basicConfig(
|
||||
level=os.getenv("EUNIV_LOG_LEVEL", "INFO"),
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
)
|
||||
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||
logging.getLogger("httpcore").setLevel(logging.WARNING)
|
||||
|
||||
from fastapi import Depends, FastAPI, HTTPException, Request, Response, WebSocket, WebSocketDisconnect
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -71,6 +79,11 @@ class StudyControlBody(_BaseModel):
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_app: FastAPI):
|
||||
from euniv.instance_lock import acquire_instance_lock
|
||||
|
||||
acquire_instance_lock()
|
||||
if os.environ.get("EUNIV_DISABLE_REFRESH") == "1":
|
||||
logging.getLogger("euniv.api").warning("EUNIV_DISABLE_REFRESH=1,后台刷新、自动学习和代理池都不会启动")
|
||||
init_db()
|
||||
ensure_admin_seed()
|
||||
from admin.settings import ensure_key_check
|
||||
@@ -129,7 +142,22 @@ class QrPollBody(BaseModel):
|
||||
|
||||
@app.get("/api/health")
|
||||
def health() -> dict:
|
||||
return {"ok": True}
|
||||
from sqlalchemy import text
|
||||
|
||||
from euniv.db import SessionLocal
|
||||
|
||||
with SessionLocal() as db:
|
||||
db.execute(text("SELECT 1"))
|
||||
study_alive = False
|
||||
try:
|
||||
from euniv import autostudy
|
||||
|
||||
worker = autostudy._worker
|
||||
thread = getattr(worker, "_thread", None) if worker is not None else None
|
||||
study_alive = bool(thread is not None and thread.is_alive())
|
||||
except Exception:
|
||||
study_alive = False
|
||||
return {"ok": True, "database": True, "study": study_alive}
|
||||
|
||||
|
||||
def _open_session(response: Response, payload: dict) -> dict:
|
||||
|
||||
Reference in New Issue
Block a user