import httpx from typing import Optional from app.core.logging import get_logger logger = get_logger(__name__) class RuntimeClient: """HTTP client for communicating with runtime service""" def __init__(self, base_url: str = "http://deepdata-runtime:8081"): self.base_url = base_url self.client = httpx.AsyncClient(timeout=60.0) async def submit_task(self, task_id: str, retry_count: int = 0) -> bool: """Submit cleaning task to runtime executor""" try: url = f"{self.base_url}/api/task/{task_id}/submit" response = await self.client.post(url, json={"retry_count": retry_count}) response.raise_for_status() logger.info(f"Task {task_id} submitted successfully with retry_count={retry_count}") return True except httpx.HTTPError as e: logger.error(f"Failed to submit task {task_id}: {e}") return False except Exception as e: logger.error(f"Unexpected error submitting task {task_id}: {e}") return False async def stop_task(self, task_id: str) -> bool: """Stop running cleaning task""" try: url = f"{self.base_url}/api/task/{task_id}/stop" response = await self.client.post(url) response.raise_for_status() logger.info(f"Task {task_id} stopped successfully") return True except httpx.HTTPError as e: logger.error(f"Failed to stop task {task_id}: {e}") return False except Exception as e: logger.error(f"Unexpected error stopping task {task_id}: {e}") return False ''' async def get_task_status(self, task_id: str) -> Optional[dict]: """Get task status from runtime""" try: url = f"{self.base_url}/api/task/{task_id}/status" response = await self.client.get(url) response.raise_for_status() return response.json() except httpx.HTTPError as e: logger.error(f"Failed to get task status {task_id}: {e}") return None except Exception as e: logger.error(f"Unexpected error getting task status {task_id}: {e}") return None ''' async def get_task_status(self, task_id: str) -> Optional[dict]: """Get task status from runtime""" url = f"{self.base_url}/api/task/{task_id}/status" try: logger.info(f"[RuntimeClient] Requesting status for task {task_id} from {url}") response = await self.client.get(url) # [调试] 打印 HTTP 状态码 logger.info(f"[RuntimeClient] Response status: {response.status_code}") # 尝试读取响应体,无论状态码是多少 try: resp_json = response.json() logger.info(f"[RuntimeClient] Response body: {resp_json}") # 如果 HTTP 状态码不是 2xx,我们也打印出来,但不抛异常(视具体业务逻辑而定) # 这里保持原逻辑:raise_for_status response.raise_for_status() return resp_json except httpx.HTTPError as e: logger.error(f"[RuntimeClient] HTTP Error: {e}") logger.debug(f"[RuntimeClient] Response text: {response.text}") return None except json.JSONDecodeError as e: logger.error(f"[RuntimeClient] JSON Decode Error: {e}") logger.debug(f"[RuntimeClient] Raw response: {response.text}") return None except httpx.ConnectError as e: # [调试] 区分连接超时和连接被拒绝 logger.error(f"[RuntimeClient] Connect Error: {e}") return None except httpx.TimeoutException as e: # [调试] 明确记录超时 logger.error(f"[RuntimeClient] Timeout waiting for status of {task_id}: {e}") return None except Exception as e: logger.error(f"[RuntimeClient] Unexpected error: {e}") return None async def close(self): """Close HTTP client""" await self.client.aclose()