From f637c539306acec5dc21fad7758b8144bd71f1c3 Mon Sep 17 00:00:00 2001 From: Wendell Jones Date: Sun, 26 Apr 2026 14:06:45 -0400 Subject: [PATCH] Enhance sync_data.ps1: add shutdown option for failed jobs and improve error handling Co-authored-by: Copilot --- personal/cron_scripts/sync_data.ps1 | 30 ++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/personal/cron_scripts/sync_data.ps1 b/personal/cron_scripts/sync_data.ps1 index df6dd4c..bc41a74 100644 --- a/personal/cron_scripts/sync_data.ps1 +++ b/personal/cron_scripts/sync_data.ps1 @@ -1,5 +1,11 @@ #Requires -Version 5.1 + +# Use -ShutdownRegardless to force shutdown even if one or more sync jobs fail. +param( + [switch]$ShutdownRegardless +) + $FfsExe = 'C:\Program Files\FreeFileSync\FreeFileSync.exe' $ScriptDir = $PSScriptRoot $LogDir = Join-Path $ScriptDir 'logs' @@ -30,6 +36,9 @@ Write-Log 'Sync run started' Write-Host "Log file: $LogFile" # ── Run sync jobs ────────────────────────────────────────────────────────────── +$HadFatalError = $false +$FatalExitCodes = @() + foreach ($job in $SyncJobs) { $jobName = Split-Path $job -Leaf Write-Host '' @@ -48,7 +57,9 @@ foreach ($job in $SyncJobs) { 1 { Write-Log 'Synchronization completed with warnings.' } default { Write-Log "Synchronization failed or was aborted. Exit code: $rc" - exit $rc + $HadFatalError = $true + $FatalExitCodes += $rc + continue } } } @@ -64,8 +75,8 @@ $logPaths = Select-String -Path $LogFile -Pattern '"logFile"\s*:\s*"([^"]+)"' | $copied = 0 foreach ($path in $logPaths) { if ([IO.Path]::GetExtension($path) -ieq '.html') { - if (Test-Path $path) { - Copy-Item -Path $path -Destination $ReportDir -Force + if (Test-Path -LiteralPath $path) { + Copy-Item -LiteralPath $path -Destination $ReportDir -Force Write-Log "Copied report: $(Split-Path $path -Leaf)" $copied++ } else { @@ -77,5 +88,18 @@ foreach ($path in $logPaths) { Write-Log "Total reports copied: $copied" # ── Shutdown ─────────────────────────────────────────────────────────────────── +if ($HadFatalError) { + $codes = ($FatalExitCodes | Sort-Object -Unique) -join ', ' + Write-Log "One or more synchronization jobs failed. Fatal exit code(s): $codes" + + if (-not $ShutdownRegardless) { + Write-Log 'Skipping shutdown because not all synchronization jobs completed successfully.' + Write-Log 'Use -ShutdownRegardless to force shutdown even when jobs fail.' + exit ($FatalExitCodes | Select-Object -Last 1) + } + + Write-Log 'Forced shutdown enabled. Continuing to shutdown despite synchronization failures.' +} + Write-Log 'Shutting down server now.' Stop-Computer -Force