刘光辉
昨天 bb638871a7fb692d80f1b7a758f991dc0879002c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#Requires -Version 5.1
<#
.SYNOPSIS
  JNPF 微服务批量停止脚本(Windows / PowerShell 版)
 
.DESCRIPTION
  按 start-all.ps1 写入的 .pids/*.pid 文件停止服务。
  不依赖 Java、Maven 或 Docker,适合在另一个终端执行。
 
  用法:
    .\stop-all.ps1                         停止全部服务
    .\stop-all.ps1 jnpf-lims               只停止 jnpf-lims
    .\stop-all.ps1 jnpf-lims jnpf-dms      停止多个服务
    .\stop-all.ps1 --help                  显示帮助信息
 
  也可双击 stop-all.bat,或在 cmd 中: stop-all.bat [服务名...]
#>
 
param(
    [Parameter(Position = 0, ValueFromRemainingArguments = $true)]
    [string[]]$ServiceNames = @()
)
 
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$ServiceNames = @($ServiceNames)
 
$Script:BaseDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$Script:ServicePidDir = Join-Path $Script:BaseDir '.pids'
 
# Must stay in sync with start-all.ps1. The JAR name is used to avoid killing
# an unrelated process if an old PID has already been reused by Windows.
$Script:Services = @(
    [PSCustomObject]@{ Name = 'jnpf-gateway';    Jar = 'jnpf-gateway-6.1.0-RELEASE.jar';    Port = 30000 }
    [PSCustomObject]@{ Name = 'jnpf-platform';   Jar = 'jnpf-platform-6.1.0-RELEASE.jar';   Port = 30002 }
    [PSCustomObject]@{ Name = 'jnpf-biz-common'; Jar = 'jnpf-biz-common-6.1.0-RELEASE.jar'; Port = 30015 }
    [PSCustomObject]@{ Name = 'jnpf-eln';        Jar = 'jnpf-eln-6.1.0-RELEASE.jar';        Port = 30013 }
    [PSCustomObject]@{ Name = 'jnpf-lims';       Jar = 'jnpf-lims-6.1.0-RELEASE.jar';       Port = 30019 }
    [PSCustomObject]@{ Name = 'jnpf-dms';        Jar = 'jnpf-dms-6.1.0-RELEASE.jar';        Port = 30016 }
)
 
function Write-ColorLine {
    param(
        [string]$Text,
        [ConsoleColor]$Color = [ConsoleColor]::Gray
    )
    $previous = [Console]::ForegroundColor
    [Console]::ForegroundColor = $Color
    Write-Host $Text
    [Console]::ForegroundColor = $previous
}
 
function Write-Ok {
    param([string]$Text)
    Write-ColorLine $Text Green
}
function Write-Warn {
    param([string]$Text)
    Write-ColorLine $Text Yellow
}
function Write-Err {
    param([string]$Text)
    Write-ColorLine $Text Red
}
 
function Show-Help {
    $scriptName = if ($PSCommandPath) { Split-Path -Leaf $PSCommandPath } else { 'stop-all.ps1' }
    Write-Host ''
    Write-Host 'JNPF 微服务批量停止脚本(Windows)'
    Write-Host ''
    Write-Host "用法: .\$scriptName [服务名...]"
    Write-Host ''
    Write-Host '省略服务名时停止全部服务;指定服务名时只停止对应服务:'
    Write-Host '  jnpf-gateway  jnpf-platform  jnpf-biz-common'
    Write-Host '  jnpf-eln      jnpf-lims      jnpf-dms'
    Write-Host ''
    Write-Host '示例:'
    Write-Host "  .\$scriptName"
    Write-Host "  .\$scriptName jnpf-lims"
    Write-Host "  .\$scriptName jnpf-lims jnpf-platform"
    Write-Host ''
    Write-Host '脚本只处理 start-all.ps1 写入的 .pids/*.pid,不会停止未登记的端口占用进程。'
}
 
function Resolve-TargetServices {
    param([string[]]$Names)
 
    if ($Names.Count -eq 0) {
        return @($Script:Services)
    }
 
    $targets = @()
    foreach ($name in $Names) {
        $service = $Script:Services |
            Where-Object { $_.Name -eq $name } |
            Select-Object -First 1
        if (-not $service) {
            $available = ($Script:Services | ForEach-Object { $_.Name }) -join ' '
            throw "未知服务名: $name`n可用服务:$available"
        }
        $targets += $service
    }
    return $targets
}
 
function Get-PidFilePath {
    param([string]$Name)
    return Join-Path $Script:ServicePidDir "$Name.pid"
}
 
function Get-RecordedPid {
    param([string]$PidFile)
 
    $raw = (Get-Content -LiteralPath $PidFile -Raw -ErrorAction SilentlyContinue)
    $recordedPid = 0
    if ($null -eq $raw -or -not [int]::TryParse($raw.Trim(), [ref]$recordedPid) -or $recordedPid -le 0) {
        return $null
    }
    return $recordedPid
}
 
function Get-ProcessCommandLine {
    param([int]$ProcessId)
 
    $process = Get-CimInstance -ClassName Win32_Process -Filter "ProcessId = $ProcessId" -ErrorAction SilentlyContinue
    if ($process) { return [string]$process.CommandLine }
    return ''
}
 
function Test-RecordedProcessMatchesService {
    param(
        [int]$ProcessId,
        [PSCustomObject]$Service
    )
 
    $commandLine = Get-ProcessCommandLine -ProcessId $ProcessId
    if ([string]::IsNullOrWhiteSpace($commandLine)) { return $true }
 
    $jarPattern = [regex]::Escape($Service.Jar)
    $namePattern = [regex]::Escape($Service.Name)
    return ($commandLine -match $jarPattern -or $commandLine -match $namePattern)
}
 
function Test-ProcessAlive {
    param([int]$ProcessId)
    if ($ProcessId -le 0) { return $false }
    return [bool](Get-Process -Id $ProcessId -ErrorAction SilentlyContinue)
}
 
function Stop-ProcessTree {
    param([int]$ProcessId)
 
    $taskkill = Join-Path $env:SystemRoot 'System32\taskkill.exe'
    if (Test-Path -LiteralPath $taskkill) {
        $previousErrorAction = $ErrorActionPreference
        $ErrorActionPreference = 'Continue'
        & $taskkill /PID $ProcessId /T /F 2>$null | Out-Null
        $ErrorActionPreference = $previousErrorAction
        return ($LASTEXITCODE -eq 0)
    }
 
    Stop-Process -Id $ProcessId -Force -ErrorAction SilentlyContinue
    return ($LASTEXITCODE -eq 0 -or -not (Test-ProcessAlive $ProcessId))
}
 
function Stop-JnpfService {
    param([PSCustomObject]$Service)
 
    $name = $Service.Name
    $pidFile = Get-PidFilePath $name
 
    if (-not (Test-Path -LiteralPath $pidFile)) {
        Write-Warn "  [跳过] $name — 无 PID 文件"
        return $true
    }
 
    $processId = Get-RecordedPid -PidFile $pidFile
    if ($null -eq $processId) {
        Write-Warn "  [清理] $name — PID 文件内容无效"
        Remove-Item -LiteralPath $pidFile -Force -ErrorAction SilentlyContinue
        return $true
    }
 
    if (-not (Test-ProcessAlive $processId)) {
        Write-Warn "  [跳过] $name — 进程已不存在 (PID $processId)"
        Remove-Item -LiteralPath $pidFile -Force -ErrorAction SilentlyContinue
        return $true
    }
 
    if (-not (Test-RecordedProcessMatchesService -ProcessId $processId -Service $Service)) {
        Write-Warn "  [跳过] $name — PID $processId 当前不是该服务,保留 PID 文件供人工检查"
        return $false
    }
 
    if (Stop-ProcessTree -ProcessId $processId) {
        Start-Sleep -Milliseconds 300
        if (Test-ProcessAlive $processId) {
            Write-Warn "  [警告] $name — 已发送停止命令,但 PID $processId 仍在运行"
            return $false
        }
        Write-Ok "  [停止] $name (PID $processId)"
        Remove-Item -LiteralPath $pidFile -Force -ErrorAction SilentlyContinue
        return $true
    }
 
    Write-Err "  [失败] $name — 无法停止 PID $processId"
    return $false
}
 
if ($ServiceNames.Count -eq 1 -and $ServiceNames[0] -in @('help', '--help', '-h')) {
    Show-Help
    exit 0
}
 
$targets = @(Resolve-TargetServices -Names $ServiceNames)
Write-Host '========================================'
Write-Host " 停止 JNPF 微服务 (本次 $($targets.Count) 个)"
Write-Host '========================================'
 
$failed = 0
foreach ($service in $targets) {
    if (-not (Stop-JnpfService -Service $service)) { $failed++ }
}
 
Write-Host '========================================'
if ($failed -gt 0) {
    Write-Err "停止完成,但有 $failed 个服务需要人工检查。"
    exit 1
}
Write-Ok '停止完成。'
exit 0