Fix packaged service console output
This commit is contained in:
@@ -7,6 +7,7 @@ import os
|
||||
import subprocess
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import TextIO
|
||||
|
||||
|
||||
def _set_console_title(title: str) -> None:
|
||||
@@ -14,6 +15,26 @@ def _set_console_title(title: str) -> None:
|
||||
ctypes.windll.kernel32.SetConsoleTitleW(title)
|
||||
|
||||
|
||||
def _open_console_output(encoding: str) -> tuple[TextIO | None, bool]:
|
||||
if os.name == "nt":
|
||||
try:
|
||||
return open("CONOUT$", "w", encoding=encoding, errors="replace", buffering=1), True
|
||||
except OSError:
|
||||
pass
|
||||
return sys.stdout, False
|
||||
|
||||
|
||||
def _write_console(stream: TextIO | None, value: str) -> bool:
|
||||
if stream is None:
|
||||
return False
|
||||
try:
|
||||
stream.write(value)
|
||||
stream.flush()
|
||||
return True
|
||||
except (OSError, ValueError):
|
||||
return False
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(description="Run a service in a visible console and tee its output to a log file.")
|
||||
parser.add_argument("--title", default="后台服务")
|
||||
@@ -39,6 +60,10 @@ def main() -> int:
|
||||
executable = Path(command[0])
|
||||
if executable.name.lower() in {"catalina.bat", "catalina.sh"} and executable.exists():
|
||||
environment["CATALINA_HOME"] = str(executable.parent.parent)
|
||||
console_output, owns_console_output = _open_console_output(encoding)
|
||||
opened_console_output = console_output if owns_console_output else None
|
||||
process: subprocess.Popen[str] | None = None
|
||||
try:
|
||||
with log_path.open("a", encoding="utf-8") as log_file:
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
@@ -54,21 +79,26 @@ def main() -> int:
|
||||
)
|
||||
pid_path.write_text(str(process.pid), encoding="ascii")
|
||||
assert process.stdout is not None
|
||||
try:
|
||||
for line in process.stdout:
|
||||
print(line, end="", flush=True)
|
||||
if not _write_console(console_output, line):
|
||||
console_output = None
|
||||
log_file.write(line)
|
||||
log_file.flush()
|
||||
return process.wait()
|
||||
except KeyboardInterrupt:
|
||||
if process is None:
|
||||
return 1
|
||||
process.terminate()
|
||||
return process.wait()
|
||||
finally:
|
||||
if process is not None and process.stdout is not None:
|
||||
process.stdout.close()
|
||||
try:
|
||||
pid_path.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
if opened_console_output is not None:
|
||||
opened_console_output.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -5,6 +5,7 @@ from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
from deploy_tool.service_console import main
|
||||
import deploy_tool.service_console as service_console_module
|
||||
|
||||
|
||||
class ServiceConsoleTests(unittest.TestCase):
|
||||
@@ -32,6 +33,40 @@ class ServiceConsoleTests(unittest.TestCase):
|
||||
self.assertIn("service-ready", log_path.read_text(encoding="utf-8"))
|
||||
self.assertFalse(pid_path.exists())
|
||||
|
||||
def test_invalid_console_output_does_not_stop_service_or_log_capture(self):
|
||||
class InvalidConsole:
|
||||
def write(self, _value):
|
||||
raise OSError(22, "Invalid argument")
|
||||
|
||||
def flush(self):
|
||||
pass
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
root = Path(temp)
|
||||
log_path = root / "service.log"
|
||||
pid_path = root / "service.pid"
|
||||
argv = [
|
||||
"service_console.py",
|
||||
"--log",
|
||||
str(log_path),
|
||||
"--pid-file",
|
||||
str(pid_path),
|
||||
"--",
|
||||
sys.executable,
|
||||
"-c",
|
||||
"print('java-service-ready')",
|
||||
]
|
||||
|
||||
with (
|
||||
patch.object(sys, "argv", argv),
|
||||
patch.object(service_console_module, "_open_console_output", return_value=(InvalidConsole(), False)),
|
||||
):
|
||||
exit_code = main()
|
||||
|
||||
self.assertEqual(exit_code, 0)
|
||||
self.assertIn("java-service-ready", log_path.read_text(encoding="utf-8"))
|
||||
self.assertFalse(pid_path.exists())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user