From 87ebfe88b667b2cf9cd7d7031fb6e20f815d00c6 Mon Sep 17 00:00:00 2001 From: Kyle Ebbinga <26614720+TheGuitarleader@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:33:38 -0500 Subject: [PATCH 1/2] Enhance documentation for file syncing and restoration in `ISyncManager` interface and `BaseSyncManager` class. Add detailed XML comments to clarify method purposes and parameters. --- Parallel.Core/IO/Syncing/BaseSyncManager.cs | 4 +-- Parallel.Core/IO/Syncing/ISyncManager.cs | 30 +++++++++++++-------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/Parallel.Core/IO/Syncing/BaseSyncManager.cs b/Parallel.Core/IO/Syncing/BaseSyncManager.cs index a9ed5bb..6d6faa8 100644 --- a/Parallel.Core/IO/Syncing/BaseSyncManager.cs +++ b/Parallel.Core/IO/Syncing/BaseSyncManager.cs @@ -36,9 +36,8 @@ public abstract class BaseSyncManager : ISyncManager public IStorageProvider StorageProvider { get; set; } /// - /// + /// Represents the base functionality for syncing files to an associated file system. /// - /// protected BaseSyncManager(LocalVaultConfig localVault) { LocalVault = localVault; @@ -134,6 +133,7 @@ public async Task DisconnectAsync() /// public abstract Task PruneFilesAsync(IReadOnlyList files, IProgressReporter progress); + /// public abstract Task ScrubFilesAsync(IReadOnlyList files, IProgressReporter progress); } } \ No newline at end of file diff --git a/Parallel.Core/IO/Syncing/ISyncManager.cs b/Parallel.Core/IO/Syncing/ISyncManager.cs index cb09cc4..8c57c12 100644 --- a/Parallel.Core/IO/Syncing/ISyncManager.cs +++ b/Parallel.Core/IO/Syncing/ISyncManager.cs @@ -49,28 +49,36 @@ public interface ISyncManager Task DisconnectAsync(); /// - /// Backs up an array of files to a vault. + /// Synchronizes a list of local files with the remote storage. Files will be uploaded if they differ from their remote counterpart or if they do not exist in the remote storage. /// - /// - /// - /// + /// A read-only list of local files to be considered for synchronization. + /// An instance to report the progress of the sync operation. + /// A boolean flag indicating whether existing files on the remote storage should be overwritten. + /// Returns the number of files that were successfully synchronized. Task SyncFilesAsync(IReadOnlyList files, IProgressReporter progress, bool overwrite); /// - /// Restores an array of files from a vault. + /// Restores the specified list of local files from a remote storage, updating the progress through the provided progress reporter. /// - /// - /// + /// A read-only list of local files to be considered for restoration. + /// An instance to report the progress of the restore operation. + /// Returns the number of files that were successfully restored. Task RestoreFilesAsync(IReadOnlyList files, IProgressReporter progress); /// - /// Deletes files from a vault. + /// Removes unnecessary or transient files from the provided list of local files to clean up storage, retaining only essential data. /// - /// - /// - /// + /// A read-only list of local files to be considered for pruning. + /// An instance to report the progress of the pruning operation. + /// Returns the number of files that were successfully pruned. Task PruneFilesAsync(IReadOnlyList files, IProgressReporter progress); + /// + /// Scrubs the specified files to ensure they are in a valid state. + /// + /// A read-only list of local files to be considered for scrubbing. + /// An instance to report the progress of the scrubbing operation. + /// Returns the number of files that were successfully scrubbed. Task ScrubFilesAsync(IReadOnlyList files, IProgressReporter progress); } } \ No newline at end of file From eaf93244f9f90ccd72b25d60b1b25c2fbfef580e Mon Sep 17 00:00:00 2001 From: Kyle Ebbinga <26614720+TheGuitarleader@users.noreply.github.com> Date: Thu, 26 Nov 2026 21:19:08 -0600 Subject: [PATCH 2/2] Add watcher-based vault sync service Extract shared vault lifecycle handling into BaseSyncService and register WatcherSyncService to monitor configured directories. Also use structured logging for task queue starts. --- .../{SyncService.cs => BaseSyncService.cs} | 37 +++------- Parallel.Cli/Services/RunCommand.cs | 9 ++- Parallel.Cli/Services/WatcherSyncService.cs | 74 +++++++++++++++++++ Parallel.Core/Tasks/TaskQueuer.cs | 2 +- 4 files changed, 94 insertions(+), 28 deletions(-) rename Parallel.Cli/Services/{SyncService.cs => BaseSyncService.cs} (60%) create mode 100644 Parallel.Cli/Services/WatcherSyncService.cs diff --git a/Parallel.Cli/Services/SyncService.cs b/Parallel.Cli/Services/BaseSyncService.cs similarity index 60% rename from Parallel.Cli/Services/SyncService.cs rename to Parallel.Cli/Services/BaseSyncService.cs index a3ed9a2..85c4bde 100644 --- a/Parallel.Cli/Services/SyncService.cs +++ b/Parallel.Cli/Services/BaseSyncService.cs @@ -21,13 +21,14 @@ public SyncWorker(Task task, CancellationTokenSource cts) } } - public class SyncService : BackgroundService + public abstract class BaseSyncService : BackgroundService { - private readonly ConcurrentDictionary _vaults = new(); - private readonly ILogger _logger; - private readonly TaskQueuer _queuer; + private readonly ILogger _logger; - public SyncService(ILogger logger, TaskQueuer queuer) + protected readonly ConcurrentDictionary Vaults = new(); + protected readonly TaskQueuer _queuer; + + public BaseSyncService(ILogger logger, TaskQueuer queuer) { _logger = logger; _queuer = queuer; @@ -39,19 +40,19 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) { LocalVaultConfig[] enabledVaults = ParallelConfig.GetEnabledVaults(); string[] enabledVaultIds = enabledVaults.Select(v => v.Id).ToArray(); - string[] disabledVaults = _vaults.Keys.Where(v => !enabledVaultIds.Contains(v)).ToArray(); + string[] disabledVaults = Vaults.Keys.Where(v => !enabledVaultIds.Contains(v)).ToArray(); // Adds newly enabled vaults to be synced. foreach (LocalVaultConfig vault in enabledVaults) { - if (_vaults.ContainsKey(vault.Id)) continue; + if (Vaults.ContainsKey(vault.Id)) continue; _logger.LogDebug("Adding vault to syncing..."); CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken); Task workerTask = SyncVaultAsync(new FileSyncManager(vault), cts.Token); SyncWorker worker = new SyncWorker(workerTask, cts); - if (!_vaults.TryAdd(vault.Id, worker)) continue; + if (!Vaults.TryAdd(vault.Id, worker)) continue; _logger.LogInformation("Added vault to be synced: {VaultId}", vault.Id); } @@ -59,10 +60,10 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) foreach (string id in disabledVaults) { _logger.LogDebug("Removing vault from syncing..."); - if (!_vaults.TryGetValue(id, out SyncWorker? worker)) continue; + if (!Vaults.TryGetValue(id, out SyncWorker? worker)) continue; await worker.Cts.CancelAsync(); await worker.WorkerTask; - _vaults.TryRemove(id, out _); + Vaults.TryRemove(id, out _); _logger.LogInformation("Removed vault from syncing: {VaultId}", id); } @@ -71,20 +72,6 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) } } - private async Task SyncVaultAsync(FileSyncManager syncManager, CancellationToken stoppingToken) - { - while (!stoppingToken.IsCancellationRequested) - { - _queuer.Enqueue(syncManager.Id, (ct) => - { - Console.WriteLine($"Syncing vault {syncManager.Id}"); - return Task.CompletedTask; - }); - - Log.Debug($"{syncManager.Id} will sync every {syncManager.RemoteVault.SyncInterval} minutes"); - //await Task.Delay(TimeSpan.FromMinutes(syncManager.RemoteVault.SyncInterval), stoppingToken); - await Task.Delay(TimeSpan.FromSeconds(5), stoppingToken); - } - } + protected abstract Task SyncVaultAsync(FileSyncManager syncManager, CancellationToken stoppingToken); } } \ No newline at end of file diff --git a/Parallel.Cli/Services/RunCommand.cs b/Parallel.Cli/Services/RunCommand.cs index b92cd40..be70f43 100644 --- a/Parallel.Cli/Services/RunCommand.cs +++ b/Parallel.Cli/Services/RunCommand.cs @@ -26,9 +26,14 @@ private async Task HandleCommandAsync() if (OperatingSystem.IsLinux()) builder.Services.AddSystemd(); builder.Services.AddHostedService(); - builder.Services.AddHostedService(); + //builder.Services.AddHostedService(); + //builder.Services.AddHostedService(); + + builder.Services.AddHostedService(); + + //builder.Services.AddHostedService(); - builder.Services.AddSingleton(); + //builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Logging.ClearProviders(); diff --git a/Parallel.Cli/Services/WatcherSyncService.cs b/Parallel.Cli/Services/WatcherSyncService.cs new file mode 100644 index 0000000..2f5b48d --- /dev/null +++ b/Parallel.Cli/Services/WatcherSyncService.cs @@ -0,0 +1,74 @@ +// Copyright 2026 Entex Interactive + +using System.Collections.Concurrent; +using Microsoft.Extensions.Logging; +using Parallel.Core.IO.Scanning; +using Parallel.Core.IO.Syncing; +using Parallel.Core.Tasks; + +namespace Parallel.Cli.Services +{ + public class WatcherSyncService : BaseSyncService + { + private readonly ILogger _logger; + private readonly ConcurrentBag _watchers = new ConcurrentBag(); + + public WatcherSyncService(ILogger logger, TaskQueuer queuer) : base(logger, queuer) + { + _logger = logger; + } + + protected override async Task SyncVaultAsync(FileSyncManager syncManager, CancellationToken stoppingToken) + { + foreach (string path in syncManager.RemoteVault.SyncDirectories) + { + _logger.LogDebug("Watching directory: {Path}", path); + FileSystemWatcher watcher = new FileSystemWatcher() + { + Path = path, + IncludeSubdirectories = true, + NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size, + InternalBufferSize = 32 * 1024, + EnableRaisingEvents = true + }; + + watcher.Created += OnCreated; + watcher.Changed += OnChanged; + watcher.Deleted += OnDeleted; + watcher.Renamed += OnRenamed; + watcher.Error += OnError; + _watchers.Add(watcher); + } + } + + private void OnCreated(object sender, FileSystemEventArgs e) + { + if (FileScanner.IsDirectory(e.FullPath)) return; + + _logger.LogDebug("Created file: {Path}", e.FullPath); + } + + private void OnChanged(object sender, FileSystemEventArgs e) + { + if (FileScanner.IsDirectory(e.FullPath)) return; + _logger.LogDebug("Changed file: {Path}", e.FullPath); + } + + private void OnDeleted(object sender, FileSystemEventArgs e) + { + if (FileScanner.IsDirectory(e.FullPath)) return; + _logger.LogDebug("Deleted file: {Path}", e.FullPath); + } + + private void OnRenamed(object sender, RenamedEventArgs e) + { + if (FileScanner.IsDirectory(e.FullPath)) return; + _logger.LogDebug("Renamed file: {Path}", e.FullPath); + } + + private void OnError(object sender, ErrorEventArgs e) + { + _logger.LogError("Exception throw: {Exception}", e.GetException().ToString()); + } + } +} \ No newline at end of file diff --git a/Parallel.Core/Tasks/TaskQueuer.cs b/Parallel.Core/Tasks/TaskQueuer.cs index a990d1c..367eb71 100644 --- a/Parallel.Core/Tasks/TaskQueuer.cs +++ b/Parallel.Core/Tasks/TaskQueuer.cs @@ -72,7 +72,7 @@ public async Task> WaitAsync(CancellationToken ct) EnqueuedTask? next = _tasks.Values.MaxBy(t => t.Priority + (DateTime.UtcNow - t.EnqueuedAt).TotalSeconds * 0.1); if (ReferenceEquals(candidate, next)) { - _logger.LogDebug($"Starting task with key: '{candidate.Key}', remaining: {_queue.Count - 1}"); + _logger.LogDebug("Starting task with key: '{CandidateKey}', remaining: {QueueCount}", candidate.Key, _queue.Count - 1); _tasks.TryRemove(candidate.Key, out _); return candidate.TaskFunc; }