""" Celery 应用配置 配置从 app.core.config.settings 读取。 Worker 启动时需聚合注册所有 feature 的 model,否则 User 等 relationship("Order", ...) 解析时会报找不到 Order。 与 main.py / Alembic 一致:下方 import 仅用于注册 ORM model。 """ from app.core.logging import setup_logging # 与 app.main 一致:先配置 loguru + InterceptHandler,再加载会打日志的依赖 setup_logging() from celery import Celery from app.core.config import settings from app.features.asset import models as _asset_models # noqa: F401 - register Asset 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.memoir import models as _memoir_models # noqa: F401 from app.features.memory import models as _memory_models # noqa: F401 from app.features.payment import models as _payment_models # noqa: F401 from app.features.story import models as _story_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", "app.tasks.story_image_tasks", "app.tasks.chapter_cover_tasks", "app.tasks.chapter_compose_tasks", "app.tasks.memory_compaction_tasks", ], ) # Celery 配置 celery_app.conf.update( # 不劫持根 logger,便于与 loguru + InterceptHandler 统一格式与等级 worker_hijack_root_logger=False, # 任务序列化 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 = { "memory-compaction-sweep": { "task": "app.tasks.memory_compaction_tasks.memory_compaction_sweep", "schedule": 6 * 3600.0, }, }