diff --git a/Worker.cs b/Worker.cs index b890297..ea65372 100644 --- a/Worker.cs +++ b/Worker.cs @@ -6,6 +6,9 @@ using OneDriveBackupService.OneDrive; namespace OneDriveBackupService; public class Worker(ILogger logger, ConfigData config, OneDriveClient client) : BackgroundService { + + private bool _hasErrors = false; + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { if (Environment.GetCommandLineArgs().Contains("--run-once")) { logger.LogInformation("Manual backup triggered"); @@ -34,7 +37,21 @@ public class Worker(ILogger logger, ConfigData config, OneDriveClient cl logger.LogInformation("Next backup: {time}", nextRun.ToString("f")); await Task.Delay(delay, stoppingToken); + _hasErrors = false; await RunBackup(nextRun, stoppingToken); + + if (_hasErrors) { + for (var i = 0; i < 3 && _hasErrors; i++) { + logger.LogInformation("Backup failed, trying again in 5 minutes"); + await Task.Delay(TimeSpan.FromMinutes(5), stoppingToken); + _hasErrors = false; + await RunBackup(nextRun, stoppingToken); + } + + if (_hasErrors) { + logger.LogCritical("Backup still failing, skipping retries"); + } + } } } @@ -44,6 +61,7 @@ public class Worker(ILogger logger, ConfigData config, OneDriveClient cl var file = await CreateBackupArchive(optimalTime, stoppingToken); if (string.IsNullOrEmpty(file)) { logger.LogError("Backup archive creation failed"); + _hasErrors = true; return; }