From 303d69efe6e8a92fea3162a9c613982be778e475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Sat, 2 Sep 2023 16:39:17 +0200 Subject: [PATCH] adds full sync command --- .../Commands/CommonCommandSettings.cs | 27 +++++++++-- PiwigoDirectorySync/Commands/ScanCommand.cs | 19 +++++--- .../Commands/SyncFullCommand.cs | 48 +++++++++++++++++++ .../Commands/SyncFullSettings.cs | 3 ++ PiwigoDirectorySync/Program.cs | 1 + .../Properties/launchSettings.json | 6 +++ 6 files changed, 94 insertions(+), 10 deletions(-) create mode 100644 PiwigoDirectorySync/Commands/SyncFullCommand.cs create mode 100644 PiwigoDirectorySync/Commands/SyncFullSettings.cs diff --git a/PiwigoDirectorySync/Commands/CommonCommandSettings.cs b/PiwigoDirectorySync/Commands/CommonCommandSettings.cs index 7442047..71b08f9 100644 --- a/PiwigoDirectorySync/Commands/CommonCommandSettings.cs +++ b/PiwigoDirectorySync/Commands/CommonCommandSettings.cs @@ -1,9 +1,30 @@ -using Spectre.Console.Cli; +using System.ComponentModel; +using System.Diagnostics.CodeAnalysis; +using Spectre.Console; +using Spectre.Console.Cli; namespace PiwigoDirectorySync.Commands; +[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global", Justification = "Done by parser")] public class CommonCommandSettings : CommandSettings { - [CommandArgument(0, "")] - public int PiwigoServerId { get; set; } + [CommandArgument(0, "[PiwigoServerId]")] + [DefaultValue(1)] + public int PiwigoServerId { get; init; } + + public override ValidationResult Validate() + { + var baseResult = base.Validate(); + if (!baseResult.Successful) + { + return baseResult; + } + + if (PiwigoServerId < 1) + { + return ValidationResult.Error("the piwigo server id can not be less than 1"); + } + + return ValidationResult.Success(); + } } \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/ScanCommand.cs b/PiwigoDirectorySync/Commands/ScanCommand.cs index c13ae74..ada6b84 100644 --- a/PiwigoDirectorySync/Commands/ScanCommand.cs +++ b/PiwigoDirectorySync/Commands/ScanCommand.cs @@ -20,29 +20,34 @@ public class ScanCommand : AsyncCommand } public override async Task ExecuteAsync(CommandContext context, ScanSettings settings) + { + var cancellationTokenSource = new CancellationTokenSource(); + + await ScanDirectory(_logger, _fileIndexer, _fileSystemScanner, settings.PiwigoServerId, cancellationTokenSource.Token); + return 0; + } + + internal static async Task ScanDirectory(ILogger logger, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner, int piwigoServerId, CancellationToken ct) { //TODO: check files for deletion -> files in db but no longer exist - _logger.LogInformation("Starting scanner and remover"); + logger.LogInformation("Starting scanner and remover"); var stopWatch = Stopwatch.StartNew(); - var cancellationTokenSource = new CancellationTokenSource(); var fileQueue = Channel.CreateUnbounded(); - var indexerTask = _fileIndexer.StartProcessingAsync(fileQueue, settings.PiwigoServerId, cancellationTokenSource.Token); + var indexerTask = fileIndexer.StartProcessingAsync(fileQueue, piwigoServerId, ct); - await _fileSystemScanner.ScanAsync(fileQueue, settings.PiwigoServerId, cancellationTokenSource.Token); + await fileSystemScanner.ScanAsync(fileQueue, piwigoServerId, ct); fileQueue.Writer.Complete(); await Task.WhenAll(fileQueue.Reader.Completion, indexerTask); stopWatch.Stop(); - _logger.LogInformation("Processed {IndexerTotalFilesScanned} image files in {ElapsedTotalSeconds} seconds", _fileIndexer.TotalFilesScanned, stopWatch.Elapsed.TotalSeconds); + logger.LogInformation("Processed {IndexerTotalFilesScanned} image files in {ElapsedTotalSeconds} seconds", fileIndexer.TotalFilesScanned, stopWatch.Elapsed.TotalSeconds); //TODO: write failed files to log - - return 0; } } \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/SyncFullCommand.cs b/PiwigoDirectorySync/Commands/SyncFullCommand.cs new file mode 100644 index 0000000..f34e8d2 --- /dev/null +++ b/PiwigoDirectorySync/Commands/SyncFullCommand.cs @@ -0,0 +1,48 @@ +using System.Diagnostics; +using Microsoft.Extensions.Logging; +using PiwigoDirectorySync.Services; +using Spectre.Console.Cli; + +namespace PiwigoDirectorySync.Commands; + +public class SyncFullCommand : AsyncCommand +{ + private readonly IAlbumSynchronizer _albumSynchronizer; + private readonly IFileIndexer _fileIndexer; + private readonly IFileSystemScanner _fileSystemScanner; + private readonly IImageSynchronizer _imageSynchronizer; + private readonly ILogger _logger; + + public SyncFullCommand(IAlbumSynchronizer albumSynchronizer, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner, IImageSynchronizer imageSynchronizer, + ILogger logger) + { + _albumSynchronizer = albumSynchronizer; + _fileIndexer = fileIndexer; + _fileSystemScanner = fileSystemScanner; + _imageSynchronizer = imageSynchronizer; + _logger = logger; + } + + public override async Task ExecuteAsync(CommandContext context, SyncFullSettings settings) + { + _logger.LogInformation("Starting full synchronization for piwigo server {SettingsPiwigoServerId}", settings.PiwigoServerId); + var stopWatch = Stopwatch.StartNew(); + + var cancellationTokenSource = new CancellationTokenSource(); + + _logger.LogInformation("running file system scan"); + await ScanCommand.ScanDirectory(_logger, _fileIndexer, _fileSystemScanner, settings.PiwigoServerId, cancellationTokenSource.Token); + + _logger.LogInformation("running album synchronization"); + await _albumSynchronizer.SynchronizeAlbums(settings.PiwigoServerId, cancellationTokenSource.Token); + + _logger.LogInformation("running image synchronization"); + await _imageSynchronizer.SynchronizeImages(settings.PiwigoServerId, cancellationTokenSource.Token); + + stopWatch.Stop(); + _logger.LogInformation("Full synchronization for piwigo server {SettingsPiwigoServerId} finished in {ElapsedTotalSeconds} seconds", settings.PiwigoServerId, + stopWatch.Elapsed.TotalSeconds); + + return 0; + } +} \ No newline at end of file diff --git a/PiwigoDirectorySync/Commands/SyncFullSettings.cs b/PiwigoDirectorySync/Commands/SyncFullSettings.cs new file mode 100644 index 0000000..67f573d --- /dev/null +++ b/PiwigoDirectorySync/Commands/SyncFullSettings.cs @@ -0,0 +1,3 @@ +namespace PiwigoDirectorySync.Commands; + +public class SyncFullSettings : CommonCommandSettings {} \ No newline at end of file diff --git a/PiwigoDirectorySync/Program.cs b/PiwigoDirectorySync/Program.cs index ad55ef1..b1ab1a0 100644 --- a/PiwigoDirectorySync/Program.cs +++ b/PiwigoDirectorySync/Program.cs @@ -49,6 +49,7 @@ app.Configure(config => { c.AddCommand("albums"); c.AddCommand("images"); + c.AddCommand("full"); }); }); diff --git a/PiwigoDirectorySync/Properties/launchSettings.json b/PiwigoDirectorySync/Properties/launchSettings.json index fa6d61a..07f461b 100644 --- a/PiwigoDirectorySync/Properties/launchSettings.json +++ b/PiwigoDirectorySync/Properties/launchSettings.json @@ -24,6 +24,12 @@ "commandLineArgs": "sync images 1", "environmentVariables": { } + }, + "SyncFull": { + "commandName": "Project", + "commandLineArgs": "sync full", + "environmentVariables": { + } } } }