Pass Node path to frontend services

This commit is contained in:
王鹏
2026-10-09 13:23:12 +08:00
parent e5e584eb73
commit c2d124cc09
3 changed files with 56 additions and 1 deletions

View File

@@ -17,7 +17,8 @@ AutoDeploy 是一个面向 Windows 的 Spring Boot / SSM + Vue 本地自动化
- 服务启动前检查端口占用并终止旧进程,等待新服务端口就绪后再继续。 - 服务启动前检查端口占用并终止旧进程,等待新服务端口就绪后再继续。
- 后端在独立控制台中显示实时输出,各服务同时保留运行日志。 - 后端在独立控制台中显示实时输出,各服务同时保留运行日志。
- Maven、npm 等命令输出会自动识别 UTF-8 和 Windows 中文编码,避免日志解码导致部署任务提前中断。 - Maven、npm 等命令输出会自动识别 UTF-8 和 Windows 中文编码,避免日志解码导致部署任务提前中断。
- 使用手动指定或工具自动安装的 npm 时,会把其 Node.js 目录加入部署子进程的 `PATH`,保证依赖安装脚本可以调用 `node`。 - 使用手动指定或工具自动安装的 npm 时,会把其 Node.js 目录加入依赖安装和前端启动子进程的 `PATH`,保证 npm 生命周期脚本可以调用 `node`。
- 前端进程在端口就绪前退出时,主界面会直接显示日志最后 30 行,便于定位编译和配置错误。
## 运行环境 ## 运行环境

View File

@@ -662,6 +662,7 @@ def _restart_frontend(
stdin=subprocess.DEVNULL, stdin=subprocess.DEVNULL,
stdout=log_file, stdout=log_file,
stderr=subprocess.STDOUT, stderr=subprocess.STDOUT,
env=_build_process_environment(command),
shell=False, shell=False,
creationflags=_background_creation_flags(), creationflags=_background_creation_flags(),
) )
@@ -677,6 +678,9 @@ def _restart_frontend(
return_code = process.poll() return_code = process.poll()
if return_code is not None: if return_code is not None:
on_output(f"{service_name}进程 PID {process.pid} 在端口就绪前退出,退出码: {return_code},日志: {log_path}") on_output(f"{service_name}进程 PID {process.pid} 在端口就绪前退出,退出码: {return_code},日志: {log_path}")
log_tail = _read_log_tail(log_path)
if log_tail:
on_output(f"{service_name}日志末尾:\n{log_tail}")
return return_code or 1 return return_code or 1
time.sleep(0.2) time.sleep(0.2)
@@ -685,6 +689,18 @@ def _restart_frontend(
return 1 return 1
def _read_log_tail(log_path: Path, line_count: int = 30) -> str:
try:
raw_lines = log_path.read_bytes().splitlines()[-line_count:]
except OSError:
return ""
return "\n".join(
line
for line in (_decode_process_output(raw).rstrip() for raw in raw_lines)
if line
)
def _background_creation_flags() -> int: def _background_creation_flags() -> int:
if os.name != "nt": if os.name != "nt":
return 0 return 0

View File

@@ -326,6 +326,44 @@ class DeployerTests(unittest.TestCase):
self.assertEqual(exit_code, 7) self.assertEqual(exit_code, 7)
restart_backend.assert_called_once() restart_backend.assert_called_once()
def test_frontend_early_exit_reports_log_tail(self):
logs = []
popen_options = {}
npm = Path.cwd() / "fake-node" / "npm.cmd"
class ExitedProcess:
pid = 22996
def poll(self):
return 1
def fake_popen(*args, **kwargs):
popen_options.update(kwargs)
kwargs["stdout"].write("ERROR Failed to compile.\nModule not found: missing-package\n")
kwargs["stdout"].flush()
return ExitedProcess()
with TemporaryDirectory() as temp:
root = Path(temp)
exit_code = deployer_module._restart_frontend(
[str(npm), "run", "serve"],
root,
logs.append,
port=8081,
service_name="管理端",
log_path=root / "frontend-admin.log",
replace_listeners=lambda port, on_output: True,
popen=fake_popen,
)
self.assertEqual(exit_code, 1)
self.assertTrue(any("管理端日志末尾" in line for line in logs))
self.assertTrue(any("Module not found: missing-package" in line for line in logs))
self.assertEqual(
Path(popen_options["env"]["PATH"].split(os.pathsep)[0]),
npm.parent,
)
def test_backend_restart_separates_each_run_in_backend_log(self): def test_backend_restart_separates_each_run_in_backend_log(self):
restart_backend = getattr(deployer_module, "_restart_backend", lambda *args, **kwargs: 0) restart_backend = getattr(deployer_module, "_restart_backend", lambda *args, **kwargs: 0)