Files
geMoldInsight/src/celery_tasks.py
T

48 lines
1.8 KiB
Python
Raw Normal View History

from celery_app import app
2026-05-29 18:10:08 +08:00
from moldinsight.services.processing_service import processing_service
2026-07-13 17:44:50 +08:00
from shared.config.settings import settings
2026-05-29 18:10:08 +08:00
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, file_path: str, stp_file_id: int,
process_params: dict):
"""Celery 任务:异步处理 STP 文件生成模具型腔"""
import asyncio
2026-07-13 17:44:50 +08:00
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
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,
)
await processing_service.process_file_with_storage(
task_id, file_path, stp_file_id, process_params
)
try:
logger.info(f"[celery] 开始处理: {task_id}")
2026-07-13 17:44:50 +08:00
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