From adcf8b1504fd31e1783ed3a5b70a08abe2b89e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20H=C3=A4felfinger?= Date: Mon, 11 Sep 2023 16:51:46 +0200 Subject: [PATCH] fixes some concurrency issues on filecount and failed files --- PiwigoDirectorySync/Services/FileIndexer.cs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/PiwigoDirectorySync/Services/FileIndexer.cs b/PiwigoDirectorySync/Services/FileIndexer.cs index 386c131..18d3b89 100644 --- a/PiwigoDirectorySync/Services/FileIndexer.cs +++ b/PiwigoDirectorySync/Services/FileIndexer.cs @@ -1,4 +1,5 @@ -using System.Threading.Channels; +using System.Collections.Concurrent; +using System.Threading.Channels; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -9,9 +10,10 @@ namespace PiwigoDirectorySync.Services; internal class FileIndexer : IFileIndexer { - private readonly IList _failedFiles = new List(); + private readonly ConcurrentBag _failedFiles = new(); private readonly ILogger _logger; private readonly IServiceProvider _serviceProvider; + private int _totalFilesScanned; public FileIndexer(ILogger logger, IServiceProvider serviceProvider) { @@ -19,8 +21,9 @@ internal class FileIndexer : IFileIndexer _serviceProvider = serviceProvider; } - public int TotalFilesScanned { get; private set; } - public IReadOnlyCollection FailedFiles => _failedFiles.AsReadOnly(); + public int TotalFilesScanned => _totalFilesScanned; + + public IReadOnlyCollection FailedFiles => _failedFiles; public async Task StartProcessingAsync(Channel fileQueue, int piwigoServerId, CancellationToken ct) { @@ -74,7 +77,7 @@ internal class FileIndexer : IFileIndexer await db.SaveChangesAsync(ct); - TotalFilesScanned++; + Interlocked.Add(ref _totalFilesScanned, 1); } catch (Exception ex) {