mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-02 02:01:10 +00:00
Implements debounced library scan progress updater
This commit is contained in:
1 parent
a8fd7949b4
commit
aa9d2f6768
2 files changed
+50
-25
No files matched your search
+49
-25
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using DebounceThrottle;
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.Mapping.MediaLibrary.Management;
|
||||
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.ValueObjects;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
@@ -20,6 +23,7 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
|
||||
{
|
||||
private readonly IHubContext<MediaLibraryScanProgressHub> _hubContext;
|
||||
private readonly IMediaLibrariesScanProgressTracker _mediaLibrariesScanProgressTracker;
|
||||
private readonly ConcurrentDictionary<MediaLibraryScanCompositeId, DebounceDispatcher> _debouncers = new();
|
||||
|
||||
/// <summary>
|
||||
/// 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>
|
||||
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);
|
||||
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();
|
||||
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false);
|
||||
}, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Notifies the SignalR clients about the completion of a media library scan.
|
||||
/// </summary>
|
||||
@@ -58,15 +70,16 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
public async Task SendLibraryScanFinishedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId);
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
return;
|
||||
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId);
|
||||
|
||||
CleanupDebouncer(mediaLibraryScanCompositeId);
|
||||
|
||||
if (!progressResult.IsError)
|
||||
{
|
||||
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
||||
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanFinishedEvent", progress, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
if (!progressResult.IsError)
|
||||
{
|
||||
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
||||
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>
|
||||
public async Task SendLibraryScanFailedEventAsync(MediaLibraryScanCompositeId mediaLibraryScanCompositeId, CancellationToken cancellationToken)
|
||||
{
|
||||
if (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId);
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
return;
|
||||
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.RemoveScanProgress(mediaLibraryScanCompositeId);
|
||||
|
||||
if (!progressResult.IsError)
|
||||
{
|
||||
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
||||
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync(
|
||||
"libraryScanFailedEvent", progress with { Status = LibraryScanJobStatus.Failed.ToString() }, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
CleanupDebouncer(mediaLibraryScanCompositeId);
|
||||
|
||||
if (!progressResult.IsError)
|
||||
{
|
||||
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>
|
||||
<PackageReference Include="DebounceThrottle" Version="3.0.1" />
|
||||
<PackageReference Include="FluentValidation" Version="11.10.0" />
|
||||
<PackageReference Include="Konscious.Security.Cryptography.Argon2" Version="1.3.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.2.0" />
|
||||
|
||||
Reference in new issue
Block a user