From 93148fe85bcb2e641b8333ccf6b177138f6fe9bd Mon Sep 17 00:00:00 2001 From: 16337 <1633794139@qq.com> Date: Tue, 31 Mar 2026 09:37:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=EF=BC=9A=E5=B7=A5=E5=8D=95?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=E5=A2=9E=E5=8A=A0=E9=87=8D=E8=AF=95=E6=9C=BA?= =?UTF-8?q?=E5=88=B6=EF=BC=88=E6=9C=80=E5=A4=9A2=E6=AC=A1=E9=87=8D?= =?UTF-8?q?=E8=AF=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit IoT ops 服务偶发 500 异常(auto_complete 后状态污染), 添加 3s/6s 间隔重试,避免暂时性故障导致工单丢失。 --- app/services/work_order_client.py | 51 ++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/app/services/work_order_client.py b/app/services/work_order_client.py index 67b18a7..4ef9297 100644 --- a/app/services/work_order_client.py +++ b/app/services/work_order_client.py @@ -132,28 +132,43 @@ class WorkOrderClient: body_json = json.dumps(body, ensure_ascii=False, separators=(",", ":")) - try: - headers = self._build_headers(body_json) - url = f"{self._base_url}/open-api/ops/security/order/create" - logger.info(f"创建工单请求: url={url}, body={body_json}") + max_retries = 2 + for attempt in range(max_retries + 1): + try: + headers = self._build_headers(body_json) + url = f"{self._base_url}/open-api/ops/security/order/create" + if attempt == 0: + logger.info(f"创建工单请求: url={url}, body={body_json}") - async with httpx.AsyncClient(timeout=self._timeout) as client: - resp = await client.post(url, content=body_json, headers=headers) - data = resp.json() + async with httpx.AsyncClient(timeout=self._timeout) as client: + resp = await client.post(url, content=body_json, headers=headers) + data = resp.json() - if data.get("code") != 0: - logger.error(f"创建工单失败: status={resp.status_code}, resp={data}, body={body_json}") + if data.get("code") != 0: + if attempt < max_retries: + import asyncio + wait = (attempt + 1) * 3 + logger.warning(f"创建工单失败(第{attempt+1}次), {wait}s后重试: code={data.get('code')}, msg={data.get('msg')}") + await asyncio.sleep(wait) + continue + logger.error(f"创建工单失败(已重试{max_retries}次): status={resp.status_code}, resp={data}, body={body_json}") + return None + + # API 返回 {"code":0, "data": 1234567890} — data 直接是 orderId + order_id = str(data.get("data", "")) + logger.info(f"工单已创建: orderId={order_id}, alarmId={alarm_id}" + (f" (第{attempt+1}次尝试)" if attempt > 0 else "")) + return order_id + + except Exception as e: + if attempt < max_retries: + import asyncio + wait = (attempt + 1) * 3 + logger.warning(f"创建工单异常(第{attempt+1}次), {wait}s后重试: {e}") + await asyncio.sleep(wait) + continue + logger.error(f"创建工单异常(已重试{max_retries}次): {e}") return None - # API 返回 {"code":0, "data": 1234567890} — data 直接是 orderId - order_id = str(data.get("data", "")) - logger.info(f"工单已创建: orderId={order_id}, alarmId={alarm_id}") - return order_id - - except Exception as e: - logger.error(f"创建工单异常: {e}") - return None - async def auto_complete_order( self, order_id: str,