fix: 防止新依赖缺失导致后端启动崩溃
All checks were successful
Build and Deploy Backend / build-and-deploy (push) Successful in 1m22s
Build and Deploy Web / build-and-deploy (push) Successful in 59s

scheduler和reports模块加载失败时不再拖垮整个应用,
核心功能(登录/项目/提交)可正常使用。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
seaislee1209 2026-02-13 18:44:23 +08:00
parent 90707005ed
commit 6d636e9799

View File

@ -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")