Added Retries
This commit is contained in:
18
Worker.cs
18
Worker.cs
@@ -6,6 +6,9 @@ using OneDriveBackupService.OneDrive;
|
||||
namespace OneDriveBackupService;
|
||||
|
||||
public class Worker(ILogger<Worker> 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<Worker> 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<Worker> logger, ConfigData config, OneDriveClient cl
|
||||
var file = await CreateBackupArchive(optimalTime, stoppingToken);
|
||||
if (string.IsNullOrEmpty(file)) {
|
||||
logger.LogError("Backup archive creation failed");
|
||||
_hasErrors = true;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user