Merge pull request #125 from BinaryAlley/feature/implement-debounced-library-scan-progress-updater

Implements debounced library scan progress updater
This commit is contained in:
BinaryAlley authored and GitHub committed 2025-02-23 11:08:22 +02:00
commit 3c04d32b60
2 files changed
+50 -25

No files matched your search

@@ -1,4 +1,5 @@
#region ========================================================================= USING ===================================================================================== #region ========================================================================= USING =====================================================================================
using DebounceThrottle;
using ErrorOr; using ErrorOr;
using Lumina.Application.Common.Mapping.MediaLibrary.Management; using Lumina.Application.Common.Mapping.MediaLibrary.Management;
using Lumina.Application.Core.MediaLibrary.Management.Progress; using Lumina.Application.Core.MediaLibrary.Management.Progress;
@@ -7,6 +8,8 @@ using Lumina.Domain.Common.Enums.MediaLibrary;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Progress; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Progress;
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects; using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
using Microsoft.AspNetCore.SignalR; using Microsoft.AspNetCore.SignalR;
using System;
using System.Collections.Concurrent;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
#endregion #endregion
@@ -20,6 +23,7 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
{ {
private readonly IHubContext<MediaLibraryScanProgressHub> _hubContext; private readonly IHubContext<MediaLibraryScanProgressHub> _hubContext;
private readonly IMediaLibrariesScanProgressTracker _mediaLibrariesScanProgressTracker; private readonly IMediaLibrariesScanProgressTracker _mediaLibrariesScanProgressTracker;
private readonly ConcurrentDictionary<MediaLibraryScanCompositeId, DebounceDispatcher> _debouncers = new();
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="DebouncedMediaLibraryScanProgressNotifier"/> class. /// Initializes a new instance of the <see cref="DebouncedMediaLibraryScanProgressNotifier"/> class.
@@ -39,18 +43,26 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param> /// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async Task SendLibraryProgressUpdateEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken) public async Task SendLibraryProgressUpdateEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken)
{ {
if (!cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
return;
// throttle network traffic to no more than 20 updates a second, any more are pointless from a progress report perspective
DebounceDispatcher debouncer = _debouncers.GetOrAdd(mediaLibraryScanCompositeId, _ => new DebounceDispatcher(TimeSpan.FromMilliseconds(50)));
await debouncer.DebounceAsync(async () =>
{ {
// the token might be canceled after initial check but before debounced execution starts
if (cancellationToken.IsCancellationRequested)
return;
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.GetScanProgress(mediaLibraryScanCompositeId); ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.GetScanProgress(mediaLibraryScanCompositeId);
if (progressResult.IsError)
return;
if (!progressResult.IsError) MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
{ await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false);
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); }, cancellationToken).ConfigureAwait(false);
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false);
}
}
} }
/// <summary> /// <summary>
/// Notifies the SignalR clients about the completion of a media library scan. /// Notifies the SignalR clients about the completion of a media library scan.
/// </summary> /// </summary>
@@ -58,15 +70,16 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param> /// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async Task SendLibraryScanFinishedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken) public async Task SendLibraryScanFinishedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken)
{ {
if (!cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ return;
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId); ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId);
CleanupDebouncer(mediaLibraryScanCompositeId);
if (!progressResult.IsError) if (!progressResult.IsError)
{ {
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanFinishedEvent", progress, cancellationToken).ConfigureAwait(false); await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanFinishedEvent", progress, cancellationToken).ConfigureAwait(false);
}
} }
} }
@@ -77,16 +90,27 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param> /// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public async Task SendLibraryScanFailedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken) public async Task SendLibraryScanFailedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken)
{ {
if (!cancellationToken.IsCancellationRequested) if (cancellationToken.IsCancellationRequested)
{ return;
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId); ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId);
if (!progressResult.IsError) CleanupDebouncer(mediaLibraryScanCompositeId);
{
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse(); if (!progressResult.IsError)
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync( {
"libraryScanFailedEvent", progress with { Status = LibraryScanJobStatus.Failed.ToString() }, cancellationToken).ConfigureAwait(false); MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
} await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync(
"libraryScanFailedEvent", progress with { Status = LibraryScanJobStatus.Failed.ToString() }, cancellationToken).ConfigureAwait(false);
} }
} }
/// <summary>
/// Disposes the debouncer that was used in a scan identified by <paramref name="mediaLibraryScanCompositeId"/>, that is now completed.
/// </summary>
/// <param name="mediaLibraryScanCompositeId">The object representing the unique identifier for the media library scan whose debouncer needs disposing.</param>
private void CleanupDebouncer(MediaLibraryScanCompositeId mediaLibraryScanCompositeId)
{
if (_debouncers.TryRemove(mediaLibraryScanCompositeId, out DebounceDispatcher? debouncer))
debouncer.Dispose();
}
} }
@@ -14,6 +14,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="DebounceThrottle" Version="3.0.1" />
<PackageReference Include="FluentValidation" Version="11.10.0" /> <PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" /> <PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.2.0" /> <PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.2.0" />