fix_guanlan_startbat.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """第三处修复: start.bat 把 "网关已起但有模块降级" 误报成 "没起来"。
  4. 现状: guanlan.py serve 的退出码 1 有**两种**含义 —— ① 网关 healthz 压根没起来;
  5. ② 网关起来了, 但有模块 [DOWN] (本机 Ollama 没跑 / 没拉模型时必然如此)。
  6. start.bat 只看 errorlevel, 于是 ② 也走 "[X] Server did not start ... Not opening the browser",
  7. 用户看到的是 "服务没起", 实际网关在跑、6/7 页面都正常 —— 与原始报障里那条
  8. "[X] Server did not start (reason above)" 同源。
  9. 修复: 退出码非 0 时先探一次 /healthz 区分两种情形; 真没起来才报 [X] 并停;
  10. 起来了但有降级则打 [!] 说明 (哪一类页面受影响), 浏览器照常打开。
  11. 保持 .bat 的 CRLF / 无 BOM 约定。
  12. """
  13. from __future__ import annotations
  14. import pathlib
  15. import shutil
  16. import sys
  17. ROOT = pathlib.Path(r"F:\temp\guanlan-rudong-v2_0.2.0")
  18. START = ROOT / "start.bat"
  19. OLD = (
  20. '".venv\\Scripts\\python.exe" guanlan.py serve\n'
  21. 'if errorlevel 1 (\n'
  22. ' echo.\n'
  23. ' echo [X] Server did not start ^(reason above^). Not opening the browser -\n'
  24. ' echo a "site cannot be reached" page would look like a network problem.\n'
  25. ' echo See logs\\gateway.log\n'
  26. ' pause\n'
  27. ' exit /b 1\n'
  28. ')\n'
  29. 'start "" http://127.0.0.1:28084/\n'
  30. )
  31. NEW = '''".venv\\Scripts\\python.exe" guanlan.py serve
  32. if errorlevel 1 (
  33. rem serve exits 1 in two different cases: the gateway never came up, or the gateway is
  34. rem up with at least one degraded module, e.g. Ollama not running. Probe healthz to tell
  35. rem them apart, otherwise a working server gets reported as "did not start".
  36. ".venv\\Scripts\\python.exe" -c "import urllib.request,sys; sys.exit(0 if urllib.request.urlopen('http://127.0.0.1:28084/healthz',timeout=6).status==200 else 1)" 2>nul
  37. if errorlevel 1 (
  38. echo.
  39. echo [X] Gateway did not come up ^(reason above^). Not opening the browser -
  40. echo a "site cannot be reached" page would look like a network problem.
  41. echo See logs\\gateway.log
  42. pause
  43. exit /b 1
  44. )
  45. echo.
  46. echo [!] Gateway is up, but at least one module is degraded ^(the [DOWN] lines above^).
  47. echo Usual cause: local Ollama is not running or has no models pulled, which only
  48. echo disables the Q^&A / local-review pages. Every other page works.
  49. echo Details: logs\\ for the degraded module.
  50. )
  51. start "" http://127.0.0.1:28084/
  52. '''
  53. def main() -> int:
  54. raw = START.read_bytes()
  55. if raw.startswith(b"\xef\xbb\xbf"):
  56. print(" [X] start.bat 不该有 BOM")
  57. return 1
  58. text = raw.decode("utf-8").replace("\r\n", "\n") # .bat 是 CRLF, 先归一化再匹配
  59. if "Gateway did not come up" in text:
  60. print(" [=] start.bat 已修过, 跳过")
  61. return 0
  62. if text.count(OLD) != 1:
  63. print(f" [X] start.bat 锚点命中 {text.count(OLD)} 次, 未改动")
  64. return 1
  65. b = START.with_name(START.name + ".bak-healthz")
  66. if not b.exists():
  67. shutil.copy2(START, b)
  68. print(f" 备份 start.bat -> {b.name}")
  69. out = text.replace(OLD, NEW).replace("\r\n", "\n").replace("\n", "\r\n")
  70. START.write_bytes(out.encode("utf-8")) # UTF-8 无 BOM + CRLF
  71. print(" [OK] start.bat 已修 (CRLF / 无 BOM 保持不变)")
  72. return 0
  73. if __name__ == "__main__":
  74. sys.exit(main())