PayrollManagementSystem/scripts/windows/Run-PayrollHost.ps1

36 lines
2.0 KiB
PowerShell
Raw Normal View History

[CmdletBinding()]
param(
[Parameter(Mandatory = $true)][ValidateSet('backend', 'worker', 'maintenance')][string]$Role,
[Parameter(Mandatory = $true)][string]$ProjectRoot,
[Parameter(Mandatory = $true)][string]$PythonPath,
[Parameter(Mandatory = $true)][string]$RuntimeDirectory,
[Parameter(Mandatory = $true)][string]$ConnectionFile,
[int]$ApiPort = 8000
)
. (Join-Path $PSScriptRoot 'TaskHost.Common.ps1')
$plan = Get-PayrollTaskPlan -ProjectRoot $ProjectRoot -PythonPath $PythonPath -RuntimeDirectory $RuntimeDirectory `
-ConnectionFile $ConnectionFile -TaskPath '\PayrollManagementSystem\' -ApiPort $ApiPort
if (Test-Path -LiteralPath $plan.StopFile) { exit 0 }
$env:T_PLUS_STOP_FILE = $plan.StopFile
$env:T_PLUS_ENABLED = 'true'
$env:T_PLUS_CONNECTION_FILE = $plan.ConnectionFile
$logs = Join-Path $plan.RuntimeDirectory 'logs'
New-Item -ItemType Directory -Path $logs -Force | Out-Null
$stamp = (Get-Date -Format 'yyyyMMdd-HHmmss') + '-' + [guid]::NewGuid().ToString('N').Substring(0,8)
$arguments = switch ($Role) {
'backend' { '-m uvicorn app.main:app --host 127.0.0.1 --port ' + $ApiPort }
'worker' { '-m app.cli tplus-worker --watch --connection-file ' + (Quote-TaskArgument $plan.ConnectionFile) + ' --stop-file ' + (Quote-TaskArgument $plan.StopFile) }
'maintenance' { '-m app.cli tplus-maintain --once --connection-file ' + (Quote-TaskArgument $plan.ConnectionFile) + ' --stop-file ' + (Quote-TaskArgument $plan.StopFile) }
}
$process = Start-Process -FilePath $plan.PythonPath -ArgumentList $arguments -WorkingDirectory (Join-Path $plan.ProjectRoot 'backend') `
-WindowStyle Hidden -PassThru -RedirectStandardOutput (Join-Path $logs "$Role-$stamp.out.log") `
-RedirectStandardError (Join-Path $logs "$Role-$stamp.err.log")
$processHandle = $process.Handle
while (-not $process.HasExited) {
if ($Role -eq 'backend' -and (Test-Path -LiteralPath $plan.StopFile)) { Stop-Process -Id $process.Id -ErrorAction Stop }
Start-Sleep -Seconds 1
$process.Refresh()
}
$process.WaitForExit()
exit $process.ExitCode