mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Implements debounced library scan progress updater
This commit is contained in:
1 parent
a8fd7949b4
commit
aa9d2f6768
2 files changed
+36
-11
No files matched your search
+35
-11
@@ -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,16 +43,24 @@ 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;
|
||||||
ErrorOr<MediaLibraryScanProgress> progressResult = _mediaLibrariesScanProgressTracker.GetScanProgress(mediaLibraryScanCompositeId);
|
|
||||||
|
|
||||||
if (!progressResult.IsError)
|
// 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;
|
||||||
|
|
||||||
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
||||||
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false);
|
await _hubContext.Clients.Group(mediaLibraryScanCompositeId.ToString()).SendAsync("libraryScanProgressUpdateEvent", progress, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -58,17 +70,18 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Notifies the SignalR clients about the failure of a media library scan.
|
/// Notifies the SignalR clients about the failure of a media library scan.
|
||||||
@@ -77,10 +90,12 @@ 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);
|
||||||
|
|
||||||
|
CleanupDebouncer(mediaLibraryScanCompositeId);
|
||||||
|
|
||||||
if (!progressResult.IsError)
|
if (!progressResult.IsError)
|
||||||
{
|
{
|
||||||
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
MediaLibraryScanProgressResponse progress = progressResult.Value.ToResponse();
|
||||||
@@ -88,5 +103,14 @@ public sealed class DebouncedMediaLibraryScanProgressNotifier : IMediaLibrarySca
|
|||||||
"libraryScanFailedEvent", progress with { Status = LibraryScanJobStatus.Failed.ToString() }, cancellationToken).ConfigureAwait(false);
|
"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" />
|
||||||
|
|||||||
Reference in new issue
Block a user