From e1c58d8117b16b560f1f8945cf467af80cfc5061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E9=B9=8F?= Date: Sat, 10 Oct 2026 16:35:29 +0800 Subject: [PATCH] Handle missing Java service executable --- deploy_tool/deployer.py | 17 +++++++++++++++++ deploy_tool/service_console.py | 9 +++++++++ tests/test_deployer.py | 13 +++++++++++++ tests/test_service_console.py | 24 ++++++++++++++++++++++++ 4 files changed, 63 insertions(+) diff --git a/deploy_tool/deployer.py b/deploy_tool/deployer.py index f2342c2..9d748ae 100644 --- a/deploy_tool/deployer.py +++ b/deploy_tool/deployer.py @@ -579,6 +579,16 @@ def _restart_backend( port = port if port is not None else _extract_server_port(command) if port is None: raise ValueError("后端启动命令缺少 -Dserver.port 配置") + if popen is None and not _launch_executable_exists(command[0]): + executable = command[0] + if Path(executable).name.lower() in {"java", "java.exe"}: + on_output( + "未找到 Java 启动程序。请在“安装环境”中安装 JDK," + "或点击 JDK 的“选择路径”选择 JDK 根目录或 bin\\java.exe,然后重新检测环境。" + ) + else: + on_output(f"未找到服务启动程序: {executable}") + return 1 replace_listeners = replace_listeners or _replace_port_listeners if not replace_listeners(port, on_output): on_output(f"端口 {port} 未能在限定时间内释放,后端未启动。") @@ -644,6 +654,13 @@ def _restart_backend( return 1 +def _launch_executable_exists(executable: str) -> bool: + path = Path(executable) + if path.is_absolute() or any(separator in executable for separator in ("/", "\\")): + return path.is_file() + return shutil.which(executable) is not None + + def _service_console_launcher() -> list[str]: if getattr(sys, "frozen", False): return [sys.executable, "--service-console"] diff --git a/deploy_tool/service_console.py b/deploy_tool/service_console.py index 02381ad..af577c2 100644 --- a/deploy_tool/service_console.py +++ b/deploy_tool/service_console.py @@ -90,6 +90,15 @@ def main() -> int: return 1 process.terminate() return process.wait() + except OSError as exc: + message = f"无法启动服务命令 {command[0]}: {exc}\n" + _write_console(console_output, message) + try: + with log_path.open("a", encoding="utf-8") as log_file: + log_file.write(message) + except OSError: + pass + return 1 finally: if process is not None and process.stdout is not None: process.stdout.close() diff --git a/tests/test_deployer.py b/tests/test_deployer.py index 10a8c56..faea43d 100644 --- a/tests/test_deployer.py +++ b/tests/test_deployer.py @@ -175,6 +175,19 @@ class DeployerTests(unittest.TestCase): self.assertEqual(exit_code, 1) self.assertEqual(spawned, []) + def test_backend_restart_reports_missing_java_before_opening_console(self): + logs = [] + with TemporaryDirectory() as temp, patch.object(deployer_module.shutil, "which", return_value=None): + exit_code = deployer_module._restart_backend( + ["java", "-Dserver.port=8080", "-jar", "app.jar"], + Path(temp), + logs.append, + replace_listeners=lambda _port, _output: True, + ) + + self.assertEqual(exit_code, 1) + self.assertTrue(any("未找到 Java 启动程序" in line for line in logs)) + def test_backend_restart_returns_child_exit_code_when_java_exits_early(self): restart_backend = getattr(deployer_module, "_restart_backend", lambda *args, **kwargs: 0) diff --git a/tests/test_service_console.py b/tests/test_service_console.py index 2c56f14..dc09987 100644 --- a/tests/test_service_console.py +++ b/tests/test_service_console.py @@ -67,6 +67,30 @@ class ServiceConsoleTests(unittest.TestCase): self.assertIn("java-service-ready", log_path.read_text(encoding="utf-8")) self.assertFalse(pid_path.exists()) + def test_missing_service_executable_is_logged_without_unhandled_exception(self): + with tempfile.TemporaryDirectory() as temp: + root = Path(temp) + log_path = root / "service.log" + pid_path = root / "service.pid" + missing_executable = root / "missing-java.exe" + argv = [ + "service_console.py", + "--log", + str(log_path), + "--pid-file", + str(pid_path), + "--", + str(missing_executable), + "-version", + ] + + with patch.object(sys, "argv", argv): + exit_code = main() + + self.assertEqual(exit_code, 1) + self.assertIn("无法启动服务命令", log_path.read_text(encoding="utf-8")) + self.assertFalse(pid_path.exists()) + if __name__ == "__main__": unittest.main()