Files
geMoldInsight/src/celery_tasks.py
T
2026-09-16 17:55:04 +08:00

51 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from celery_app import app
from moldinsight.services.processing_service import processing_service
from shared.config.settings import settings
from shared.utils.logger import get_logger
logger = get_logger(__name__)
@app.task(bind=True, max_retries=1, default_retry_delay=60)
def process_stp_task(self, task_id: str, stp_file_id: int, process_params: dict):
"""Celery 任务:异步处理 STP 文件生成模具型腔(源文件按 stp_file_id 从 RustFS 获取)"""
import asyncio
async def _run():
# Celery 进程不执行 FastAPI startup,必须在此处显式建立连接:
# - redis.asyncio 客户端绑定创建它的循环,而本任务经 asyncio.run 每次新建循环,
# 故每任务需 reconnect();
# - RustFS(Minio) 为同步客户端,不绑定循环,连一次后跨任务复用。
from shared.services.redis_task_manager import redis_task_manager
from moldinsight.storage.rustfs_storage import rustfs_manager
from shared.database.database import db_manager
await redis_task_manager.reconnect()
if not rustfs_manager.is_connected:
await rustfs_manager.connect(
endpoint=settings.RUSTFS_ENDPOINT,
access_key=settings.RUSTFS_ACCESS_KEY,
secret_key=settings.RUSTFS_SECRET_KEY,
timeout=settings.RUSTFS_TIMEOUT,
)
# Celery worker 使用独立连接池配置(较小池)
if not db_manager.is_connected:
await db_manager.connect(role="celery")
await processing_service.process_file_with_storage(
task_id, stp_file_id, process_params
)
try:
logger.info(f"[celery] 开始处理: {task_id}")
asyncio.run(_run())
logger.info(f"[celery] 处理完成: {task_id}")
return {"task_id": task_id, "status": "completed"}
except Exception as exc:
logger.error(f"[celery] 处理失败: {task_id}, error={exc}")
try:
self.retry(exc=exc)
except Exception:
pass
raise