mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 16:11:10 +00:00
Serializes media library scans through a scan queue
Replaces the job queue with a scan queue that executes one media library scan at a time, in the order in which the scans were queued, so several libraries requested at once no longer run one complete job chain each on the thread pool shared with the web server. Keeps queued scans Pending and cancellable, reports them as queued, and cancels the executing scan and the queued ones when the user cancels all scans. A media library type without a scanner now fails its own scan instead of leaving it stuck Pending, without stopping the scans queued behind it. Makes the file system discovery job the only top level job of a scan graph, so awaiting it completes the whole scan, and leaves one logical processor free for the rest of the process while discovery and hashing read files in parallel. Removes the per job Task.Run wrappers, links the scan cancellation with the host shutdown token in the scan processor, updates the ADR amendments, API documentation, manual, web client and tests, and normalizes the comments of the touched files.
This commit is contained in:
1 parent
b0c24a1722
commit
8ff5149b6e
85 files changed
+2559
-1394
No files matched your search
+6
@@ -2,8 +2,10 @@
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Events;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
@@ -17,6 +19,7 @@ namespace Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedCo
|
||||
public class LibraryScanQueuedDomainEventFixture
|
||||
{
|
||||
private readonly ScanIdFixture _scanIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
|
||||
/// <summary>
|
||||
@@ -24,18 +27,21 @@ public class LibraryScanQueuedDomainEventFixture
|
||||
/// </summary>
|
||||
/// <param name="id">Optional. The Id of the domain event.</param>
|
||||
/// <param name="scanId">Optional. The unique identifier of the queued media library scan.</param>
|
||||
/// <param name="userId">Optional. The unique identifier of the user that initiated the queued media library scan.</param>
|
||||
/// <param name="libraryId">Optional. The unique identifier of the library whose scan was queued.</param>
|
||||
/// <param name="occurredOnUtc">Optional. The date and time when the domain event occurred.</param>
|
||||
/// <returns>The created <see cref="LibraryScanQueuedDomainEvent"/>.</returns>
|
||||
public LibraryScanQueuedDomainEvent Create(
|
||||
Guid? id = null,
|
||||
ScanId? scanId = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
DateTime? occurredOnUtc = null)
|
||||
{
|
||||
return new LibraryScanQueuedDomainEvent(
|
||||
id ?? Guid.NewGuid(),
|
||||
scanId ?? _scanIdFixture.Create(),
|
||||
userId ?? _userIdFixture.Create(),
|
||||
libraryId ?? _libraryIdFixture.Create(),
|
||||
occurredOnUtc ?? DateTime.UtcNow);
|
||||
}
|
||||
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Controllable test double for the <see cref="IMediaLibraryScanDiscoveryJob"/> interface.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public sealed class FakeMediaLibraryScanDiscoveryJob : IMediaLibraryScanDiscoveryJob
|
||||
{
|
||||
private readonly TaskCompletionSource _executionStarted = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
private readonly TaskCompletionSource _executionCompleted = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Id of the media library scan that this job is part of.
|
||||
/// </summary>
|
||||
public required ScanId ScanId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Id of the user that initiated the media library scan that this job is part of.
|
||||
/// </summary>
|
||||
public required UserId UserId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the unique identifier of the media library upon which the scan is performed.
|
||||
/// </summary>
|
||||
public required LibraryId LibraryId { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of child media scan jobs that will be triggered when this job's payload completes execution.
|
||||
/// </summary>
|
||||
public List<IMediaLibraryScanJob> Children { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of parent media scan jobs that will trigger this job when their payload will complete execution.
|
||||
/// </summary>
|
||||
public List<IMediaLibraryScanJob> Parents { get; } = [];
|
||||
|
||||
/// <summary>
|
||||
/// Gets the status of the current media library scan job.
|
||||
/// </summary>
|
||||
public LibraryScanJobStatus Status { get; private set; } = LibraryScanJobStatus.Pending;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of times this job was executed.
|
||||
/// </summary>
|
||||
public int ExecutionCount { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether this job is currently executing.
|
||||
/// </summary>
|
||||
public bool IsExecuting { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the Id this job was last executed with.
|
||||
/// </summary>
|
||||
public Guid? LastExecutionId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the cancellation token this job was last executed with.
|
||||
/// </summary>
|
||||
public CancellationToken LastCancellationToken { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the optional callback invoked while the job executes, allowing a test to block or throw.
|
||||
/// </summary>
|
||||
public Func<CancellationToken, Task>? OnExecute { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets a task that completes as soon as the job starts executing.
|
||||
/// </summary>
|
||||
public Task ExecutionStarted => _executionStarted.Task;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a task that completes as soon as the job finishes executing successfully.
|
||||
/// </summary>
|
||||
public Task ExecutionCompleted => _executionCompleted.Task;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a media library scan job to the list of child media library scan jobs that will be triggered when this job's payload completes execution.
|
||||
/// </summary>
|
||||
/// <param name="job">The child media library scan job to be added.</param>
|
||||
public void AddChild(IMediaLibraryScanJob job)
|
||||
{
|
||||
if (!Children.Contains(job) && !Parents.Contains(job))
|
||||
Children.Add(job);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a media library scan job to the list of parent media library scan jobs that will trigger this job when their payload will complete execution.
|
||||
/// </summary>
|
||||
/// <param name="job">The child media library scan job to be added.</param>
|
||||
public void AddParent(IMediaLibraryScanJob job)
|
||||
{
|
||||
if (!Parents.Contains(job) && !Children.Contains(job))
|
||||
Parents.Add(job);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Executes the payload of the media library scan job.
|
||||
/// </summary>
|
||||
/// <typeparam name="TInput">The type of the input parameter representing the data to be processed by this payload.</typeparam>
|
||||
/// <param name="id">The id of the media library scan job.</param>
|
||||
/// <param name="input">The input data to be processed by this payload.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public async Task ExecuteAsync<TInput>(Guid id, TInput input, CancellationToken cancellationToken)
|
||||
{
|
||||
ExecutionCount++;
|
||||
LastExecutionId = id;
|
||||
LastCancellationToken = cancellationToken;
|
||||
IsExecuting = true;
|
||||
Status = LibraryScanJobStatus.Running;
|
||||
_executionStarted.TrySetResult();
|
||||
|
||||
if (OnExecute is not null)
|
||||
await OnExecute(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
Status = LibraryScanJobStatus.Completed;
|
||||
IsExecuting = false;
|
||||
_executionCompleted.TrySetResult();
|
||||
}
|
||||
}
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
|
||||
using Lumina.Domain.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.ValueObjects;
|
||||
using Lumina.Domain.Fixtures.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Domain.Fixtures.Core.BoundedContexts.LibraryManagementBoundedContext.LibraryScanAggregate.Services.Jobs;
|
||||
|
||||
/// <summary>
|
||||
/// Fixture class for the <see cref="IMediaLibraryScanDiscoveryJob"/> interface.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class MediaLibraryScanDiscoveryJobFixture
|
||||
{
|
||||
private readonly ScanIdFixture _scanIdFixture = new();
|
||||
private readonly UserIdFixture _userIdFixture = new();
|
||||
private readonly LibraryIdFixture _libraryIdFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a controllable <see cref="FakeMediaLibraryScanDiscoveryJob"/>.
|
||||
/// </summary>
|
||||
/// <param name="scanId">Optional. The Id of the media library scan that the job is part of.</param>
|
||||
/// <param name="userId">Optional. The Id of the user that initiated the media library scan.</param>
|
||||
/// <param name="libraryId">Optional. The Id of the media library that is scanned.</param>
|
||||
/// <param name="children">Optional. The child jobs of the created job.</param>
|
||||
/// <param name="onExecute">Optional. The callback invoked while the job executes.</param>
|
||||
/// <returns>The created <see cref="FakeMediaLibraryScanDiscoveryJob"/>.</returns>
|
||||
public FakeMediaLibraryScanDiscoveryJob Create(
|
||||
ScanId? scanId = null,
|
||||
UserId? userId = null,
|
||||
LibraryId? libraryId = null,
|
||||
List<IMediaLibraryScanJob>? children = null,
|
||||
Func<CancellationToken, Task>? onExecute = null)
|
||||
{
|
||||
FakeMediaLibraryScanDiscoveryJob job = new()
|
||||
{
|
||||
ScanId = scanId ?? _scanIdFixture.Create(),
|
||||
UserId = userId ?? _userIdFixture.Create(),
|
||||
LibraryId = libraryId ?? _libraryIdFixture.Create(),
|
||||
OnExecute = onExecute
|
||||
};
|
||||
|
||||
foreach (IMediaLibraryScanJob child in children ?? [])
|
||||
job.AddChild(child);
|
||||
|
||||
return job;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user