adds full sync command
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-09-02 16:39:17 +02:00
parent 98fea6c0be
commit 303d69efe6
6 changed files with 94 additions and 10 deletions

View File

@ -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, "<PiwigoServerId>")]
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();
}
}

View File

@ -20,29 +20,34 @@ public class ScanCommand : AsyncCommand<ScanSettings>
}
public override async Task<int> 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<string>();
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;
}
}

View File

@ -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<SyncFullSettings>
{
private readonly IAlbumSynchronizer _albumSynchronizer;
private readonly IFileIndexer _fileIndexer;
private readonly IFileSystemScanner _fileSystemScanner;
private readonly IImageSynchronizer _imageSynchronizer;
private readonly ILogger<SyncFullCommand> _logger;
public SyncFullCommand(IAlbumSynchronizer albumSynchronizer, IFileIndexer fileIndexer, IFileSystemScanner fileSystemScanner, IImageSynchronizer imageSynchronizer,
ILogger<SyncFullCommand> logger)
{
_albumSynchronizer = albumSynchronizer;
_fileIndexer = fileIndexer;
_fileSystemScanner = fileSystemScanner;
_imageSynchronizer = imageSynchronizer;
_logger = logger;
}
public override async Task<int> 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;
}
}

View File

@ -0,0 +1,3 @@
namespace PiwigoDirectorySync.Commands;
public class SyncFullSettings : CommonCommandSettings {}

View File

@ -49,6 +49,7 @@ app.Configure(config =>
{
c.AddCommand<SyncAlbumsCommand>("albums");
c.AddCommand<SyncImagesCommand>("images");
c.AddCommand<SyncFullCommand>("full");
});
});

View File

@ -24,6 +24,12 @@
"commandLineArgs": "sync images 1",
"environmentVariables": {
}
},
"SyncFull": {
"commandName": "Project",
"commandLineArgs": "sync full",
"environmentVariables": {
}
}
}
}