77 lines
3.3 KiB
PowerShell
77 lines
3.3 KiB
PowerShell
<#
|
||
使用 NSSM(Non-Sucking Service Manager,https://nssm.cc/download)将后端/前端
|
||
注册为 Windows 服务,实现开机自启 + 崩溃自动重启。
|
||
|
||
前置条件:
|
||
- 已下载 nssm.exe 并加入 PATH(或把完整路径填到 $Nssm 变量)
|
||
- 后端虚拟环境已就绪:<repo>\.venv
|
||
- 前端依赖已安装 + 已 `npm run build`:<repo>\frontend
|
||
|
||
用法(管理员 PowerShell):
|
||
cd C:\wagtailcms
|
||
.\deploy\windows\install-nssm-services.ps1
|
||
#>
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
|
||
# PowerShell 7.3+ 默认会把"原生命令写了 stderr 且退出码非 0"当作终止性错误抛出
|
||
# (即使用 2>$null 重定向也无法拦截,因为触发条件是退出码而非 stderr 内容)。
|
||
# 下面 Install-NssmService 里首次安装时 `nssm stop`/`nssm remove` 针对尚不存在的
|
||
# 服务必然会失败,这是预期内的可忽略错误,因此关闭该行为。
|
||
$PSNativeCommandUseErrorActionPreference = $false
|
||
|
||
$Nssm = "nssm.exe" # 若 nssm 不在 PATH,改成绝对路径,如 "C:\tools\nssm-2.24\win64\nssm.exe"
|
||
$RepoRoot = Resolve-Path (Join-Path $PSScriptRoot "..\..")
|
||
$Powershell = (Get-Command powershell.exe).Source
|
||
|
||
function Install-NssmService {
|
||
param(
|
||
[string]$Name,
|
||
[string]$Application,
|
||
[string]$Arguments,
|
||
[string]$WorkingDirectory
|
||
)
|
||
|
||
# 首次安装时服务尚不存在,stop/remove 必然失败(写 stderr + 非 0 退出码)。
|
||
# $PSNativeCommandUseErrorActionPreference 在部分环境下未必生效,这里显式把
|
||
# $ErrorActionPreference 局部降级为 SilentlyContinue,确保这两步失败不会中断脚本。
|
||
$prevEAP = $ErrorActionPreference
|
||
$ErrorActionPreference = "SilentlyContinue"
|
||
& $Nssm stop $Name 2>$null | Out-Null
|
||
& $Nssm remove $Name confirm 2>$null | Out-Null
|
||
$ErrorActionPreference = $prevEAP
|
||
|
||
& $Nssm install $Name $Application $Arguments
|
||
& $Nssm set $Name AppDirectory $WorkingDirectory
|
||
& $Nssm set $Name AppStdout (Join-Path $WorkingDirectory "logs\$Name-stdout.log")
|
||
& $Nssm set $Name AppStderr (Join-Path $WorkingDirectory "logs\$Name-stderr.log")
|
||
& $Nssm set $Name AppRotateFiles 1
|
||
& $Nssm set $Name AppRotateBytes 10485760
|
||
& $Nssm set $Name Start SERVICE_AUTO_START
|
||
& $Nssm set $Name AppRestartDelay 5000
|
||
|
||
Write-Host "已注册服务:$Name" -ForegroundColor Green
|
||
}
|
||
|
||
New-Item -ItemType Directory -Force -Path (Join-Path $RepoRoot "logs") | Out-Null
|
||
New-Item -ItemType Directory -Force -Path (Join-Path $RepoRoot "frontend\logs") | Out-Null
|
||
|
||
# --- 后端:wagtailcms-backend(waitress WSGI,监听 127.0.0.1:8080) -------------
|
||
Install-NssmService `
|
||
-Name "wagtailcms-backend" `
|
||
-Application $Powershell `
|
||
-Arguments "-NoProfile -ExecutionPolicy Bypass -File `"$RepoRoot\deploy\windows\serve-backend.ps1`"" `
|
||
-WorkingDirectory $RepoRoot
|
||
|
||
# --- 前端:wagtailcms-frontend(Next.js, npm run start,监听 3000) -------------
|
||
$npmCmd = (Get-Command npm.cmd).Source
|
||
Install-NssmService `
|
||
-Name "wagtailcms-frontend" `
|
||
-Application $npmCmd `
|
||
-Arguments "run start" `
|
||
-WorkingDirectory (Join-Path $RepoRoot "frontend")
|
||
|
||
Write-Host ""
|
||
Write-Host "启动服务:Start-Service wagtailcms-backend; Start-Service wagtailcms-frontend" -ForegroundColor Yellow
|
||
Write-Host "查看状态:Get-Service wagtailcms-*" -ForegroundColor Yellow
|