From 4a719b6c9a6bac2696e273fa09e938f2c54afab8 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Thu, 10 Jul 2025 21:59:19 -0700 Subject: [PATCH] Improves reliability of build log monitoring Deletes the old log file before starting a new build job to prevent the UI from displaying stale content from a previous run. Replaces a fixed delay with a more robust wait loop that polls for the new log file's creation. This avoids a race condition where the monitor could fail to attach if the background job was slow to start. A timeout is included to prevent the UI from hanging. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index 76f6410..18bdf42 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -159,19 +159,31 @@ $script:uiState.Controls.btnRun.Add_Click({ & "$PSScriptRoot\BuildFFUVM.ps1" @buildParams } + # Delete the old log file before starting the build job to ensure we don't read stale content. + $mainLogPath = Join-Path $config.FFUDevelopmentPath "FFUDevelopment.log" + if (Test-Path $mainLogPath) { + WriteLog "Removing old FFUDevelopment.log file." + Remove-Item -Path $mainLogPath -Force + } + # Start the job and store it in the shared state object $script:uiState.Data.currentBuildJob = Start-Job -ScriptBlock $scriptBlock -ArgumentList @($buildParams, $PSScriptRoot) + # Wait for the new log file to be created by the background job. + $logWaitTimeout = 15 # seconds + $watch = [System.Diagnostics.Stopwatch]::StartNew() + while (-not (Test-Path $mainLogPath) -and $watch.Elapsed.TotalSeconds -lt $logWaitTimeout) { + Start-Sleep -Milliseconds 250 + } + $watch.Stop() + # Open a stream reader to the main log file - $mainLogPath = "$($config.FFUDevelopmentPath)\FFUDevelopment.log" - # Wait a moment for the file to be created by the new process - Start-Sleep -Seconds 1 if (Test-Path $mainLogPath) { $fileStream = [System.IO.File]::Open($mainLogPath, 'Open', 'Read', 'ReadWrite') $script:uiState.Data.logStreamReader = [System.IO.StreamReader]::new($fileStream) } else { - WriteLog "Warning: Main log file not found at $mainLogPath. Monitor tab will not update." + WriteLog "Warning: Main log file not found at $mainLogPath after waiting. Monitor tab will not update." } # Create a timer to poll the job status from the UI thread