feat: 添加Redis支持和Celery任务处理
- 新增Redis服务模块用于会话状态存储和缓存 - 集成Celery用于后台任务处理 - 更新Docker Compose配置以支持开发环境 - 优化API以支持异步调用和Redis会话存储 - 更新文档以反映新的开发环境配置和使用方法
This commit is contained in:
58
api/tasks/celery_app.py
Normal file
58
api/tasks/celery_app.py
Normal file
@@ -0,0 +1,58 @@
|
||||
"""
|
||||
Celery 应用配置
|
||||
"""
|
||||
import os
|
||||
from celery import Celery
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
|
||||
# Redis URL
|
||||
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")
|
||||
|
||||
# 创建 Celery 应用
|
||||
celery_app = Celery(
|
||||
"life_echo",
|
||||
broker=REDIS_URL,
|
||||
backend=REDIS_URL,
|
||||
include=["tasks.memoir_tasks"]
|
||||
)
|
||||
|
||||
# Celery 配置
|
||||
celery_app.conf.update(
|
||||
# 任务序列化
|
||||
task_serializer="json",
|
||||
accept_content=["json"],
|
||||
result_serializer="json",
|
||||
|
||||
# 时区
|
||||
timezone="UTC",
|
||||
enable_utc=True,
|
||||
|
||||
# 任务结果过期时间(1小时)
|
||||
result_expires=3600,
|
||||
|
||||
# 任务执行设置
|
||||
task_soft_time_limit=300, # 5分钟软超时
|
||||
task_time_limit=600, # 10分钟硬超时
|
||||
|
||||
# 并发设置
|
||||
worker_prefetch_multiplier=1, # 每次只预取一个任务
|
||||
worker_concurrency=4, # 并发 worker 数量
|
||||
|
||||
# 任务重试设置
|
||||
task_acks_late=True, # 任务完成后再确认
|
||||
task_reject_on_worker_lost=True, # worker 丢失时拒绝任务
|
||||
|
||||
# 不设置自定义队列路由,使用 Celery 默认队列
|
||||
)
|
||||
|
||||
# 定时任务配置(如果需要)
|
||||
celery_app.conf.beat_schedule = {
|
||||
# 示例:每小时清理过期会话
|
||||
# "cleanup-expired-sessions": {
|
||||
# "task": "tasks.cleanup.cleanup_sessions",
|
||||
# "schedule": 3600.0,
|
||||
# },
|
||||
}
|
||||
Reference in New Issue
Block a user