docs: 移动蓝绿部署指南到 troubleshooting 目录
This commit is contained in:
@@ -28,6 +28,8 @@ from app.api.router import api_router
|
||||
from app.dependencies import init_shared_services, cleanup_shared_services
|
||||
# 导入异常处理器和异常类
|
||||
from app.utils.response import AppException, app_exception_handler
|
||||
# 导入定时任务
|
||||
from app.tasks.reminder_task import check_unreplied_sessions
|
||||
|
||||
# 配置日志格式
|
||||
logging.basicConfig(
|
||||
@@ -88,6 +90,9 @@ async def lifespan(app: FastAPI):
|
||||
# 初始化默认数据
|
||||
await _init_default_data()
|
||||
|
||||
# 启动超时提醒定时任务
|
||||
_start_scheduler()
|
||||
|
||||
logger.info("✅ 企微IT智能服务台启动完成")
|
||||
|
||||
yield # 应用运行中
|
||||
@@ -95,12 +100,75 @@ async def lifespan(app: FastAPI):
|
||||
# ===== 关闭事件 =====
|
||||
logger.info("👋 企微IT智能服务台关闭中...")
|
||||
|
||||
# 停止超时提醒定时任务
|
||||
_stop_scheduler()
|
||||
|
||||
# 清理共享服务实例(关闭 Redis 连接、httpx 连接池等)
|
||||
await cleanup_shared_services()
|
||||
|
||||
logger.info("✅ 企微IT智能服务台已关闭")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 定时任务调度器
|
||||
# --------------------------------------------------------------------------
|
||||
# 全局调度器实例
|
||||
_scheduler = None
|
||||
|
||||
|
||||
def _start_scheduler():
|
||||
"""启动定时任务调度器。
|
||||
|
||||
启动 APScheduler 调度器,注册超时提醒定时任务。
|
||||
每 30 秒检查一次超时未回复的会话。
|
||||
"""
|
||||
global _scheduler
|
||||
|
||||
if _scheduler is not None:
|
||||
logger.warning("调度器已启动,跳过")
|
||||
return
|
||||
|
||||
try:
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
|
||||
_scheduler = AsyncIOScheduler()
|
||||
|
||||
# 注册超时提醒任务(每 30 秒执行一次)
|
||||
_scheduler.add_job(
|
||||
check_unreplied_sessions,
|
||||
'interval',
|
||||
seconds=30,
|
||||
id='check_unreplied_sessions',
|
||||
name='检查超时未回复会话',
|
||||
replace_existing=True,
|
||||
)
|
||||
|
||||
_scheduler.start()
|
||||
logger.info("✅ 超时提醒定时任务已启动(每 30 秒执行一次)")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"启动定时任务调度器失败: {e}")
|
||||
# 定时任务启动失败不阻塞应用启动
|
||||
|
||||
|
||||
def _stop_scheduler():
|
||||
"""停止定时任务调度器。
|
||||
|
||||
在应用关闭时调用,确保定时任务正确关闭。
|
||||
"""
|
||||
global _scheduler
|
||||
|
||||
if _scheduler is None:
|
||||
return
|
||||
|
||||
try:
|
||||
_scheduler.shutdown(wait=False)
|
||||
_scheduler = None
|
||||
logger.info("✅ 超时提醒定时任务已停止")
|
||||
except Exception as e:
|
||||
logger.error(f"停止定时任务调度器失败: {e}")
|
||||
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# 配置校验(启动时检查关键配置项是否为占位符)
|
||||
# --------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user