3D 打板部署
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
All checks were successful
git commit AiDA python develop 分支构建部署 / scheduled_deploy (push) Has been skipped
This commit is contained in:
35
app/service/sketch2garment/callback.py
Normal file
35
app/service/sketch2garment/callback.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import logging
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger("app")
|
||||
|
||||
|
||||
async def notify_callback(callback_url: str, task_id: str, status: str, result: dict, ):
|
||||
"""
|
||||
调用客户端提供的回调接口
|
||||
"""
|
||||
try:
|
||||
payload = {
|
||||
"task_id": task_id,
|
||||
"status": status,
|
||||
"result": result
|
||||
}
|
||||
logger.info(payload)
|
||||
async with httpx.AsyncClient(timeout=30.0) as client:
|
||||
resp = await client.post(
|
||||
str(callback_url),
|
||||
json=payload,
|
||||
headers={"Content-Type": "application/json"}
|
||||
)
|
||||
|
||||
if 200 <= resp.status_code < 300:
|
||||
logger.info(f"回调成功 | task_id: {task_id} | status: {status} | url: {callback_url}")
|
||||
return True
|
||||
else:
|
||||
logger.warning(f"回调返回非2xx状态码 | task_id: {task_id} | status: {resp.status_code} | url: {callback_url}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"回调失败 | task_id: {task_id} | url: {callback_url} | error: {e}", exc_info=True)
|
||||
return False
|
||||
46
app/service/sketch2garment/celery_app.py
Normal file
46
app/service/sketch2garment/celery_app.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from celery import Celery
|
||||
from kombu import Queue, Exchange
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
celery_app = Celery(
|
||||
"sketch_to_garment",
|
||||
broker=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/2",
|
||||
backend=f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/{settings.REDIS_DB}",
|
||||
include=["app.service.sketch2garment.tasks"]
|
||||
)
|
||||
print(f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/3")
|
||||
print(f"celery_app: {celery_app}")
|
||||
|
||||
celery_app.conf.update(
|
||||
task_serializer="json",
|
||||
accept_content=["json"],
|
||||
result_serializer="json",
|
||||
timezone="Asia/Hong_Kong",
|
||||
enable_utc=True,
|
||||
task_track_started=True,
|
||||
task_time_limit=300, # 单个任务最长 5 分钟
|
||||
task_soft_time_limit=280,
|
||||
# 定义队列
|
||||
task_queues=(
|
||||
Queue("sketch_to_garment_queue",
|
||||
exchange=Exchange("sketch_to_garment_exchange", type="direct"),
|
||||
durable=True),
|
||||
|
||||
),
|
||||
|
||||
task_routes={
|
||||
'app.service.sketch2garment.tasks.sketch_to_garment':
|
||||
{
|
||||
'queue': 'sketch_to_garment_queue',
|
||||
'exchange': 'sketch_to_garment_exchange', # ← 修改这里
|
||||
},
|
||||
},
|
||||
task_default_queue="sketch_to_garment_queue",
|
||||
|
||||
worker_concurrency=1,
|
||||
worker_prefetch_multiplier=1,
|
||||
worker_max_tasks_per_child=1,
|
||||
task_acks_late=True,
|
||||
task_reject_on_worker_lost=True,
|
||||
)
|
||||
44
app/service/sketch2garment/server.py
Normal file
44
app/service/sketch2garment/server.py
Normal file
@@ -0,0 +1,44 @@
|
||||
import logging
|
||||
|
||||
from app.service.sketch2garment.tasks import sketch_to_garment
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def submit_sketch_to_garment_task(model: str = "single", task_id: str = "", callback_url: str = "", bucket_name: str = "test", user_id: str = "123", input_image_path: str = ""):
|
||||
"""提交 img_to_3D 任务(带队列长度限制)"""
|
||||
queue_name = "img_to_3d_queue"
|
||||
max_queue_length = 10
|
||||
|
||||
try:
|
||||
# current_length = get_queue_length(queue_name)
|
||||
|
||||
# if current_length >= max_queue_length:
|
||||
# return {
|
||||
# "state": "queue_full",
|
||||
# "message": "当前 3D 生成请求较多,请稍后重试。",
|
||||
# "queue_length": current_length,
|
||||
# "max_length": max_queue_length
|
||||
# }
|
||||
|
||||
# 提交任务
|
||||
task = sketch_to_garment.apply_async(
|
||||
args=(task_id, callback_url, bucket_name, input_image_path, user_id, model),
|
||||
task_id=task_id,
|
||||
queue="sketch_to_garment_queue")
|
||||
|
||||
# logger.info(f"img_to_3d_task 已提交 | task_id: {task_id} | 当前队列长度: {current_length}")
|
||||
|
||||
return {
|
||||
"state": "success",
|
||||
"task_id": task_id,
|
||||
"message": "任务已成功提交,正在后台处理...",
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"提交 img_to_3d_task 失败: {e}", exc_info=True)
|
||||
return {
|
||||
"state": "fail",
|
||||
"message": "提交失败,请稍后重试。",
|
||||
"error": str(e)
|
||||
}
|
||||
57
app/service/sketch2garment/tasks.py
Normal file
57
app/service/sketch2garment/tasks.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
from app.core.config import settings
|
||||
from app.service.sketch2garment.callback import notify_callback
|
||||
import httpx
|
||||
|
||||
from app.service.sketch2garment.celery_app import celery_app
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@celery_app.task(bind=True, queue="sketch_to_garment_queue", max_retries=3, name='app.service.sketch2garment.tasks.sketch_to_garment')
|
||||
def sketch_to_garment(self, task_id: str, callback_url: str, bucket_name: str, input_image_path: str, user_id: str, category: str = None):
|
||||
payload = {
|
||||
"bucket_name": bucket_name,
|
||||
"category": category or settings.DEFAULT_CATEGORY,
|
||||
"input_image_path": input_image_path,
|
||||
"user_id": user_id
|
||||
}
|
||||
logger.info(f"payload: {payload}")
|
||||
|
||||
try:
|
||||
with httpx.Client(timeout=300.0) as client: # 注意这里用 AsyncClient 配合 Celery
|
||||
# 如果你的 LitServe 是同步 endpoint,也可以用 httpx.Client()
|
||||
response = client.post(settings.SKETCH_TO_GARMENT_URL, json=payload)
|
||||
if response.status_code == 200:
|
||||
result = response.json()
|
||||
result_json = {
|
||||
"pattern": result[1],
|
||||
"texture": result[2],
|
||||
"glb": result[3],
|
||||
"texture_fabric": result[4]
|
||||
}
|
||||
asyncio.run(
|
||||
notify_callback(callback_url=callback_url, task_id=task_id, result=result_json, status="success")
|
||||
)
|
||||
else:
|
||||
asyncio.run(
|
||||
notify_callback(
|
||||
callback_url=callback_url,
|
||||
task_id=task_id,
|
||||
result={
|
||||
"status": "fail",
|
||||
"task_id": task_id,
|
||||
"message": "fail",
|
||||
"error": "fail"
|
||||
},
|
||||
status="fail")
|
||||
)
|
||||
except Exception as e:
|
||||
return {
|
||||
"status": "failed",
|
||||
"task_id": task_id,
|
||||
"input": payload,
|
||||
"error": str(e)
|
||||
}
|
||||
Reference in New Issue
Block a user