Explorar o código

refactor(decode): remove test script and enhance error handling in response structure

- Deleted the test script for the decode task.
- Updated logging to accurately reflect quota usage.
- Improved response structure to include error messages for failed tasks.
jihuaqiang hai 1 día
pai
achega
83836261d9
Modificáronse 3 ficheiros con 31 adicións e 14 borrados
  1. 3 3
      tasks/decode.py
  2. 28 10
      tasks/detail.py
  3. 0 1
      test.py

+ 3 - 3
tasks/decode.py

@@ -131,7 +131,7 @@ def _check_quota(scene: SceneEnum, capability: CapabilityEnum = CapabilityEnum.D
                     
                     # 提交事务
                     conn.commit()
-                    logger.info(f"配额检查通过,scene={scene_value}, capability={capability_value}, used={actual_used_count + 1}, limit={quota_limit}")
+                    logger.info(f"配额检查通过,scene={scene_value}, capability={capability_value}, used={actual_used_count}, limit={quota_limit}")
                     return True
                     
             except Exception as e:
@@ -205,8 +205,8 @@ def begin_decode_task(param: DecodeContentParam) -> Dict[str, Any]:
 def _trigger_topic_decode_workflow(task_id: str) -> Dict[str, Any]:
     """发起解构任务(调用上游工作流服务)"""
     try:
-        # url = "http://192.168.81.96:8000/workflow/topic/decode"
-        url = "http://supply-content-deconstruction-workflow.piaoquantv.com/workflow/topic/decode"
+        url = "http://192.168.81.96:8000/workflow/topic/decode"
+        # url = "http://supply-content-deconstruction-workflow.piaoquantv.com/workflow/topic/decode"
         params = {"taskId": task_id}
 
         resp = requests.get(url, params=params, timeout=10)

+ 28 - 10
tasks/detail.py

@@ -13,19 +13,17 @@ STATUS_RUNNING = 1  # 执行中
 STATUS_SUCCESS = 2  # 成功
 STATUS_FAILED = 3   # 失败
 
-# 不需要查询结果的状态
-STATUS_WITHOUT_RESULT = {STATUS_PENDING, STATUS_RUNNING, STATUS_FAILED}
+# 不需要查询结果的状态(失败状态需要查询 error_message,因此不在此集合中)
+STATUS_WITHOUT_RESULT = {STATUS_PENDING, STATUS_RUNNING}
 
 
-def _build_response(data: Dict[str, Any], reason: Optional[str] = None) -> Dict[str, Any]:
+def _build_response(data: Dict[str, Any]) -> Dict[str, Any]:
     """构建统一响应格式"""
     response = {
         "code": 0,
         "msg": "ok",
         "data": data
     }
-    if reason:
-        response["reason"] = reason
     return response
 
 
@@ -54,12 +52,13 @@ def _fetch_decode_result(task_id: str) -> Optional[Dict[str, Any]]:
     }
 
 
-def _build_result_data(task_id: str, status: int, result: Any = None) -> Dict[str, Any]:
+def _build_result_data(task_id: str, status: int, result: Any = None, reason: Optional[str] = None) -> Dict[str, Any]:
     """构建结果数据"""
     return {
         "taskId": task_id,
         "status": status,
-        "result": result
+        "result": result,
+        "reason": reason
     }
 
 
@@ -77,12 +76,12 @@ def _handle_success_status(task_id: str, capability: int) -> Dict[str, Any]:
     result_data = _build_result_data(
         task_id=task_id,
         status=STATUS_SUCCESS,
-        result=decode_result.get("result")
+        result=decode_result.get("result"),
+        reason=decode_result.get("error_message")
     )
     
     return _build_response(
-        data=result_data,
-        reason=decode_result.get("error_message")
+        result_data
     )
 
 
@@ -109,6 +108,25 @@ def get_decode_detail_by_task_id(task_id: str) -> Optional[Dict[str, Any]]:
     if status == STATUS_SUCCESS:
         return _handle_success_status(task_id_value, capability)
     
+    # 失败状态,需要返回 error_message 到 reason 字段,result 固定为 "[]"
+    if status == STATUS_FAILED:
+        error_message: Optional[str] = None
+        # 仅对解构任务从结果表中查询失败原因
+        if capability == CapabilityEnum.DECODE.value:
+            decode_result = _fetch_decode_result(task_id_value)
+            if decode_result:
+                error_message = decode_result.get("error_message")
+        
+        result_data = _build_result_data(
+            task_id=task_id_value,
+            status=STATUS_FAILED,
+            result="[]",
+            reason=error_message or ""
+        )
+        return _build_response(
+            result_data
+        )
+    
     # 其他未知状态,返回基础数据
     result_data = _build_result_data(task_id_value, status)
     return _build_response(result_data)

+ 0 - 1
test.py

@@ -1 +0,0 @@
-curl -X POST "http://localhost:8000/api/v1/content/tasks/decode" -H "Content-Type: application/json" -d '{"scene":0,"content_type":3,"content":{"channel_content_id":"content_123456","video_url":"http://weapppiccdn.yishihui.com/rosup/decode2/58840748.mp4","images":[],"body_text":"","title":"测试标题","channel_account_id":"account_001","channel_account_name":"测试账号"}}'