fix: 防止新依赖缺失导致后端启动崩溃
scheduler和reports模块加载失败时不再拖垮整个应用, 核心功能(登录/项目/提交)可正常使用。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
90707005ed
commit
6d636e9799
@ -41,7 +41,11 @@ from routers.submissions import router as submissions_router
|
|||||||
from routers.costs import router as costs_router
|
from routers.costs import router as costs_router
|
||||||
from routers.dashboard import router as dashboard_router
|
from routers.dashboard import router as dashboard_router
|
||||||
from routers.roles import router as roles_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(auth_router)
|
||||||
app.include_router(users_router)
|
app.include_router(users_router)
|
||||||
@ -50,7 +54,8 @@ app.include_router(submissions_router)
|
|||||||
app.include_router(costs_router)
|
app.include_router(costs_router)
|
||||||
app.include_router(dashboard_router)
|
app.include_router(dashboard_router)
|
||||||
app.include_router(roles_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")
|
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")
|
@app.on_event("startup")
|
||||||
async def start_scheduler():
|
async def start_scheduler():
|
||||||
"""启动定时任务调度器"""
|
"""启动定时任务调度器"""
|
||||||
|
try:
|
||||||
from services.scheduler_service import setup_scheduler
|
from services.scheduler_service import setup_scheduler
|
||||||
setup_scheduler()
|
setup_scheduler()
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"[定时任务] 启动失败(不影响核心功能): {e}")
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("shutdown")
|
@app.on_event("shutdown")
|
||||||
async def stop_scheduler():
|
async def stop_scheduler():
|
||||||
"""关闭定时任务调度器"""
|
"""关闭定时任务调度器"""
|
||||||
|
try:
|
||||||
from services.scheduler_service import scheduler
|
from services.scheduler_service import scheduler
|
||||||
scheduler.shutdown(wait=False)
|
scheduler.shutdown(wait=False)
|
||||||
logger.info("[定时任务] 已关闭")
|
logger.info("[定时任务] 已关闭")
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@app.on_event("startup")
|
@app.on_event("startup")
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user