3838 users ,
3939 weread ,
4040)
41- from app .tasks import send_bootstrap_emails , send_feishu_message
41+ from app .tasks import send_bootstrap_emails
42+ from app .tasks .broker import broker
43+ from app .tasks .task import send_feishu_message
4244from app .utils .cache import close_cache_redis
4345
4446
@@ -62,6 +64,13 @@ async def lifespan(app: FastAPI):
6264 )
6365 app .state .redis , app .state .redis2 = await init_redis () # type: ignore
6466
67+ # 启动 Taskiq broker
68+ try :
69+ await broker .startup ()
70+ logger .info ("Taskiq broker started successfully" )
71+ except Exception as e :
72+ logger .warning (f"Taskiq broker failed to start: { e !s} " )
73+
6574 logger .debug (f"Settings:{ get_settings ().model_dump ()} " )
6675 logger .info ("FastAPI started successfully." )
6776
@@ -73,15 +82,24 @@ async def lifespan(app: FastAPI):
7382 name = bootstrap_key , value = "1" , ex = 600 , nx = True
7483 )
7584 if lock_acquired :
76- # 发送飞书消息
77- await send_feishu_message .kiq ()
78- await send_bootstrap_emails .kiq (admin_email = admin_email )
79- app_logger .info ("✅启动通知任务已添加到队列" )
85+ try :
86+ await send_feishu_message .kiq ()
87+ await send_bootstrap_emails .kiq (admin_email = admin_email )
88+ app_logger .info ("✅启动通知任务已添加到队列" )
89+ except Exception as e :
90+ logger .warning (f"Failed to queue bootstrap email: { e !s} " )
8091 else :
8192 app_logger .info ("✅引导邮件已发送" )
8293
8394 yield
8495
96+ # 关闭 Taskiq broker
97+ try :
98+ await broker .shutdown ()
99+ logger .info ("Taskiq broker shutdown successfully" )
100+ except Exception as e :
101+ logger .warning (f"Taskiq broker shutdown failed: { e !s} " )
102+
85103 # 应用关闭时的清理工作
86104 await cleanup_resources (app = app )
87105
0 commit comments