From 6d636e97997d0da7b95b90e76fead083bd24fa9e Mon Sep 17 00:00:00 2001 From: seaislee1209 Date: Fri, 13 Feb 2026 18:44:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=98=B2=E6=AD=A2=E6=96=B0=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=E7=BC=BA=E5=A4=B1=E5=AF=BC=E8=87=B4=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E5=90=AF=E5=8A=A8=E5=B4=A9=E6=BA=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit scheduler和reports模块加载失败时不再拖垮整个应用, 核心功能(登录/项目/提交)可正常使用。 Co-Authored-By: Claude Opus 4.6 --- backend/main.py | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/backend/main.py b/backend/main.py index 65bb580..8364f7d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -41,7 +41,11 @@ from routers.submissions import router as submissions_router from routers.costs import router as costs_router from routers.dashboard import router as dashboard_router from routers.roles import router as roles_router -from routers.reports import router as reports_router +try: + from routers.reports import router as reports_router +except ImportError as e: + reports_router = None + logging.warning(f"[路由] reports 模块加载失败: {e}") app.include_router(auth_router) app.include_router(users_router) @@ -50,7 +54,8 @@ app.include_router(submissions_router) app.include_router(costs_router) app.include_router(dashboard_router) app.include_router(roles_router) -app.include_router(reports_router) +if reports_router: + app.include_router(reports_router) # 前端静态文件 frontend_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "frontend", "dist") @@ -68,16 +73,22 @@ if os.path.exists(frontend_dir): @app.on_event("startup") async def start_scheduler(): """启动定时任务调度器""" - from services.scheduler_service import setup_scheduler - setup_scheduler() + try: + from services.scheduler_service import setup_scheduler + setup_scheduler() + except Exception as e: + logger.warning(f"[定时任务] 启动失败(不影响核心功能): {e}") @app.on_event("shutdown") async def stop_scheduler(): """关闭定时任务调度器""" - from services.scheduler_service import scheduler - scheduler.shutdown(wait=False) - logger.info("[定时任务] 已关闭") + try: + from services.scheduler_service import scheduler + scheduler.shutdown(wait=False) + logger.info("[定时任务] 已关闭") + except Exception: + pass @app.on_event("startup")