Files
life-echo/api/tasks/celery_app.py

59 lines
1.4 KiB
Python
Raw Normal View History

"""
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,
# },
}