# dev/status.ps1 — 查看本地开发环境状态(端口 / PID / 健康检查) $ErrorActionPreference = 'Stop' $CliRoot = Split-Path -Parent $PSScriptRoot . (Join-Path $CliRoot 'lib\common.ps1') $envMap = Import-LocalEnv (Join-Path $CliRoot 'config\local.env') $backendPort = [int](Get-Setting $envMap 'BACKEND_PORT' '8090') $frontendPort = [int](Get-Setting $envMap 'FRONTEND_PORT' '4321') $visionPort = [int](Get-Setting $envMap 'VISION_PORT' '8900') # 注意:不要写成 @( cmd1, cmd2 ) —— PowerShell 会把逗号当成 cmd1 的参数分隔符 $stBackend = Get-ServiceStatus -Name 'backend' -Port $backendPort -HealthUrl "http://127.0.0.1:$backendPort/api/health" $stFrontend = Get-ServiceStatus -Name 'frontend' -Port $frontendPort -HealthUrl "http://127.0.0.1:$frontendPort/cn/runway-looks/" $stVision = Get-ServiceStatus -Name 'vision' -Port $visionPort -HealthUrl "http://127.0.0.1:$visionPort/health" $rows = @($stBackend, $stFrontend, $stVision) # 后端另外两个内部端口只做占用展示 $internal = @( @{ Name = 'ssg'; Port = 8091 }, @{ Name = 'backstage'; Port = 8092 } ) Write-Host '' Write-Host ' 本地开发环境状态' -ForegroundColor White Write-Host ' ----------------------------------------------' -ForegroundColor DarkGray Write-Host (' {0,-10} {1,-6} {2,-8} {3,-10} {4}' -f '服务', '端口', 'PID', '监听', '健康') -ForegroundColor DarkGray foreach ($r in $rows) { $listen = '否' if ($r.Listening) { $listen = '是' } $health = '否' if ($r.Healthy) { $health = '是' } $color = 'Gray' if ($r.Healthy) { $color = 'Green' } elseif ($r.Listening) { $color = 'Yellow' } $pidText = '-' if ($r.Pid -gt 0) { $pidText = [string]$r.Pid } elseif ($r.OwnerPid -gt 0) { $pidText = [string]$r.OwnerPid + '*' } Write-Host (' {0,-10} {1,-6} {2,-8} {3,-10} {4}' -f $r.Name, $r.Port, $pidText, $listen, $health) -ForegroundColor $color } # 8091/8092 属于后端同一进程,占用者等于登记的 backend PID 时不算「野进程」 $backendPid = Get-ServicePid 'backend' foreach ($i in $internal) { $listen = '否' $pidText = '-' if (Test-PortInUse $i.Port) { $listen = '是' $owner = Get-PortOwnerPid $i.Port if ($backendPid -gt 0 -and $owner -eq $backendPid) { $pidText = [string]$owner } else { $pidText = [string]$owner + '*' } } Write-Host (' {0,-10} {1,-6} {2,-8} {3,-10} {4}' -f $i.Name, $i.Port, $pidText, $listen, '-') -ForegroundColor DarkGray } Write-Host '' Write-Note '* = 端口被占用,但不是 dev.ps1 登记的进程(可能是手动启动的)' Write-Note '启动:.\dev\dev.ps1 停止:.\dev\stop.ps1 体检:.\dev\doctor.ps1'