|
|
@@ -29,6 +29,33 @@ from service.online_request.module_online_data import OnlineDataRequest
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
|
|
from functools import partial
|
|
|
from service.online_request.module_online_data import AIGCDataRequest
|
|
|
+import asyncio
|
|
|
+from fastapi import BackgroundTasks
|
|
|
+import functools
|
|
|
+import traceback
|
|
|
+
|
|
|
+def log_exception_with_context(context_message=""):
|
|
|
+ """装饰器:为函数添加异常日志上下文"""
|
|
|
+
|
|
|
+ def decorator(func):
|
|
|
+ @functools.wraps(func)
|
|
|
+ def wrapper(*args, **kwargs):
|
|
|
+ try:
|
|
|
+ return func(*args, **kwargs)
|
|
|
+ except Exception as e:
|
|
|
+ logger.error(f"=== 异常发生在函数: {func.__name__} ===")
|
|
|
+ if context_message:
|
|
|
+ logger.error(f"上下文信息: {context_message}")
|
|
|
+ logger.error(f"函数参数: args={args}, kwargs={kwargs}")
|
|
|
+ logger.error(f"异常类型: {type(e).__name__}")
|
|
|
+ logger.error(f"异常信息: {str(e)}")
|
|
|
+ logger.error("完整堆栈跟踪:")
|
|
|
+ logger.error(traceback.format_exc())
|
|
|
+ raise # 重新抛出异常
|
|
|
+
|
|
|
+ return wrapper
|
|
|
+
|
|
|
+ return decorator
|
|
|
|
|
|
|
|
|
def parserGoodsDict2Aigc(return_data_check_before_detail):
|
|
|
@@ -39,13 +66,13 @@ def parserGoodsDict2Aigc(return_data_check_before_detail):
|
|
|
return goods_no_dict
|
|
|
|
|
|
|
|
|
-async def sendAsyncMessage(msg="", goods_arts=[], status="",msg_type=""):
|
|
|
+async def sendAsyncMessage(msg="", goods_arts=[], status="",msg_type="",data=None):
|
|
|
"""异步发送消息"""
|
|
|
data = {
|
|
|
"code": 0,
|
|
|
"msg": msg,
|
|
|
"status": 2,
|
|
|
- "data": {
|
|
|
+ "data": data if data is not None else {
|
|
|
"status": status,
|
|
|
"goods_art_nos": goods_arts,
|
|
|
},
|
|
|
@@ -222,7 +249,7 @@ async def fromExcelHandler(params: HandlerDetail):
|
|
|
temp_class_dict[key] = cls
|
|
|
|
|
|
config_data["temp_class"] = temp_class_dict
|
|
|
- executor = ThreadPoolExecutor(max_workers=4)
|
|
|
+ # executor = ThreadPoolExecutor(max_workers=4)
|
|
|
# 此处对抠图进行批量处理,保证所有的图片在生成详情图之前已经完成抠图,以保证详情图生成的效率
|
|
|
return_data = run_main.check_before_cutout(config_data)
|
|
|
cutout_res = run_main.check_for_cutout_image_first_call_back(return_data)
|
|
|
@@ -230,7 +257,7 @@ async def fromExcelHandler(params: HandlerDetail):
|
|
|
try:
|
|
|
if cutout_res:
|
|
|
return_data_check_before_detail = run_main.check_before_detail(config_data)
|
|
|
- check_for_detail_first_res = run_main.check_for_detail_first_call_back(
|
|
|
+ check_for_detail_first_res = await run_main.check_for_detail_first_call_back(
|
|
|
return_data_check_before_detail
|
|
|
)
|
|
|
if isinstance(check_for_detail_first_res, partial):
|
|
|
@@ -295,11 +322,13 @@ async def fromExcelHandler(params: HandlerDetail):
|
|
|
{"goods_art_no": good_art, "success": False, "info": str(e)}
|
|
|
)
|
|
|
handler_result_folder = "/".join(handler_result_folder.split("/")[:-1])
|
|
|
- return {
|
|
|
- "code": 0,
|
|
|
- "msg": "",
|
|
|
- "data": {"output_folder": handler_result_folder, "list": handler_result},
|
|
|
- }
|
|
|
+ await sendAsyncMessage(
|
|
|
+ msg="处理结束",
|
|
|
+ data={"output_folder": handler_result_folder, "list": handler_result},
|
|
|
+ status="处理结束",
|
|
|
+ msg_type="detail_progress",
|
|
|
+ )
|
|
|
+ return
|
|
|
|
|
|
|
|
|
def group_by_style_number(data):
|
|
|
@@ -315,7 +344,15 @@ def group_by_style_number(data):
|
|
|
|
|
|
|
|
|
@app.post("/handle_detail")
|
|
|
-async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
+async def handle_detail_background(
|
|
|
+ request: Request, params: HandlerDetail, background_tasks: BackgroundTasks
|
|
|
+):
|
|
|
+ # background_tasks.add_task(process_handle_detail, request, params)
|
|
|
+ asyncio.create_task(process_handle_detail(request, params))
|
|
|
+ return {"code": 0, "msg": "任务已提交后台处理", "data": {"status": "processing"}}
|
|
|
+
|
|
|
+
|
|
|
+async def process_handle_detail(request: Request, params: HandlerDetail):
|
|
|
obj = None
|
|
|
token = "Bearer " + params.token
|
|
|
uuid = params.uuid
|
|
|
@@ -446,11 +483,18 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
}
|
|
|
)
|
|
|
if is_only_cutout == 1:
|
|
|
- return {
|
|
|
- "code": 0,
|
|
|
- "msg": "",
|
|
|
- "data": {"output_folder": handler_result_folder, "list": handler_result},
|
|
|
- }
|
|
|
+ # return {
|
|
|
+ # "code": 0,
|
|
|
+ # "msg": "",
|
|
|
+ # "data": {"output_folder": handler_result_folder, "list": handler_result},
|
|
|
+ # }
|
|
|
+ await sendAsyncMessage(
|
|
|
+ msg="处理结束",
|
|
|
+ data={"output_folder": handler_result_folder, "list": handler_result},
|
|
|
+ status="处理结束",
|
|
|
+ msg_type="detail_progress",
|
|
|
+ )
|
|
|
+ return
|
|
|
if is_product_scene == 1:
|
|
|
if product_scene_prompt == "" or product_scene_prompt == None:
|
|
|
raise UnicornException("请填写场景描述")
|
|
|
@@ -465,12 +509,13 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
raise UnicornException("请选择女模特")
|
|
|
try:
|
|
|
return_data_check_before_detail = run_main.check_before_detail(config_data)
|
|
|
+ print("报错前返回的结果数据", return_data_check_before_detail)
|
|
|
if is_product_scene == 1:
|
|
|
goods_dict = parserGoodsDict2Aigc(return_data_check_before_detail)
|
|
|
new_goods_dict = {}
|
|
|
await sendAsyncMessage(
|
|
|
msg="开始处理场景图",
|
|
|
- goods_arts=list(goods_dict.keys()),
|
|
|
+ goods_arts=[goods_art_no for goods_art_no in goods_dict.keys()],
|
|
|
status="开始处理",
|
|
|
msg_type="scene_progress",
|
|
|
)
|
|
|
@@ -487,7 +532,7 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
continue
|
|
|
aigc_clazz.center_paste_image(ceshi_image_path, save_image_path)
|
|
|
try:
|
|
|
- image_path = aigc_clazz.generateProductScene(
|
|
|
+ image_path = await aigc_clazz.generateProductScene(
|
|
|
save_image_path, product_scene_prompt, save_image_path
|
|
|
)
|
|
|
goods_art_dict_info["场景图"] = image_path
|
|
|
@@ -545,7 +590,7 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
continue
|
|
|
shutil.copy(ceshi_image_path, save_image_path)
|
|
|
try:
|
|
|
- image_path = aigc_clazz.generateUpperShoes(
|
|
|
+ image_path = await aigc_clazz.generateUpperShoes(
|
|
|
save_image_path, model_id, save_image_path
|
|
|
)
|
|
|
goods_art_dict_info["模特图"] = image_path
|
|
|
@@ -580,14 +625,21 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
)
|
|
|
return_data_check_before_detail["data"]["goods_no_dict"] = new_goods_dict
|
|
|
if is_detail == 0:
|
|
|
- return {
|
|
|
- "code": 0,
|
|
|
- "msg": "",
|
|
|
- "data": {
|
|
|
- "output_folder": handler_result_folder,
|
|
|
- "list": handler_result,
|
|
|
- },
|
|
|
- }
|
|
|
+ # return {
|
|
|
+ # "code": 0,
|
|
|
+ # "msg": "",
|
|
|
+ # "data": {
|
|
|
+ # "output_folder": handler_result_folder,
|
|
|
+ # "list": handler_result,
|
|
|
+ # },
|
|
|
+ # }
|
|
|
+ await sendAsyncMessage(
|
|
|
+ msg="处理结束",
|
|
|
+ data={"output_folder": handler_result_folder, "list": handler_result},
|
|
|
+ status="处理结束",
|
|
|
+ msg_type="detail_progress",
|
|
|
+ )
|
|
|
+ return
|
|
|
check_for_detail_first_res = await run_main.check_for_detail_first_call_back(
|
|
|
return_data_check_before_detail
|
|
|
)
|
|
|
@@ -649,9 +701,12 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
status="开始上传商品数据",
|
|
|
msg_type="upload_goods_progress",
|
|
|
)
|
|
|
- await onlineData.uploadGoods2ThirdParty(
|
|
|
- result_goods_no_dict, online_stores=online_stores
|
|
|
- )
|
|
|
+ try:
|
|
|
+ await onlineData.uploadGoods2ThirdParty(
|
|
|
+ result_goods_no_dict, online_stores=online_stores
|
|
|
+ )
|
|
|
+ except Exception as e:
|
|
|
+ print(f'上传任务出现错误:{e}')
|
|
|
await sendAsyncMessage(
|
|
|
msg="商品上传第三方成功",
|
|
|
goods_arts=[],
|
|
|
@@ -671,11 +726,18 @@ async def handle_detail(request: Request, params: HandlerDetail):
|
|
|
print(f"详情图生成错误信息:{e}")
|
|
|
handler_result_folder = ""
|
|
|
handler_result.append({"goods_art_no": "", "success": False, "info": str(e)})
|
|
|
- return {
|
|
|
- "code": 0,
|
|
|
- "msg": "",
|
|
|
- "data": {"output_folder": handler_result_folder, "list": handler_result},
|
|
|
- }
|
|
|
+ # return {
|
|
|
+ # "code": 0,
|
|
|
+ # "msg": "",
|
|
|
+ # "data": {"output_folder": handler_result_folder, "list": handler_result},
|
|
|
+ # }
|
|
|
+ await sendAsyncMessage(
|
|
|
+ msg="处理结束",
|
|
|
+ data={"output_folder": handler_result_folder, "list": handler_result},
|
|
|
+ status="处理结束",
|
|
|
+ msg_type="detail_progress",
|
|
|
+ )
|
|
|
+ return
|
|
|
|
|
|
|
|
|
@app.get("/get_device_tabs", description="获取可执行程序命令列表")
|