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,