diff --git a/server/recruit.py b/server/recruit.py index 4f599d9..20c99b6 100644 --- a/server/recruit.py +++ b/server/recruit.py @@ -212,18 +212,29 @@ def public_row(row) -> dict: async def fetch_page(client: httpx.AsyncClient, page: int, page_size: int = 50): - response = await client.get(LIST_ENDPOINT, params={"pageNum": page, "pageSize": page_size, "keyword": "", "timeRange": ""}) - response.raise_for_status() - payload = response.json() - if payload.get("code") != 0: - raise RuntimeError(payload.get("msg") or "招聘公告接口返回异常") - return payload + last_error: Exception | None = None + for attempt in range(2): + try: + response = await asyncio.wait_for( + client.get(LIST_ENDPOINT, params={"pageNum": page, "pageSize": page_size, "keyword": "", "timeRange": ""}), + timeout=15, + ) + response.raise_for_status() + payload = response.json() + if payload.get("code") != 0: + raise RuntimeError(payload.get("msg") or "招聘公告接口返回异常") + return payload + except (httpx.HTTPError, asyncio.TimeoutError, RuntimeError) as exc: + last_error = exc + if attempt == 0: + await asyncio.sleep(0.4) + raise RuntimeError("就业平台暂时无法访问") from last_error async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict: owns_client = client is None if owns_client: - client = httpx.AsyncClient(timeout=20) + client = httpx.AsyncClient(timeout=20, trust_env=False) added = updated = 0 try: first = await fetch_page(client, 1) @@ -269,7 +280,7 @@ async def sync_all(db, client: httpx.AsyncClient | None = None) -> dict: async def docx_bytes(rows, client: httpx.AsyncClient | None = None, cache_dir: Path | None = None) -> bytes: owns_client = client is None if owns_client: - client = httpx.AsyncClient(timeout=20, follow_redirects=True) + client = httpx.AsyncClient(timeout=20, follow_redirects=True, trust_env=False) sources = [] for row in rows: for kind, value in content_blocks(row["content_html"]):