sheepit/flamenco toggle

This commit is contained in:
Nathan
2025-12-17 16:46:35 -07:00
parent 0460865ebc
commit 553758c378
7 changed files with 16425 additions and 44 deletions

View File

@@ -5,14 +5,41 @@ param(
[Parameter(Mandatory = $true)]
[string]$WorkerType,
[string]$DataRoot = (Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'UnifiedWorkers'),
[string]$DataRoot = (Join-Path ([Environment]::GetFolderPath('LocalApplicationData')) 'UnifiedWorkers'),
[switch]$CommandOnly,
[string]$Command
)
$ErrorActionPreference = 'Stop'
$ErrorActionPreference = 'Continue'
# Ensure we can see output immediately
try {
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
} catch {}
try {
$Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(200, 9999)
} catch {}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Worker Attach Session Starting" -ForegroundColor Cyan
Write-Host "Worker: $WorkerName" -ForegroundColor White
Write-Host "Type: $WorkerType" -ForegroundColor White
Write-Host "========================================" -ForegroundColor Cyan
[Console]::Out.Flush()
# Ensure output is visible immediately
try {
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
} catch {}
try {
$Host.UI.RawUI.BufferSize = New-Object System.Management.Automation.Host.Size(200, 9999)
} catch {}
Write-Host "Starting attach session..." -ForegroundColor Green
Write-Host "Worker: $WorkerName, Type: $WorkerType" -ForegroundColor Gray
[Console]::Out.Flush()
function Get-WorkerPaths {
param([string]$Root, [string]$Type, [string]$Name)
@@ -28,7 +55,17 @@ function Get-WorkerPaths {
$paths = Get-WorkerPaths -Root $DataRoot -Type $WorkerType -Name $WorkerName
if (-not (Test-Path $paths.Metadata)) {
Write-Host "No worker metadata found for $WorkerName ($WorkerType)." -ForegroundColor Red
Write-Host ""
Write-Host "ERROR: No worker metadata found for $WorkerName ($WorkerType)." -ForegroundColor Red
Write-Host "Metadata path: $($paths.Metadata)" -ForegroundColor Gray
Write-Host "Data root: $DataRoot" -ForegroundColor Gray
Write-Host ""
Write-Host "Press any key to exit..."
try {
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} catch {
Start-Sleep -Seconds 5
}
exit 1
}
@@ -73,16 +110,58 @@ if ($CommandOnly) {
exit 0
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "Attaching to $WorkerName ($WorkerType) logs." -ForegroundColor Cyan
Write-Host "Type commands and press Enter. Type 'detach' to exit session." -ForegroundColor Yellow
Write-Host "Log file: $($paths.Log)" -ForegroundColor Gray
Write-Host "========================================" -ForegroundColor Cyan
[Console]::Out.Flush()
[Console]::Error.WriteLine("Attach session initialized")
# Show initial log content if available
if (Test-Path $paths.Log) {
$initialLines = Get-Content -Path $paths.Log -Tail 50 -ErrorAction SilentlyContinue
if ($initialLines) {
Write-Host "--- Recent log output ---" -ForegroundColor DarkGray
$initialLines | ForEach-Object { Write-Host $_ }
Write-Host "--- End of initial output ---" -ForegroundColor DarkGray
} else {
Write-Host "Log file exists but is empty. Waiting for worker output..." -ForegroundColor Yellow
}
} else {
Write-Host "Log file does not exist yet. Waiting for worker to start..." -ForegroundColor Yellow
}
Write-Host ""
Write-Host "Type commands and press Enter. Type 'detach' to exit session." -ForegroundColor Yellow
Write-Host ""
# Use Register-ObjectEvent for file system watcher, or simpler: just tail in background and poll
$logJob = Start-Job -ScriptBlock {
param($LogPath)
Get-Content -Path $LogPath -Tail 50 -Wait
if (Test-Path $LogPath) {
Get-Content -Path $LogPath -Tail 0 -Wait -ErrorAction SilentlyContinue
} else {
while (-not (Test-Path $LogPath)) {
Start-Sleep -Milliseconds 500
}
Get-Content -Path $LogPath -Tail 0 -Wait -ErrorAction SilentlyContinue
}
} -ArgumentList $paths.Log
try {
while ($true) {
# Check for new log output from background job (non-blocking)
$logOutput = Receive-Job -Job $logJob -ErrorAction SilentlyContinue
if ($logOutput) {
$logOutput | ForEach-Object { Write-Host $_ }
[Console]::Out.Flush()
}
# Use a timeout on Read-Host to allow periodic log checking
# Since Read-Host doesn't support timeout, we'll just check logs before each Read-Host
# This means logs will be checked whenever user presses Enter
$input = Read-Host "> "
if ($null -eq $input) {
continue
@@ -104,10 +183,14 @@ try {
Send-WorkerCommand -Value $input
}
}
catch {
Write-Host "Error in attach session: $($_.Exception.Message)" -ForegroundColor Red
Write-Host $_.ScriptStackTrace -ForegroundColor DarkRed
[Console]::Error.WriteLine("Attach error: $($_.Exception.Message)")
}
finally {
if ($logJob) {
Stop-Job -Job $logJob -ErrorAction SilentlyContinue
Receive-Job -Job $logJob -ErrorAction SilentlyContinue | Out-Null
Remove-Job -Job $logJob -ErrorAction SilentlyContinue
}