2026-03-18 17:18:23 +08:00
|
|
|
|
"""
|
|
|
|
|
|
Celery 应用配置
|
|
|
|
|
|
配置从 app.core.config.settings 读取。
|
|
|
|
|
|
Worker 启动时需聚合注册所有 feature 的 model,否则 User 等 relationship("Order", ...) 解析时会报找不到 Order。
|
|
|
|
|
|
"""
|
2026-03-19 14:36:14 +08:00
|
|
|
|
|
2026-03-18 17:18:23 +08:00
|
|
|
|
from celery import Celery
|
|
|
|
|
|
|
|
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
|
|
|
|
|
|
# 与 main.py / Alembic 一致:注册所有 model,避免 mapper 初始化时 relationship 字符串找不到类
|
|
|
|
|
|
from app.features.auth import models as _auth_models # noqa: F401
|
|
|
|
|
|
from app.features.conversation import models as _conv_models # noqa: F401
|
|
|
|
|
|
from app.features.memory import models as _memory_models # noqa: F401
|
|
|
|
|
|
from app.features.memoir import models as _memoir_models # noqa: F401
|
|
|
|
|
|
from app.features.payment import models as _payment_models # noqa: F401
|
|
|
|
|
|
from app.features.user import models as _user_models # noqa: F401
|
|
|
|
|
|
|
|
|
|
|
|
REDIS_URL = settings.redis_url
|
|
|
|
|
|
|
|
|
|
|
|
# 创建 Celery 应用
|
|
|
|
|
|
celery_app = Celery(
|
|
|
|
|
|
"life_echo",
|
|
|
|
|
|
broker=REDIS_URL,
|
|
|
|
|
|
backend=REDIS_URL,
|
|
|
|
|
|
include=["app.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": "app.tasks.cleanup.cleanup_sessions",
|
|
|
|
|
|
# "schedule": 3600.0,
|
|
|
|
|
|
# },
|
|
|
|
|
|
}
|