# Legacy Windows compatibility only. # NSSM supervises the single Python scheduler process; this script never creates # Windows Task Scheduler entries and does not own business schedule rules. param( [string]$ProjectRoot = (Resolve-Path (Join-Path $PSScriptRoot "..\..")).Path, [string]$DataRoot = "", [string]$RuntimeEnvFile = "D:\product-collector-analyze-flow\.env", [string]$JushuitanEnvFile = "D:\product-collector-analyze-flow\private-secrets\jushuitan.env", [string]$ServiceName = "gyxx-flow-scheduler", [string]$NssmPath = "nssm.exe" ) $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest $project = (Resolve-Path -LiteralPath $ProjectRoot).Path $runtimeEnvPath = (Resolve-Path -LiteralPath $RuntimeEnvFile -ErrorAction Stop).Path $runtimeEnvPaths = @($runtimeEnvPath) if (Test-Path -LiteralPath $JushuitanEnvFile -PathType Leaf) { $runtimeEnvPaths += (Resolve-Path -LiteralPath $JushuitanEnvFile -ErrorAction Stop).Path } else { Write-Warning "Jushuitan environment file not found; Jushuitan workflows will fail safely until it is provisioned." } if ([string]::IsNullOrWhiteSpace($DataRoot)) { $DataRoot = Join-Path $project "var" } $data = [IO.Path]::GetFullPath($DataRoot) $python = Join-Path $project ".venv\Scripts\python.exe" if (-not (Test-Path -LiteralPath $python -PathType Leaf)) { throw "Python environment not found: $python" } $nssm = (Get-Command $NssmPath -ErrorAction Stop).Source $serviceLogRoot = Join-Path $data "logs\scheduler-service" New-Item -ItemType Directory -Path $serviceLogRoot -Force | Out-Null function Invoke-Nssm([string[]]$Arguments) { & $nssm @Arguments if ($LASTEXITCODE -ne 0) { throw "NSSM failed with exit code $LASTEXITCODE" } } Invoke-Nssm -Arguments @("install", $ServiceName, $python) Invoke-Nssm -Arguments @("set", $ServiceName, "AppDirectory", $project) $envFileArguments = $runtimeEnvPaths | ForEach-Object { '--env-file "' + $_ + '"' } $schedulerArguments = '-m gyxx_flow schedule run ' + ($envFileArguments -join ' ') Invoke-Nssm -Arguments @("set", $ServiceName, "AppParameters", $schedulerArguments) Invoke-Nssm -Arguments @( "set", $ServiceName, "AppEnvironmentExtra", "GYXX_PROJECT_ROOT=$project", "GYXX_DATA_ROOT=$data", "PYTHONUTF8=1" ) Invoke-Nssm -Arguments @("set", $ServiceName, "AppStdout", (Join-Path $serviceLogRoot "service.log")) Invoke-Nssm -Arguments @("set", $ServiceName, "AppStderr", (Join-Path $serviceLogRoot "service-error.log")) Invoke-Nssm -Arguments @("set", $ServiceName, "AppRotateFiles", "1") Invoke-Nssm -Arguments @("set", $ServiceName, "AppRotateBytes", "10485760") Invoke-Nssm -Arguments @("set", $ServiceName, "AppExit", "Default", "Restart") Invoke-Nssm -Arguments @("set", $ServiceName, "Start", "SERVICE_AUTO_START") Write-Host "Service installed: $ServiceName" Write-Host "Before starting it, set the Log On account and production environment as documented." Write-Host "Start with: nssm start $ServiceName"