fix: 全部检查跳过已删除账号,未捕获异常不再把内部错误返回给页面
This commit is contained in:
@@ -11,6 +11,7 @@ from admin.session import admin_session_active, clear_admin_session, write_admin
|
||||
from admin.settings import SettingsError, load_config, login_config_view, save_settings, settings_view
|
||||
from admin.users import (
|
||||
check_all,
|
||||
check_state,
|
||||
check_employee,
|
||||
clear_proxy,
|
||||
delete_user,
|
||||
@@ -181,10 +182,15 @@ def get_users(db: Session = Depends(get_db), _: str = Depends(require_admin)) ->
|
||||
|
||||
|
||||
@admin_router.post("/users/check-all")
|
||||
def post_check_all(db: Session = Depends(get_db), _: str = Depends(require_admin)) -> list[dict]:
|
||||
def post_check_all(db: Session = Depends(get_db), _: str = Depends(require_admin)) -> list[dict] | dict:
|
||||
return check_all(db)
|
||||
|
||||
|
||||
@admin_router.get("/users/check-all")
|
||||
def get_check_all(_: str = Depends(require_admin)) -> dict:
|
||||
return check_state()
|
||||
|
||||
|
||||
@admin_router.post("/users/{employee_id}/check")
|
||||
def post_check(employee_id: str, db: Session = Depends(get_db), _: str = Depends(require_admin)) -> dict:
|
||||
return check_employee(db, employee_id)
|
||||
|
||||
+63
-10
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user