Switched to dedicated sync service

This commit is contained in:
2026-01-17 21:28:57 +01:00
parent 19c27c8ef0
commit 70dd1358ca
10 changed files with 87 additions and 142 deletions

View File

@@ -1,15 +1,16 @@
using System.Diagnostics;
using Cronos;
using OneDriveBackupService.Models;
using OneDriveBackupService.OneDrive;
namespace OneDriveBackupService;
public class Worker(ILogger<Worker> logger, ConfigData config, OneDriveClient client) : BackgroundService {
protected override async Task ExecuteAsync(CancellationToken stoppingToken) {
await client.EnsureAuthenticated(stoppingToken);
if (Environment.GetCommandLineArgs().Contains("--run-once")) {
logger.LogInformation("Manual backup triggered");
await RunBackup(DateTime.Now, stoppingToken);
Environment.Exit(0);
return;
}
@@ -25,12 +26,12 @@ public class Worker(ILogger<Worker> logger, ConfigData config, OneDriveClient cl
delay = TimeSpan.Zero;
if (!cronTime.HasValue) {
logger.LogError("Cron expression falied, falling back to default delay");
logger.LogError("Cron expression failed, falling back to default delay");
delay = TimeSpan.FromHours(12);
}
var nextRun = DateTime.Now + delay;
logger.LogInformation("Next backup run: {time}", nextRun.ToString("f"));
logger.LogInformation("Next backup: {time}", nextRun.ToString("f"));
await Task.Delay(delay, stoppingToken);
await RunBackup(nextRun, stoppingToken);
@@ -45,20 +46,10 @@ public class Worker(ILogger<Worker> logger, ConfigData config, OneDriveClient cl
logger.LogError("Backup archive creation failed");
return;
}
logger.LogInformation("Backup archive created, starting upload...");
var uploadResult = await client.UploadFile(file, stoppingToken);
if (!uploadResult.UploadSucceeded) {
logger.LogError("Upload failed");
File.Delete(file);
return;
}
logger.LogInformation("Upload completed");
client.UploadFile(file);
File.Delete(file);
var count = await client.DeleteOldFiles(stoppingToken);
var count = client.DeleteOldFiles();
logger.LogInformation("Deleted {count} old backups", count);
logger.LogInformation("Backup completed");
@@ -77,7 +68,7 @@ public class Worker(ILogger<Worker> logger, ConfigData config, OneDriveClient cl
"-C",
config.LocalRoot,
"-T",
Path.GetRelativePath(config.LocalRoot, config.IncludeFile)
Path.Combine(config.LocalRoot, config.IncludeFile)
},
RedirectStandardOutput = true,
RedirectStandardError = true
@@ -88,6 +79,9 @@ public class Worker(ILogger<Worker> logger, ConfigData config, OneDriveClient cl
await process.WaitForExitAsync(stoppingToken);
if (process.ExitCode != 0) {
var error = await process.StandardError.ReadToEndAsync(stoppingToken);
logger.LogError(error);
return string.Empty;
}