Handle missing Java service executable

This commit is contained in:
王鹏
2026-10-10 16:35:29 +08:00
parent dd4763a4a7
commit e1c58d8117
4 changed files with 63 additions and 0 deletions

View File

@@ -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"]

View File

@@ -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()

View File

@@ -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)

View File

@@ -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()