feat(service): update PowerShell script for Windows service to enhance logging and execution

This commit is contained in:
Soulter
2026-05-22 20:13:20 +08:00
parent e2c55fa740
commit 85e2560bf3
2 changed files with 12 additions and 12 deletions

View File

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

View File

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