admin update

This commit is contained in:
jeremygan2021
2026-02-17 12:17:23 +08:00
parent a4475e743d
commit f6c361dd13
3 changed files with 101 additions and 228 deletions

View File

@@ -73,6 +73,13 @@ dashscope.api_key = 'sk-ce2404f55f744a1987d5ece61c6bac58'
QWEN_MODEL = 'qwen-vl-max' # Default model
AVAILABLE_QWEN_MODELS = ["qwen-vl-max", "qwen-vl-plus"]
# 清理配置 (Cleanup Config)
CLEANUP_CONFIG = {
"enabled": os.getenv("AUTO_CLEANUP_ENABLED", "True").lower() == "true",
"lifetime": int(os.getenv("FILE_LIFETIME_SECONDS", "3600")),
"interval": int(os.getenv("CLEANUP_INTERVAL_SECONDS", "600"))
}
# API Tags (用于文档分类)
TAG_GENERAL = "General Segmentation (通用分割)"
TAG_TAROT = "Tarot Analysis (塔罗牌分析)"
@@ -108,14 +115,24 @@ async def verify_api_key(api_key: Optional[str] = Depends(api_key_header)):
# 4. Lifespan Management (生命周期管理)
# ==========================================
async def cleanup_old_files(directory: str, lifetime_seconds: int, interval_seconds: int):
async def cleanup_old_files(directory: str):
"""
后台任务:定期清理过期的图片文件
- 动态读取 CLEANUP_CONFIG 配置
"""
print(f"🧹 自动清理任务已启动 | 目录: {directory} | 生命周期: {lifetime_seconds}s | 检查间隔: {interval_seconds}s")
print(f"🧹 自动清理任务已启动 | 目录: {directory}")
while True:
try:
await asyncio.sleep(interval_seconds)
# 动态读取配置
interval = CLEANUP_CONFIG["interval"]
lifetime = CLEANUP_CONFIG["lifetime"]
enabled = CLEANUP_CONFIG["enabled"]
await asyncio.sleep(interval)
if not enabled:
continue
current_time = time.time()
count = 0
# 遍历所有文件(包括子目录)
@@ -125,7 +142,7 @@ async def cleanup_old_files(directory: str, lifetime_seconds: int, interval_seco
# 获取文件修改时间
try:
file_mtime = os.path.getmtime(file_path)
if current_time - file_mtime > lifetime_seconds:
if current_time - file_mtime > lifetime:
os.remove(file_path)
count += 1
except OSError:
@@ -142,7 +159,7 @@ async def cleanup_old_files(directory: str, lifetime_seconds: int, interval_seco
pass
if count > 0:
print(f"🧹 已清理 {count} 个过期文件")
print(f"🧹 已清理 {count} 个过期文件 (Lifetime: {lifetime}s)")
except asyncio.CancelledError:
print("🛑 清理任务已停止")
@@ -185,16 +202,12 @@ async def lifespan(app: FastAPI):
# --- 启动后台清理任务 ---
cleanup_task_handle = None
# 优先读取环境变量,否则使用默认值
if os.getenv("AUTO_CLEANUP_ENABLED", "False").lower() == "true":
try:
lifetime = int(os.getenv("FILE_LIFETIME_SECONDS", "3600"))
interval = int(os.getenv("CLEANUP_INTERVAL_SECONDS", "600"))
cleanup_task_handle = asyncio.create_task(
cleanup_old_files(RESULT_IMAGE_DIR, lifetime, interval)
)
except Exception as e:
print(f"启动清理任务失败: {e}")
try:
cleanup_task_handle = asyncio.create_task(
cleanup_old_files(RESULT_IMAGE_DIR)
)
except Exception as e:
print(f"启动清理任务失败: {e}")
# -----------------------
yield
@@ -1180,9 +1193,8 @@ async def trigger_cleanup():
Manually trigger cleanup
"""
try:
# Re-use logic from cleanup_old_files but force it for all files > 0 seconds if we want deep clean?
# Or just use the standard lifetime. Let's use standard lifetime but run it now.
lifetime = int(os.getenv("FILE_LIFETIME_SECONDS", "3600"))
# Use global config
lifetime = CLEANUP_CONFIG["lifetime"]
count = 0
current_time = time.time()
@@ -1207,7 +1219,7 @@ async def trigger_cleanup():
except:
pass
return {"status": "success", "message": f"Cleaned {count} files"}
return {"status": "success", "message": f"Cleaned {count} files (Lifetime: {lifetime}s)"}
except Exception as e:
return {"status": "error", "message": str(e)}
@@ -1222,13 +1234,28 @@ async def get_config(request: Request):
return {
"device": device,
"cleanup_enabled": os.getenv("AUTO_CLEANUP_ENABLED"),
"file_lifetime": os.getenv("FILE_LIFETIME_SECONDS"),
"cleanup_interval": os.getenv("CLEANUP_INTERVAL_SECONDS"),
"cleanup_config": CLEANUP_CONFIG,
"current_qwen_model": QWEN_MODEL,
"available_qwen_models": AVAILABLE_QWEN_MODELS
}
@app.post("/admin/api/config/cleanup", dependencies=[Depends(verify_admin)])
async def update_cleanup_config(
enabled: bool = Form(...),
lifetime: int = Form(...),
interval: int = Form(...)
):
"""
Update cleanup configuration
"""
global CLEANUP_CONFIG
CLEANUP_CONFIG["enabled"] = enabled
CLEANUP_CONFIG["lifetime"] = lifetime
CLEANUP_CONFIG["interval"] = interval
print(f"Updated Cleanup Config: {CLEANUP_CONFIG}")
return {"status": "success", "message": "Cleanup configuration updated", "config": CLEANUP_CONFIG}
@app.post("/admin/api/config/model", dependencies=[Depends(verify_admin)])
async def set_model(model: str = Form(...)):
"""