From 634c9634f6f04f55e15624cf68b13a2790e7107a Mon Sep 17 00:00:00 2001 From: soehi <811446351@qq.com> Date: Fri, 4 Dec 2026 14:31:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=9B=E8=81=98=E5=85=AC=E5=91=8A=E5=90=8C?= =?UTF-8?q?=E6=AD=A5=E7=BB=95=E8=BF=87=E7=B3=BB=E7=BB=9F=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E5=B9=B6=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/recruit.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) 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"]):