fix: 全部检查跳过已删除账号,未捕获异常不再把内部错误返回给页面

This commit is contained in:
2026-11-16 17:08:36 +08:00
parent 3cff9dc37a
commit 95e9355dc0
5 changed files with 153 additions and 28 deletions
+63 -10
View File
@@ -3,6 +3,7 @@
from __future__ import annotations
import json
import threading
from datetime import datetime, timezone
from fastapi import HTTPException
@@ -159,7 +160,58 @@ def check_employee(db: Session, employee_id: str) -> dict:
return payload
def check_all(db: Session) -> list[dict]:
def _check_saved(db: Session, employee_ids: list[str]) -> list[dict]:
results = []
for employee_id in employee_ids:
try:
results.append(check_employee(db, employee_id))
except HTTPException as exc:
if exc.status_code == 404:
continue
raise
except Exception:
results.append({"employee_id": employee_id, "result": "检查失败", "detail": "检查失败"})
return results
_CHECK_INLINE = 8
_check_guard = threading.Lock()
_check_running = False
_check_results: list[dict] = []
_check_error = ""
def check_state() -> dict:
with _check_guard:
return {"running": _check_running, "results": list(_check_results), "error": _check_error}
def _run_check(employee_ids: list[str]) -> None:
global _check_running, _check_results, _check_error
from euniv.db import SessionLocal
results: list[dict] = []
error = ""
try:
for employee_id in employee_ids:
with SessionLocal() as db:
try:
results.append(check_employee(db, employee_id))
except HTTPException as exc:
if exc.status_code != 404:
raise
except Exception:
results.append({"employee_id": employee_id, "result": "检查失败", "detail": "检查失败"})
except Exception:
error = "检查失败"
with _check_guard:
_check_results = results
_check_error = error
_check_running = False
def check_all(db: Session) -> list[dict] | dict:
global _check_running
employee_ids = list(
db.scalars(
select(Account.employee_id)
@@ -167,15 +219,16 @@ def check_all(db: Session) -> list[dict]:
.order_by(Account.employee_id)
)
)
results = []
for employee_id in employee_ids:
try:
results.append(check_employee(db, employee_id))
except HTTPException:
raise
except Exception:
results.append({"employee_id": employee_id, "result": "检查失败", "detail": "检查失败"})
return results
if len(employee_ids) <= _CHECK_INLINE:
return _check_saved(db, employee_ids)
with _check_guard:
if _check_running:
return check_state()
_check_running = True
_check_results = []
_check_error = ""
threading.Thread(target=_run_check, args=(employee_ids,), name="euniv-check-all", daemon=True).start()
return {"running": True, "results": [], "error": ""}
def set_paused(db: Session, employee_id: str, paused: bool) -> dict: