瀏覽代碼

新增调用状态接口

袁威 8 小時之前
父節點
當前提交
7ccc044762
共有 2 個文件被更改,包括 32 次插入1 次删除
  1. 31 1
      app/api/auto_import.py
  2. 1 0
      app/core/config.py

+ 31 - 1
app/api/auto_import.py

@@ -203,6 +203,32 @@ async def _update_card_scores(card_id: int) -> None:
         logger.error("更新卡牌分数失败 card_id=%s error=%s", card_id, e, exc_info=True)
 
 
+async def _notify_rating_progress(cardNo: Optional[str], headers: Optional[Dict[str, str]] = None) -> bool:
+    if not cardNo:
+        logger.info("Skip rating progress notify because cardNo is empty")
+        return False
+
+    url = (settings.RATING_SAVE_PROGRESS or "").strip()
+    if not url:
+        logger.info("Skip rating progress notify because RATING_SAVE_PROGRESS is empty")
+        return False
+
+    notify_headers = {}
+    if headers and headers.get("Authorization"):
+        notify_headers["Authorization"] = headers["Authorization"]
+
+    timeout = aiohttp.ClientTimeout(total=3, connect=1, sock_connect=1, sock_read=2)
+    try:
+        async with aiohttp.ClientSession(timeout=timeout) as session:
+            async with session.post(url, json={"cardNo": cardNo}, headers=notify_headers or None) as resp:
+                resp_text = await resp.text()
+                logger.info("Save rating progress: status=%s body=%s", resp.status, resp_text[:500])
+                return resp.status == 200
+    except Exception as e:
+        logger.error("Save rating progress failed cardNo=%s error=%s", cardNo, e, exc_info=True)
+        return False
+
+
 def _resolve_internal_base_url(request: Request) -> str:
     """
     导入流程内部调用本服务时优先走集群内地址,避免经 ingress 再次鉴权导致 401。
@@ -899,7 +925,7 @@ async def auto_import_url_script_api(
         bytes_map[image_type] = (file_bytes, filename)
 
     try:
-        return await _run_import_flow(
+        result = await _run_import_flow(
             local_base_url=local_base_url,
             card_name=card_name,
             cardNo=cardNo,
@@ -910,6 +936,10 @@ async def auto_import_url_script_api(
             non_gray_to_main=True,
             forward_headers=forward_headers or None,
         )
+        progress_saved = await _notify_rating_progress(cardNo, forward_headers or None)
+        if not progress_saved:
+            result["message"] = "导入算分服务成功, 但记录进度失败"
+        return result
     except HTTPException:
         raise
     except Exception as e:

+ 1 - 0
app/core/config.py

@@ -42,6 +42,7 @@ class Settings(BaseSettings):
 
     # 分数计算接口url
     SCORE_UPDATE_SERVER_URL: str = "http://127.0.0.1:7754"
+    RATING_SAVE_PROGRESS: str = "http://order/api/rating/order/progress"
 
     # --- Fluentd 日志配置 ---
     FLUENTD_ENABLED: bool = False