189 lines
8.4 KiB
PowerShell
189 lines
8.4 KiB
PowerShell
<#
|
||
scripts/start_local_integrated.ps1
|
||
用途:在同一 IDE 终端内用 Start-Job 托管 backend / frontend / workers。
|
||
依赖:backend\.venv、frontend npm、.env.shared / .env.dev / .env.local。
|
||
逻辑:清理旧进程与端口 → 可选迁移 → Start-Job 拉起各服务。
|
||
-NoHotReload:Start-Job 下不能用 WERKZEUG_RUN_MAIN(会 KeyError: WERKZEUG_SERVER_FD),
|
||
改为 python -c + use_reloader=False,不修改 run.py。
|
||
Job 内 stderr 经 2>&1 会变成 ErrorRecord;统一转成纯文本再输出,避免终端误显示为启动失败。
|
||
#>
|
||
[CmdletBinding()]
|
||
param(
|
||
[string]$DatabaseUrl = "",
|
||
[int]$KbConcurrency = 1,
|
||
[int]$ShippingConcurrency = 1,
|
||
[switch]$SkipMigrate,
|
||
[switch]$NoHotReload
|
||
)
|
||
|
||
$ErrorActionPreference = "Stop"
|
||
$repoRoot = Split-Path -Parent $PSScriptRoot
|
||
$backendDir = Join-Path $repoRoot "backend"
|
||
$frontendDir = Join-Path $repoRoot "frontend"
|
||
$venvPython = Join-Path $backendDir ".venv\Scripts\python.exe"
|
||
|
||
if (-not (Test-Path $venvPython)) { throw "Backend virtualenv not found: $venvPython" }
|
||
if (-not (Test-Path $frontendDir)) { throw "Frontend directory not found: $frontendDir" }
|
||
|
||
function Import-DotEnv {
|
||
param([string]$Path)
|
||
if (-not (Test-Path $Path)) { return }
|
||
foreach ($line in Get-Content $Path) {
|
||
if ($line -match '^\s*#' -or $line -match '^\s*$') { continue }
|
||
if ($line -match '^\s*([^=]+)=(.*)$') {
|
||
$name = $matches[1].Trim()
|
||
$value = $matches[2].Trim()
|
||
if ($value.Length -ge 2 -and (($value.StartsWith('"') -and $value.EndsWith('"')) -or ($value.StartsWith("'") -and $value.EndsWith("'")))) {
|
||
$value = $value.Substring(1, $value.Length - 2)
|
||
}
|
||
Set-Item "env:$name" $value
|
||
}
|
||
}
|
||
}
|
||
|
||
function Stop-HxopsProcess {
|
||
param([string]$Pattern)
|
||
Get-CimInstance Win32_Process -Filter "Name='python.exe'" -ErrorAction SilentlyContinue |
|
||
Where-Object { $_.CommandLine -and $_.CommandLine -match $Pattern } |
|
||
ForEach-Object { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue }
|
||
}
|
||
|
||
function Stop-Port {
|
||
param([int]$Port)
|
||
Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue |
|
||
Select-Object -ExpandProperty OwningProcess -Unique |
|
||
ForEach-Object { Stop-Process -Id $_ -Force -ErrorAction SilentlyContinue }
|
||
}
|
||
|
||
# 将 Start-Job 输出压成纯文本,避免 stderr 的 ErrorRecord 被终端标成启动失败。
|
||
function Format-JobLine {
|
||
param([object]$Item)
|
||
if ($null -eq $Item) { return $null }
|
||
if ($Item -is [System.Management.Automation.ErrorRecord]) {
|
||
return [string]$Item.Exception.Message
|
||
}
|
||
return [string]$Item
|
||
}
|
||
|
||
Import-DotEnv (Join-Path $repoRoot ".env.shared")
|
||
Import-DotEnv (Join-Path $repoRoot ".env.dev")
|
||
Import-DotEnv (Join-Path $repoRoot ".env.local")
|
||
if (-not [string]::IsNullOrWhiteSpace($DatabaseUrl)) { $env:DATABASE_URL = $DatabaseUrl }
|
||
|
||
Write-Host "[cleanup] stopping HXOPS processes and ports..." -ForegroundColor Yellow
|
||
Stop-HxopsProcess 'run\.py|worker_kb\.py|worker_shipping\.py|worker_shipping_notice\.py|worker_scheduler\.py|worker_in_transit_transfer\.py'
|
||
Stop-Port 5052
|
||
Stop-Port 8881
|
||
Start-Sleep -Milliseconds 800
|
||
|
||
if (-not $SkipMigrate) {
|
||
$env:FLASK_APP = "run.py"
|
||
$env:FLASK_ENV = "development"
|
||
$env:ENV_NAME = "dev"
|
||
Write-Host "[db-migrate] running flask db upgrade..." -ForegroundColor Cyan
|
||
Push-Location $backendDir
|
||
try { & $venvPython -m flask db upgrade } finally { Pop-Location }
|
||
if ($LASTEXITCODE -ne 0) { throw "Database migration failed with exit code $LASTEXITCODE" }
|
||
}
|
||
|
||
# -NoHotReload 启动代码经 ArgumentList 传入;Start-Job 会吞掉内嵌双引号,故 Python 侧只用单引号。
|
||
$backendNoReloadCode = "import os; from app import create_app; create_app('development').run(host='0.0.0.0', port=int(os.getenv('HXOPS_BACKEND_PORT', '5052')), use_reloader=False, debug=True)"
|
||
|
||
$jobs = @()
|
||
$jobs += Start-Job -Name "hxops-backend" -ScriptBlock {
|
||
param($dir, $python, $db, $disableHotReload, $noReloadCode)
|
||
$ErrorActionPreference = "Continue"
|
||
if ($db) { $env:DATABASE_URL = $db }
|
||
Set-Location $dir
|
||
if ($disableHotReload) {
|
||
& $python -c $noReloadCode 2>&1 | ForEach-Object { "$_" }
|
||
} else {
|
||
& $python .\run.py 2>&1 | ForEach-Object { "$_" }
|
||
}
|
||
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL, [bool]$NoHotReload, $backendNoReloadCode)
|
||
$jobs += Start-Job -Name "hxops-frontend" -ScriptBlock {
|
||
param($dir)
|
||
$ErrorActionPreference = "Continue"
|
||
Set-Location $dir
|
||
npm run dev 2>&1 | ForEach-Object { "$_" }
|
||
} -ArgumentList @($frontendDir)
|
||
$jobs += Start-Job -Name "hxops-kb-worker" -ScriptBlock {
|
||
param($dir, $python, $db, $concurrency)
|
||
$ErrorActionPreference = "Continue"
|
||
if ($db) { $env:DATABASE_URL = $db }
|
||
$env:ENV_NAME = "dev"
|
||
$env:KB_WORKER_CONCURRENCY = "$concurrency"
|
||
Set-Location $dir
|
||
& $python .\worker_kb.py 2>&1 | ForEach-Object { "$_" }
|
||
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL, $KbConcurrency)
|
||
$jobs += Start-Job -Name "hxops-shipping-worker" -ScriptBlock {
|
||
param($dir, $python, $db, $concurrency)
|
||
$ErrorActionPreference = "Continue"
|
||
if ($db) { $env:DATABASE_URL = $db }
|
||
$env:ENV_NAME = "dev"
|
||
$env:SHIPPING_NOTICE_WORKER_CONCURRENCY = "$concurrency"
|
||
Set-Location $dir
|
||
& $python .\worker_shipping.py 2>&1 | ForEach-Object { "$_" }
|
||
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL, $ShippingConcurrency)
|
||
# 排程 worker:消费 schedule 队列并执行到期 ScheduleTask(入口为 backend/workers/worker_scheduler.py,与 start_local.ps1 的 schedule-worker 对齐)
|
||
$jobs += Start-Job -Name "hxops-schedule-worker" -ScriptBlock {
|
||
param($dir, $python, $db)
|
||
$ErrorActionPreference = "Continue"
|
||
if ($db) { $env:DATABASE_URL = $db }
|
||
$env:ENV_NAME = "dev"
|
||
Set-Location $dir
|
||
& $python .\workers\worker_scheduler.py 2>&1 | ForEach-Object { "$_" }
|
||
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL)
|
||
# 固资快照 worker:迁出 Web 进程的常驻同步循环(入口为 backend/workers/worker_fixed_assets.py,与 Compose hxops-fixed-assets-worker 对齐)
|
||
$jobs += Start-Job -Name "hxops-fixed-assets-worker" -ScriptBlock {
|
||
param($dir, $python, $db)
|
||
$ErrorActionPreference = "Continue"
|
||
if ($db) { $env:DATABASE_URL = $db }
|
||
$env:ENV_NAME = "dev"
|
||
Set-Location $dir
|
||
& $python .\workers\worker_fixed_assets.py 2>&1 | ForEach-Object { "$_" }
|
||
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL)
|
||
# 调拨 worker:消费 transfer 队列(在途调拨预检/Run/Item;入口为 backend/workers/worker_in_transit_transfer.py,与 Compose hxops-transfer-worker 对齐)
|
||
$jobs += Start-Job -Name "hxops-transfer-worker" -ScriptBlock {
|
||
param($dir, $python, $db)
|
||
$ErrorActionPreference = "Continue"
|
||
if ($db) { $env:DATABASE_URL = $db }
|
||
$env:ENV_NAME = "dev"
|
||
Set-Location $dir
|
||
& $python .\workers\worker_in_transit_transfer.py 2>&1 | ForEach-Object { "$_" }
|
||
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL)
|
||
|
||
Write-Host "[startup] backend, frontend, kb-worker, shipping-worker, transfer-worker, schedule-worker and fixed-assets-worker are running in this IDE terminal." -ForegroundColor Green
|
||
if ($NoHotReload) {
|
||
Write-Host "[startup] backend hot-reload disabled (-NoHotReload / use_reloader=False)." -ForegroundColor DarkGray
|
||
}
|
||
Write-Host "[startup] Press Ctrl+C to stop all services." -ForegroundColor DarkGray
|
||
|
||
$warnedCompleted = @{}
|
||
try {
|
||
while ($true) {
|
||
foreach ($job in $jobs) {
|
||
Receive-Job -Job $job -ErrorAction SilentlyContinue | ForEach-Object {
|
||
$line = Format-JobLine $_
|
||
if (-not [string]::IsNullOrWhiteSpace($line)) {
|
||
Write-Host "[$($job.Name)] $line"
|
||
}
|
||
}
|
||
if ($job.State -in @("Failed", "Stopped", "Completed")) {
|
||
if (-not $warnedCompleted.ContainsKey($job.Name)) {
|
||
$warnedCompleted[$job.Name] = $true
|
||
Write-Warning "[$($job.Name)] state: $($job.State)"
|
||
}
|
||
}
|
||
}
|
||
Start-Sleep -Milliseconds 500
|
||
}
|
||
}
|
||
finally {
|
||
$jobs | Stop-Job -ErrorAction SilentlyContinue
|
||
$jobs | Remove-Job -Force -ErrorAction SilentlyContinue
|
||
Stop-HxopsProcess 'run\.py|worker_kb\.py|worker_shipping\.py|worker_shipping_notice\.py|worker_scheduler\.py|worker_in_transit_transfer\.py'
|
||
Stop-Port 5052
|
||
Stop-Port 8881
|
||
}
|