feat(shipping-notice): 完善发货通知自动推进与多库存源复核闭环

This commit is contained in:
hugo committed 2026-08-09 11:43:53 +08:00
1 parent cfa19a0b7b
commit 8e0bbf7a81
180 files changed
+31210 -1864

No files matched your search

+150 -29
View File
@@ -2,15 +2,20 @@
scripts/start_local.ps1
用途:本地一键启动 HXOPS 前后端与后台 worker。
依赖:backend venv、frontend npm、.env.shared / .env.dev(worker 与 RPA 读取)。
逻辑:先停止本仓库遗留的 backend/worker/frontend 进程并释放 5052/8881 端口,再分别在新 PowerShell 窗口启动各服务。
逻辑:先停止本仓库遗留的 backend/worker/frontend 进程并释放 5052/8881 端口 → 执行 DB 迁移(flask db upgrade)→ 再分别在新 PowerShell 窗口启动各服务。
Worker 使用 backend/ 根目录的并发入口(worker_kb.py / worker_shipping.py),内部通过 multiprocessing 自分叉子进程池;并发数由 -KbConcurrency / -ShippingConcurrency 透传到环境变量。
#>
param(
[ValidateSet("sqlite", "postgres")]
[string]$DbMode = "sqlite",
[string]$DatabaseUrl = "",
[int]$KbConcurrency = 1,
[int]$ShippingConcurrency = 1,
[switch]$SkipInstall,
[switch]$SkipMigrate,
[switch]$SkipWorker,
[switch]$SkipRpaWorker
[switch]$IncludeRpaWorker,
[switch]$NoHotReload
)
$ErrorActionPreference = "Stop"
@@ -61,10 +66,17 @@ function Stop-StaleHxopsPythonProcesses {
$patterns = @(
[regex]::Escape((Join-Path $BackendPath "run.py")),
[regex]::Escape((Join-Path $BackendPath "worker.py")),
[regex]::Escape((Join-Path $BackendPath "worker_kb.py")),
[regex]::Escape((Join-Path $BackendPath "worker_shipping.py")),
[regex]::Escape((Join-Path $BackendPath "workers\worker_kb.py")),
[regex]::Escape((Join-Path $BackendPath "workers\worker_shipping_notice.py")),
[regex]::Escape((Join-Path $BackendPath "workers\worker_scheduler.py")),
[regex]::Escape((Join-Path $RepoRoot "scripts\rpa\freight_rpa_worker.py"))
)
# 说明:上面同时包含 backend/ 根目录的旧部署入口与 workers/ 目录的新单实例入口。
# 两套在仓库中并存(旧路径用于生产多进程部署,新路径推荐用于本地 IDE 单实例调试)。
# 清理阶段必须全部覆盖,避免历史残留进程导致的「多实例重复领取 job / 端口占用」。
Get-CimInstance Win32_Process -Filter "Name='python.exe'" -ErrorAction SilentlyContinue | ForEach-Object {
$cmd = $_.CommandLine
@@ -214,6 +226,32 @@ function Ensure-Backend {
}
}
function Invoke-DbMigrate {
param(
[string]$BackendPath,
[string]$Mode,
[string]$DbUrl
)
Write-Host "[db-migrate] running flask db upgrade..." -ForegroundColor Cyan
Set-Location $BackendPath
$venvPy = Join-Path $BackendPath "venv\Scripts\python.exe"
$env:FLASK_APP = "run.py"
$env:FLASK_ENV = "development"
$env:ENV_NAME = "dev"
if ($Mode -eq "postgres" -and -not [string]::IsNullOrWhiteSpace($DbUrl)) {
$env:DATABASE_URL = $DbUrl
}
& $venvPy -m flask db upgrade
if ($LASTEXITCODE -ne 0) {
throw "[db-migrate] flask db upgrade failed with exit code $LASTEXITCODE"
}
Write-Host "[db-migrate] migration completed." -ForegroundColor Green
}
function Ensure-Frontend {
param(
[string]$FrontendPath,
@@ -237,7 +275,8 @@ function Start-BackendWindow {
param(
[string]$BackendPath,
[string]$Mode,
[string]$DbUrl
[string]$DbUrl,
[bool]$DisableHotReload = $false
)
$dbExport = ""
@@ -246,7 +285,13 @@ function Start-BackendWindow {
$dbExport = "`$env:DATABASE_URL='$escaped'; "
}
$cmd = "$dbExport Set-Location '$BackendPath'; .\venv\Scripts\python.exe .\run.py"
if ($DisableHotReload) {
$reloaderEnv = "`$env:WERKZEUG_RUN_MAIN='true'; "
} else {
$reloaderEnv = ""
}
$cmd = "$dbExport$reloaderEnv Set-Location '$BackendPath'; .\venv\Scripts\python.exe .\run.py"
Start-Process powershell -ArgumentList @("-NoExit", "-ExecutionPolicy", "Bypass", "-Command", $cmd) | Out-Null
}
@@ -263,6 +308,34 @@ function Get-DatabaseUrlExport {
return ""
}
function Import-DotEnvFilesToCurrentShell {
param([string[]]$Paths)
foreach ($path in $Paths) {
if (-not (Test-Path $path)) {
Write-Host "[env] skip (not found): $path" -ForegroundColor DarkGray
continue
}
Write-Host "[env] loading: $path" -ForegroundColor Cyan
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 -Path "env:$name" -Value $value -ErrorAction SilentlyContinue
}
}
}
}
function Get-DotEnvExportPrefix {
param([string[]]$Paths)
@@ -310,13 +383,17 @@ function Start-KbWorkerWindow {
param(
[string]$BackendPath,
[string]$Mode,
[string]$DbUrl
[string]$DbUrl,
[int]$Concurrency = 1
)
$envPrefix = Get-DatabaseUrlExport -Mode $Mode -DbUrl $DbUrl
$envPrefix += " `$env:ENV_NAME='dev'; `$env:KB_WORKER_CONCURRENCY='$Concurrency';"
Start-WorkerWindow `
-Title "kb-worker" `
-Title "kb-worker (concurrency=$Concurrency)" `
-WorkingDirectory $BackendPath `
-EnvPrefix (Get-DatabaseUrlExport -Mode $Mode -DbUrl $DbUrl) `
-EnvPrefix $envPrefix `
-Command ".\venv\Scripts\python.exe .\worker_kb.py"
}
@@ -324,13 +401,17 @@ function Start-ShippingWorkerWindow {
param(
[string]$BackendPath,
[string]$Mode,
[string]$DbUrl
[string]$DbUrl,
[int]$Concurrency = 1
)
$envPrefix = Get-DatabaseUrlExport -Mode $Mode -DbUrl $DbUrl
$envPrefix += " `$env:ENV_NAME='dev'; `$env:SHIPPING_NOTICE_WORKER_CONCURRENCY='$Concurrency';"
Start-WorkerWindow `
-Title "shipping-worker" `
-Title "shipping-worker (concurrency=$Concurrency)" `
-WorkingDirectory $BackendPath `
-EnvPrefix (Get-DatabaseUrlExport -Mode $Mode -DbUrl $DbUrl) `
-EnvPrefix $envPrefix `
-Command ".\venv\Scripts\python.exe .\worker_shipping.py"
}
@@ -352,8 +433,16 @@ function Start-FreightRpaWorkerWindow {
}
function Start-FrontendWindow {
param([string]$FrontendPath)
$cmd = "Set-Location '$FrontendPath'; npm run dev"
param(
[string]$FrontendPath,
[bool]$DisableHotReload = $false
)
if ($DisableHotReload) {
$envArg = "`$env:VITE_HMR='false'; "
} else {
$envArg = ""
}
$cmd = "$envArg Set-Location '$FrontendPath'; npm run dev"
Start-Process powershell -ArgumentList @("-NoExit", "-ExecutionPolicy", "Bypass", "-Command", $cmd) | Out-Null
}
@@ -364,30 +453,61 @@ $frontendDir = Join-Path $repoRoot "frontend"
if (-not (Test-Path $backendDir)) { throw "backend directory not found: $backendDir" }
if (-not (Test-Path $frontendDir)) { throw "frontend directory not found: $frontendDir" }
$pythonCmd = Get-PythonCommand
$doInstall = -not $SkipInstall.IsPresent
Write-Host "== HXOPS local startup ==" -ForegroundColor Green
Write-Host "mode: $DbMode" -ForegroundColor Green
Stop-LocalHxopsStack -RepoRoot $repoRoot -BackendPath $backendDir -FrontendPath $frontendDir
Ensure-Backend -BackendPath $backendDir -PyCmd $pythonCmd -Mode $DbMode -DbUrl $DatabaseUrl -DoInstall $doInstall
Ensure-Frontend -FrontendPath $frontendDir -DoInstall $doInstall
$envFiles = @(
(Join-Path $repoRoot ".env.shared"),
(Join-Path $repoRoot ".env.dev"),
(Join-Path $repoRoot ".env.local")
)
$windowCount = 2
Start-BackendWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $DatabaseUrl
# 优先级加载:.env.shared → .env.dev → .env.local(后者覆盖前者)
# 显式传参 -DatabaseUrl 的优先级最高,最后覆盖。
Import-DotEnvFilesToCurrentShell -Paths $envFiles
$effectiveDbUrl = $DatabaseUrl
if ([string]::IsNullOrWhiteSpace($effectiveDbUrl)) {
$effectiveDbUrl = $env:DATABASE_URL
}
if (-not [string]::IsNullOrWhiteSpace($DatabaseUrl)) {
$env:DATABASE_URL = $DatabaseUrl
Write-Host "[env] DATABASE_URL overridden from -DatabaseUrl parameter." -ForegroundColor DarkGray
}
$pythonCmd = Get-PythonCommand
$doInstall = -not $SkipInstall.IsPresent
Write-Host "== HXOPS local startup ==" -ForegroundColor Green
Write-Host "mode: $DbMode" -ForegroundColor Green
if (-not $SkipWorker.IsPresent) {
Start-KbWorkerWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $DatabaseUrl
Start-ShippingWorkerWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $DatabaseUrl
Write-Host "kb concurrency: $KbConcurrency | shipping concurrency: $ShippingConcurrency" -ForegroundColor Green
}
if ($DbMode -eq "postgres") {
if (-not [string]::IsNullOrWhiteSpace($effectiveDbUrl)) {
$masked = $effectiveDbUrl -replace '(?<=://[^:]+:)[^@]+', '******'
Write-Host "postgres DATABASE_URL: $masked" -ForegroundColor DarkGray
}
else {
Write-Warning "DbMode=postgres but no DATABASE_URL found in env files or -DatabaseUrl parameter. Relying on app default."
}
}
Stop-LocalHxopsStack -RepoRoot $repoRoot -BackendPath $backendDir -FrontendPath $frontendDir
Ensure-Backend -BackendPath $backendDir -PyCmd $pythonCmd -Mode $DbMode -DbUrl $effectiveDbUrl -DoInstall $doInstall
if (-not $SkipMigrate.IsPresent) {
Invoke-DbMigrate -BackendPath $backendDir -Mode $DbMode -DbUrl $effectiveDbUrl
}
else {
Write-Host "[db-migrate] skipped (-SkipMigrate)." -ForegroundColor DarkGray
}
Ensure-Frontend -FrontendPath $frontendDir -DoInstall $doInstall
$windowCount = 2
Start-BackendWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $effectiveDbUrl
if (-not $SkipWorker.IsPresent) {
Start-KbWorkerWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $effectiveDbUrl -Concurrency $KbConcurrency
Start-ShippingWorkerWindow -BackendPath $backendDir -Mode $DbMode -DbUrl $effectiveDbUrl -Concurrency $ShippingConcurrency
$windowCount += 2
if (-not $SkipRpaWorker.IsPresent) {
if ($IncludeRpaWorker.IsPresent) {
Start-FreightRpaWorkerWindow -RepoRoot $repoRoot -BackendPath $backendDir -EnvFiles $envFiles
$windowCount += 1
}
@@ -398,7 +518,8 @@ $windowCount += 1
Write-Host ""
Write-Host "Started in $windowCount new PowerShell windows." -ForegroundColor Green
if (-not $SkipWorker.IsPresent) {
Write-Host "Workers: kb-worker, shipping-worker$(if (-not $SkipRpaWorker.IsPresent) { ', freight-rpa-worker (mock)' })" -ForegroundColor Green
Write-Host "Workers: kb-worker(x$KbConcurrency), shipping-worker(x$ShippingConcurrency)$(if ($IncludeRpaWorker.IsPresent) { ', freight-rpa-worker (mock)' })" -ForegroundColor Green
Write-Host "(Worker 进程由 master 进程内部通过 multiprocessing fork,每个 Worker 窗口只需要一个 master。)" -ForegroundColor DarkGray
Write-Host "Schedule worker runs inside backend process (SCHEDULE_WORKER_ENABLED)." -ForegroundColor DarkGray
}
Write-Host "Frontend URL: http://127.0.0.1:8881" -ForegroundColor Green