121 lines
4.6 KiB
PowerShell
121 lines
4.6 KiB
PowerShell
[CmdletBinding()]
|
|
param(
|
|
[string]$DatabaseUrl = "",
|
|
[int]$KbConcurrency = 1,
|
|
[int]$ShippingConcurrency = 1,
|
|
[switch]$SkipMigrate
|
|
)
|
|
|
|
$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 }
|
|
}
|
|
|
|
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'
|
|
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" }
|
|
}
|
|
|
|
$jobs = @()
|
|
$jobs += Start-Job -Name "hxops-backend" -ScriptBlock {
|
|
param($dir, $python, $db)
|
|
if ($db) { $env:DATABASE_URL = $db }
|
|
Set-Location $dir
|
|
& $python .\run.py 2>&1
|
|
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL)
|
|
$jobs += Start-Job -Name "hxops-frontend" -ScriptBlock {
|
|
param($dir)
|
|
Set-Location $dir
|
|
npm run dev 2>&1
|
|
} -ArgumentList @($frontendDir)
|
|
$jobs += Start-Job -Name "hxops-kb-worker" -ScriptBlock {
|
|
param($dir, $python, $db, $concurrency)
|
|
if ($db) { $env:DATABASE_URL = $db }
|
|
$env:ENV_NAME = "dev"
|
|
$env:KB_WORKER_CONCURRENCY = "$concurrency"
|
|
Set-Location $dir
|
|
& $python .\worker_kb.py 2>&1
|
|
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL, $KbConcurrency)
|
|
$jobs += Start-Job -Name "hxops-shipping-worker" -ScriptBlock {
|
|
param($dir, $python, $db, $concurrency)
|
|
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
|
|
} -ArgumentList @($backendDir, $venvPython, $env:DATABASE_URL, $ShippingConcurrency)
|
|
|
|
Write-Host "[startup] backend, frontend, kb-worker and shipping-worker are running in this IDE terminal." -ForegroundColor Green
|
|
Write-Host "[startup] Press Ctrl+C to stop all services." -ForegroundColor DarkGray
|
|
|
|
try {
|
|
while ($true) {
|
|
foreach ($job in $jobs) {
|
|
Receive-Job -Job $job -ErrorAction SilentlyContinue | ForEach-Object {
|
|
Write-Host "[$($job.Name)] $_"
|
|
}
|
|
if ($job.State -in @("Failed", "Stopped", "Completed")) {
|
|
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'
|
|
Stop-Port 5052
|
|
Stop-Port 8881
|
|
}
|