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:
BinaryAlley committed 2026-10-31 14:42:33 +02:00
1 parent b0c24a1722
commit 8ff5149b6e
85 files changed
+2559 -1394

No files matched your search

@@ -11,7 +11,6 @@ using NSubstitute;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
#endregion
@@ -55,11 +54,11 @@ public class MusicLibraryTypeScannerTests
MusicLibraryTypeScanner sut = new(_mockJobFactory);
// Act
List<IMediaLibraryScanJob> rootJobs = [.. sut.CreateScanJobsForLibrary(libraryId)];
IMediaLibraryScanDiscoveryJob rootJob = sut.CreateScanJobsForLibrary(libraryId);
// Assert
IMediaLibraryScanJob rootJob = Assert.Single(rootJobs);
Assert.Same(discoveryJob, rootJob);
Assert.IsAssignableFrom<IMusicFileSystemDiscoveryJob>(rootJob);
Assert.Contains(diffJob, discoveryJob.Children);
Assert.Contains(discoveryJob, diffJob.Parents);
Assert.Contains(hashJob, diffJob.Children);
@@ -79,6 +78,61 @@ public class MusicLibraryTypeScannerTests
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanArtworkEnrichmentJob>(libraryId);
}
[Fact]
public void CreateScanJobsForLibrary_WhenCalled_ShouldReturnTheFileSystemDiscoveryJobAsTheOnlyRoot()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
TestScanJob discoveryJob = CreateJob<IMusicFileSystemDiscoveryJob>();
CreateJob<IMediaLibraryScanDiffJob>();
CreateJob<IMediaLibraryScanHashJob>();
CreateJob<IMusicMetadataExtractionJob>();
CreateJob<IMediaLibraryScanResultsSaveJob>();
CreateJob<IMediaLibraryScanProviderConfigurationInvalidationJob>();
CreateJob<IMediaLibraryScanMetadataEnrichmentJob>();
CreateJob<IMediaLibraryScanArtworkEnrichmentJob>();
MusicLibraryTypeScanner sut = new(_mockJobFactory);
// Act
IMediaLibraryScanDiscoveryJob rootJob = sut.CreateScanJobsForLibrary(libraryId);
// Assert
Assert.Same(discoveryJob, rootJob);
// The file system discovery job is the only top level job, so nothing else is reachable without it.
Assert.Empty(discoveryJob.Parents);
}
[Fact]
public void CreateScanJobsForLibrary_WhenCalled_ShouldMakeTheWholeChainReachableFromTheRoot()
{
// Arrange
LibraryId libraryId = _libraryIdFixture.Create();
TestScanJob discoveryJob = CreateJob<IMusicFileSystemDiscoveryJob>();
TestScanJob diffJob = CreateJob<IMediaLibraryScanDiffJob>();
TestScanJob hashJob = CreateJob<IMediaLibraryScanHashJob>();
TestScanJob metadataExtractionJob = CreateJob<IMusicMetadataExtractionJob>();
TestScanJob saveJob = CreateJob<IMediaLibraryScanResultsSaveJob>();
TestScanJob invalidationJob = CreateJob<IMediaLibraryScanProviderConfigurationInvalidationJob>();
TestScanJob enrichmentJob = CreateJob<IMediaLibraryScanMetadataEnrichmentJob>();
TestScanJob artworkEnrichmentJob = CreateJob<IMediaLibraryScanArtworkEnrichmentJob>();
MusicLibraryTypeScanner sut = new(_mockJobFactory);
// Act
IMediaLibraryScanDiscoveryJob rootJob = sut.CreateScanJobsForLibrary(libraryId);
HashSet<IMediaLibraryScanJob> reachableJobs = GetReachableJobs(rootJob);
// Assert
Assert.Equal(8, reachableJobs.Count);
Assert.Contains(discoveryJob, reachableJobs);
Assert.Contains(diffJob, reachableJobs);
Assert.Contains(hashJob, reachableJobs);
Assert.Contains(metadataExtractionJob, reachableJobs);
Assert.Contains(saveJob, reachableJobs);
Assert.Contains(invalidationJob, reachableJobs);
Assert.Contains(enrichmentJob, reachableJobs);
Assert.Contains(artworkEnrichmentJob, reachableJobs);
}
[Fact]
public void CreateScanJobsForLibrary_WhenCalled_ShouldCreateJobsForTheLibrary()
{
@@ -95,7 +149,7 @@ public class MusicLibraryTypeScannerTests
MusicLibraryTypeScanner sut = new(_mockJobFactory);
// Act
_ = sut.CreateScanJobsForLibrary(libraryId).ToList();
_ = sut.CreateScanJobsForLibrary(libraryId);
// Assert
_mockJobFactory.Received(1).CreateJob<IMusicFileSystemDiscoveryJob>(libraryId);
@@ -108,6 +162,27 @@ public class MusicLibraryTypeScannerTests
_mockJobFactory.Received(1).CreateJob<IMediaLibraryScanArtworkEnrichmentJob>(libraryId);
}
/// <summary>
/// Collects every job that is reachable from <paramref name="rootJob"/> through the child relationships.
/// </summary>
/// <param name="rootJob">The root job of the job graph.</param>
/// <returns>The set of reachable jobs.</returns>
private static HashSet<IMediaLibraryScanJob> GetReachableJobs(IMediaLibraryScanJob rootJob)
{
HashSet<IMediaLibraryScanJob> reachableJobs = [];
Queue<IMediaLibraryScanJob> jobsToVisit = new();
jobsToVisit.Enqueue(rootJob);
while (jobsToVisit.Count > 0)
{
IMediaLibraryScanJob currentJob = jobsToVisit.Dequeue();
if (!reachableJobs.Add(currentJob))
continue;
foreach (IMediaLibraryScanJob childJob in currentJob.Children)
jobsToVisit.Enqueue(childJob);
}
return reachableJobs;
}
private TestScanJob CreateJob<TJob>() where TJob : class, IMediaLibraryScanJob
{
TestScanJob job = new()