attempt fix
This commit is contained in:
@@ -17,7 +17,14 @@ param(
|
||||
)
|
||||
|
||||
$ErrorActionPreference = 'Stop'
|
||||
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $Host.Runspace
|
||||
try {
|
||||
if ($Host -and $Host.Runspace) {
|
||||
[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace = $Host.Runspace
|
||||
}
|
||||
}
|
||||
catch {
|
||||
# Ignore runspace assignment errors - not critical for non-interactive execution
|
||||
}
|
||||
|
||||
# region Path setup
|
||||
$workerRoot = Join-Path -Path $DataRoot -ChildPath $WorkerType
|
||||
@@ -36,14 +43,36 @@ $payloadPath = Join-Path -Path $stateRoot -ChildPath "payload.ps1"
|
||||
# endregion
|
||||
|
||||
# region Logging
|
||||
$logStream = [System.IO.FileStream]::new(
|
||||
$logPath,
|
||||
[System.IO.FileMode]::Append,
|
||||
[System.IO.FileAccess]::Write,
|
||||
[System.IO.FileShare]::ReadWrite
|
||||
)
|
||||
$logWriter = [System.IO.StreamWriter]::new($logStream, [System.Text.Encoding]::UTF8)
|
||||
$logWriter.AutoFlush = $true
|
||||
try {
|
||||
$logStream = [System.IO.FileStream]::new(
|
||||
$logPath,
|
||||
[System.IO.FileMode]::Append,
|
||||
[System.IO.FileAccess]::Write,
|
||||
[System.IO.FileShare]::ReadWrite
|
||||
)
|
||||
$logWriter = [System.IO.StreamWriter]::new($logStream, [System.Text.Encoding]::UTF8)
|
||||
$logWriter.AutoFlush = $true
|
||||
}
|
||||
catch {
|
||||
# If we can't open the log file, write error to metadata and exit
|
||||
$errorMeta = [pscustomobject]@{
|
||||
WorkerName = $WorkerName
|
||||
WorkerType = $WorkerType
|
||||
Status = 'error'
|
||||
ControllerPid = $PID
|
||||
WorkerPid = $null
|
||||
Restarts = 0
|
||||
LastExitCode = 1
|
||||
LogPath = $logPath
|
||||
CommandPath = $commandPath
|
||||
PayloadPath = $payloadPath
|
||||
UpdatedAtUtc = (Get-Date).ToUniversalTime()
|
||||
ErrorMessage = "Failed to open log file: $($_.Exception.Message)"
|
||||
}
|
||||
$errorMeta | ConvertTo-Json -Depth 5 | Set-Content -Path $metaPath -Encoding UTF8 -ErrorAction SilentlyContinue
|
||||
Write-Error "Controller failed to initialize: $($_.Exception.Message)"
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Create C# event handler class that doesn't require PowerShell runspace
|
||||
if (-not ("UnifiedWorkers.ProcessLogHandler" -as [type])) {
|
||||
@@ -253,18 +282,52 @@ try {
|
||||
Write-ControllerLog "Worker process started with PID $($workerProcess.Id)"
|
||||
Write-Metadata -Status 'running' -WorkerPid $workerProcess.Id -ControllerPid $controllerPid -Restarts $restartCount
|
||||
|
||||
$stdoutHandler = [UnifiedWorkers.ProcessLogHandler]::new($logWriter, 'OUT')
|
||||
$stderrHandler = [UnifiedWorkers.ProcessLogHandler]::new($logWriter, 'ERR')
|
||||
|
||||
$outputHandler = [System.Diagnostics.DataReceivedEventHandler]$stdoutHandler.OnDataReceived
|
||||
$errorHandler = [System.Diagnostics.DataReceivedEventHandler]$stderrHandler.OnDataReceived
|
||||
# Check if process exited immediately
|
||||
if ($workerProcess.HasExited) {
|
||||
$exitCode = -1
|
||||
try {
|
||||
$exitCode = $workerProcess.ExitCode
|
||||
}
|
||||
catch {
|
||||
Write-ControllerLog "Unable to read immediate exit code: $($_.Exception.Message)"
|
||||
}
|
||||
Write-ControllerLog "Worker process exited immediately after start with code $exitCode"
|
||||
Write-Metadata -Status 'stopped' -WorkerPid $null -ControllerPid $controllerPid -Restarts $restartCount -LastExitCode $exitCode
|
||||
if ($exitCode -eq 0) { break }
|
||||
# Continue to restart logic below
|
||||
}
|
||||
else {
|
||||
$stdoutHandler = [UnifiedWorkers.ProcessLogHandler]::new($logWriter, 'OUT')
|
||||
$stderrHandler = [UnifiedWorkers.ProcessLogHandler]::new($logWriter, 'ERR')
|
||||
|
||||
$outputHandler = [System.Diagnostics.DataReceivedEventHandler]$stdoutHandler.OnDataReceived
|
||||
$errorHandler = [System.Diagnostics.DataReceivedEventHandler]$stderrHandler.OnDataReceived
|
||||
|
||||
$workerProcess.add_OutputDataReceived($outputHandler)
|
||||
$workerProcess.add_ErrorDataReceived($errorHandler)
|
||||
$workerProcess.BeginOutputReadLine()
|
||||
$workerProcess.BeginErrorReadLine()
|
||||
$workerProcess.add_OutputDataReceived($outputHandler)
|
||||
$workerProcess.add_ErrorDataReceived($errorHandler)
|
||||
$workerProcess.BeginOutputReadLine()
|
||||
$workerProcess.BeginErrorReadLine()
|
||||
Write-ControllerLog "Output handlers set up successfully"
|
||||
|
||||
while (-not $workerProcess.HasExited) {
|
||||
# Give process a moment to start, then check again
|
||||
Start-Sleep -Milliseconds 200
|
||||
if ($workerProcess.HasExited) {
|
||||
$exitCode = -1
|
||||
try {
|
||||
$exitCode = $workerProcess.ExitCode
|
||||
}
|
||||
catch {
|
||||
Write-ControllerLog "Unable to read exit code after delay: $($_.Exception.Message)"
|
||||
}
|
||||
Write-ControllerLog "Worker process exited after 200ms delay with code $exitCode"
|
||||
Write-Metadata -Status 'stopped' -WorkerPid $null -ControllerPid $controllerPid -Restarts $restartCount -LastExitCode $exitCode
|
||||
if ($exitCode -eq 0) { break }
|
||||
# Continue to restart logic below
|
||||
}
|
||||
else {
|
||||
Write-ControllerLog "Worker process is running, entering monitoring loop"
|
||||
|
||||
while (-not $workerProcess.HasExited) {
|
||||
$commands = Get-PendingCommands
|
||||
foreach ($command in $commands) {
|
||||
$trimmed = $command.Trim()
|
||||
@@ -284,10 +347,24 @@ try {
|
||||
}
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500
|
||||
Start-Sleep -Milliseconds 500
|
||||
}
|
||||
# End of monitoring loop - process has exited
|
||||
Write-ControllerLog "Worker process exited, exiting monitoring loop"
|
||||
}
|
||||
}
|
||||
|
||||
$exitCode = $workerProcess.ExitCode
|
||||
# Wait for process to fully exit before reading exit code
|
||||
$workerProcess.WaitForExit(1000)
|
||||
|
||||
$exitCode = -1
|
||||
try {
|
||||
$exitCode = $workerProcess.ExitCode
|
||||
}
|
||||
catch {
|
||||
Write-ControllerLog "Unable to read worker exit code: $($_.Exception.Message)"
|
||||
}
|
||||
|
||||
Write-ControllerLog "Worker exited with code $exitCode"
|
||||
Write-Metadata -Status 'stopped' -WorkerPid $null -ControllerPid $controllerPid -Restarts $restartCount -LastExitCode $exitCode
|
||||
|
||||
|
||||
Reference in New Issue
Block a user