Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(deploy): 生产口探活超时报出最后一次原始错误

longmeidao2d7b47f77840
diff --git a/scripts/deploy_windows_tray.py b/scripts/deploy_windows_tray.py
index 832542ea4..676eec92b 100644
--- a/scripts/deploy_windows_tray.py
+++ b/scripts/deploy_windows_tray.py
@@ -53,14 +53,17 @@ def digest(path: Path) -> str:
def serving_identity(*, timeout: float = 60.0,
- sleep: Callable[[float], None] = time.sleep) -> dict | None:
- """轮询生产 HTTPS 口的 `/healthz`,返回它报的版本号与构建提交;没回话返回 None。
+ sleep: Callable[[float], None] = time.sleep) -> dict:
+ """轮询生产 HTTPS 口的 `/healthz`,返回它报的版本号与构建提交。
+ 期限内没拿到 200 时返回 `{"error": 最后一次的原始错误}`:证书校验失败、连接被拒、
+ 非 200 状态码要原样交给调用方报告,不能只剩一句「没回话」。
地址和 CA 都取自 `build_service_specs()`,跟托盘自己探活用的是同一份规格。
HTTPS 的结论必须由项目 CA 严格校验得出,明文口的 200 不算数:那条只是跳转进程。
"""
spec = next(spec for spec in build_service_specs() if spec.name == "https")
deadline = time.monotonic() + timeout
+ last = "没有回话"
while True:
try:
response = httpx.get(spec.health_url, timeout=5.0, verify=spec.verify,
@@ -69,10 +72,11 @@ def serving_identity(*, timeout: float = 60.0,
payload = response.json()
return {"version": str(payload.get("version")),
"build_commit": payload.get("build_commit")}
- except (httpx.HTTPError, OSError, ValueError):
- pass
+ last = f"HTTP {response.status_code}"
+ except (httpx.HTTPError, OSError, ValueError) as error:
+ last = f"{type(error).__name__}: {error}"
if time.monotonic() >= deadline:
- return None
+ return {"error": f"{spec.health_url} {last}"}
sleep(1.0)
@@ -94,7 +98,7 @@ def deploy(
installer: WindowsUpdateInstaller | None = None,
restart: Callable[..., object] = restart_tray,
run: Callable[..., subprocess.CompletedProcess] = subprocess.run,
- identity: Callable[..., dict | None] = serving_identity,
+ identity: Callable[..., dict] = serving_identity,
) -> dict:
"""把 `root` 这份检出打成新托盘并换上 `target`,返回可直接打印的结果字典。
@@ -150,9 +154,11 @@ def deploy(
served = identity(timeout=timeout)
fields["expected_version"] = __version__
- if served is None:
+ if served is None or "error" in served:
+ detail = (served or {}).get("error")
return _outcome(False, "verify",
- "新托盘已就位,但生产 HTTPS 口的 /healthz 没在期限内回话。",
+ "新托盘已就位,但生产 HTTPS 口的 /healthz 没在期限内回话"
+ + (f";最后一次:{detail}" if detail else "") + "。",
**fields)
fields["version"] = served["version"]
fields["served_commit"] = served["build_commit"]
diff --git a/tests/test_deploy_windows_tray.py b/tests/test_deploy_windows_tray.py
index b59c0ad72..9f1abe497 100644
--- a/tests/test_deploy_windows_tray.py
+++ b/tests/test_deploy_windows_tray.py
@@ -144,6 +144,13 @@ class DeployWindowsTrayTests(unittest.TestCase):
self.assertEqual(outcome["step"], "verify")
self.assertIn("/healthz", outcome["message"])
+ def test_the_last_health_error_is_reported_verbatim(self):
+ error = "https://192.0.2.10/healthz ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]"
+ outcome = self.run_deploy(identity=lambda **_kwargs: {"error": error})
+ self.assertFalse(outcome["ok"])
+ self.assertEqual(outcome["step"], "verify")
+ self.assertIn(error, outcome["message"])
+
class ServingIdentityTests(unittest.TestCase):
def test_the_verdict_comes_from_the_https_port_checked_against_the_project_ca(self):
@@ -168,6 +175,21 @@ class ServingIdentityTests(unittest.TestCase):
self.assertFalse(get.call_args.kwargs["trust_env"],
"系统代理会替生产口回话,探测必须直连")
+ def test_a_timeout_carries_the_last_raw_error(self):
+ spec = mock.Mock(health_url="https://192.0.2.10/healthz", verify="/ca/peach.crt")
+ spec.name = "https"
+ refused = deploy_windows_tray.httpx.ConnectError("[SSL: CERTIFICATE_VERIFY_FAILED]")
+ unready = mock.Mock(status_code=503)
+ for answer, expected in ((refused, "ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED]"),
+ (unready, "HTTP 503")):
+ with self.subTest(expected=expected), \
+ mock.patch.object(deploy_windows_tray, "build_service_specs", return_value=(spec,)), \
+ mock.patch.object(deploy_windows_tray.httpx, "get",
+ **({"side_effect": answer} if isinstance(answer, Exception)
+ else {"return_value": answer})):
+ served = deploy_windows_tray.serving_identity(timeout=0, sleep=mock.Mock())
+ self.assertEqual(served, {"error": f"https://192.0.2.10/healthz {expected}"})
+
if __name__ == "__main__":
unittest.main()