diff --git a/browser.py b/browser.py index d6ddeb6..796f114 100644 --- a/browser.py +++ b/browser.py @@ -3,6 +3,7 @@ import json import os import subprocess +import time from config import AGENT_BROWSER, NODE_BIN_DIR, XDG_RUNTIME_DIR @@ -43,8 +44,27 @@ class AgentBrowser: pass return out - def open(self, url, timeout=60): - return self._run(['open', url], timeout=timeout, check=True) + # 瞬时网络故障(服务重启/端口切换等)会自动重试的错误特征 + RETRYABLE_ERRS = ( + 'ERR_EMPTY_RESPONSE', 'ERR_CONNECTION_REFUSED', + 'ERR_CONNECTION_RESET', 'ERR_CONNECTION_CLOSED', + 'ERR_TIMED_OUT', 'ERR_NAME_NOT_RESOLVED', 'ERR_SOCKET_NOT_CONNECTED', + 'ERR_ADDRESS_UNREACHABLE', 'ERR_NETWORK_CHANGED', 'ERR_INTERNET_DISCONNECTED', + ) + + def open(self, url, timeout=60, retries=2): + """打开页面;对瞬时连接类错误自动重试(默认最多重试 2 次,间隔 2s/4s)""" + last_err = None + for attempt in range(retries + 1): + try: + return self._run(['open', url], timeout=timeout, check=True) + except BrowserError as e: + last_err = e + if attempt < retries and any(t in str(e) for t in self.RETRYABLE_ERRS): + time.sleep(2 * (attempt + 1)) + continue + raise + raise last_err def snapshot(self, interactive=True, compact=False, depth=None, timeout=60): args = ['snapshot']