From 85e2560bf3c6ec70264e27ac501511173c7a4aa4 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Fri, 22 May 2026 20:13:20 +0800 Subject: [PATCH] feat(service): update PowerShell script for Windows service to enhance logging and execution --- astrbot/cli/commands/cmd_service.py | 17 +++++++---------- tests/test_cli_service.py | 7 +++++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/astrbot/cli/commands/cmd_service.py b/astrbot/cli/commands/cmd_service.py index 0fe6f0a3a..c0f444a46 100644 --- a/astrbot/cli/commands/cmd_service.py +++ b/astrbot/cli/commands/cmd_service.py @@ -363,21 +363,18 @@ def _build_windows_powershell_arguments( executable: Path, workdir: Path, ) -> str: + out_log, err_log = _windows_service_log_paths(service_name) script = ( "$ErrorActionPreference = 'Stop'\n" "$env:PYTHONUNBUFFERED = '1'\n" "$env:PYTHONUTF8 = '1'\n" "$env:PYTHONIOENCODING = 'utf-8'\n" - "$cmdExe = if ($env:COMSPEC) { $env:COMSPEC } else { 'cmd.exe' }\n" - f"$astrbotCommand = {_quote_powershell_literal(_build_windows_cmd_line(service_name, executable))}\n" - "$process = Start-Process " - "-FilePath $cmdExe " - "-ArgumentList @('/d', '/c', $astrbotCommand) " - f"-WorkingDirectory {_quote_powershell_literal(workdir)} " - "-WindowStyle Hidden " - "-PassThru " - "-Wait\n" - "exit $process.ExitCode\n" + f"Set-Location -LiteralPath {_quote_powershell_literal(workdir)}\n" + f"$stdoutLog = {_quote_powershell_literal(out_log)}\n" + f"$stderrLog = {_quote_powershell_literal(err_log)}\n" + "New-Item -ItemType Directory -Force -Path (Split-Path -Parent $stdoutLog) | Out-Null\n" + f"& {_quote_powershell_literal(executable)} run 1>> $stdoutLog 2>> $stderrLog\n" + "exit $LASTEXITCODE\n" ) encoded_script = base64.b64encode(script.encode("utf-16le")).decode("ascii") return ( diff --git a/tests/test_cli_service.py b/tests/test_cli_service.py index 5c192362a..97c458122 100644 --- a/tests/test_cli_service.py +++ b/tests/test_cli_service.py @@ -149,8 +149,11 @@ def test_windows_task_xml_uses_astrbot_executable_and_working_directory(): assert "$env:PYTHONUNBUFFERED = '1'" in powershell_script assert "$env:PYTHONUTF8 = '1'" in powershell_script assert "$env:PYTHONIOENCODING = 'utf-8'" in powershell_script - assert "Start-Process" in powershell_script - assert "-WindowStyle Hidden" in powershell_script + assert "Set-Location -LiteralPath 'C:\\Users\\astrbot\\AstrBot'" in powershell_script + assert "New-Item -ItemType Directory -Force" in powershell_script + assert "& 'C:\\Users\\astrbot\\.local\\bin\\astrbot.exe' run" in powershell_script + assert "1>> $stdoutLog 2>> $stderrLog" in powershell_script + assert "Start-Process" not in powershell_script assert "C:\\Users\\astrbot\\.local\\bin\\astrbot.exe" in powershell_script assert "run" in powershell_script assert "astrbot.out.log" in powershell_script