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

@@ -96,6 +96,9 @@ internal sealed class LibraryRepository : ILibraryRepository
.Include(library => library.PathTemplateParts)
.AsSplitQuery()
.Where(library => library.IsEnabled && !library.IsLocked)
// The scan queue executes the scans in this order, so it must be deterministic and match the order shown to the user.
.OrderBy(library => library.Title)
.ThenBy(library => library.Id)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}
@@ -83,16 +83,17 @@ internal sealed class LibraryScanRepository : ILibraryScanRepository
}
/// <summary>
/// Gets the media library scans that have a <see cref="LibraryScanJobStatus.Running"/> status, from the storage medium.
/// Gets the media library scans that have a <see cref="LibraryScanJobStatus.Pending"/> or a
/// <see cref="LibraryScanJobStatus.Running"/> status, from the storage medium.
/// </summary>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="Result{TValue}"/> containing either a collection of <see cref="LibraryScanEntity"/>, or an error.</returns>
public async Task<Result<IEnumerable<LibraryScanEntity>>> GetRunningScansAsync(CancellationToken cancellationToken)
public async Task<Result<IEnumerable<LibraryScanEntity>>> GetActiveScansAsync(CancellationToken cancellationToken)
{
return await _luminaDbContext.LibraryScans
.Include(library => library.Library)
.AsSplitQuery()
.Where(library => library.Status == LibraryScanJobStatus.Running)
.Where(library => library.Status == LibraryScanJobStatus.Pending || library.Status == LibraryScanJobStatus.Running)
.ToListAsync(cancellationToken).ConfigureAwait(false);
}