Skip to content

Commit 3fe61b0

Browse files
committed
v2.3.10a
1 parent e0e6110 commit 3fe61b0

2 files changed

Lines changed: 30 additions & 9 deletions

File tree

backend/app/main.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
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
4244
from 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

backend/app/tasks/broker.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@
88
redis_url="redis://localhost:6379/3",
99
result_ex_time=86400,
1010
)
11-
12-
broker: AioPikaBroker = (
11+
broker = (
1312
AioPikaBroker(
14-
broker_url="amqp://guest:guest@localhost:5672/",
13+
"amqp://guest:guest@localhost:5672/",
14+
)
15+
.with_result_backend(
16+
RedisAsyncResultBackend(
17+
"redis://localhost:6379/3", result_ex_time=86400
18+
)
1519
)
16-
.with_result_backend(result_backend=result_backend)
1720
.with_middlewares(
1821
SmartRetryMiddleware(
1922
default_retry_count=5,

0 commit comments

Comments
 (0)