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

+4 -4
View File
@@ -116,17 +116,17 @@ The template is read on every scan, so changing it changes how your folders are
## Scanning a library
Creating a library only records where the media lives. To actually read it, you scan it. You can scan a single library, or all of them at once, from the Libraries page.
Creating a library only records where the media lives. To actually read it, you scan it. You can scan a single library, or ask Lumina to scan all of them, from the Libraries page. Only one scan runs at a time: a scan that is requested while another one is running waits in the scan queue, and starts on its own when the scan ahead of it finishes.
Lumina is designed to be efficient and performant. The first scan of a library reads and indexes everything it finds, so it is the slowest one. Every scan after that compares what is on disk against the previous scan and only processes what is new or changed, so it is normally much faster. How long any scan takes also depends on what is enabled for the library: metadata and artwork providers that fetch data from the web add network time on top of reading the files, and they do it only for the items that need it.
How the scan reads the files depends on the hardware that stores them. A library whose content is on a solid state drive can discover and hash several files at the same time, so turn on **Content is on SSD** for it. A library on a mechanical hard drive should keep that setting off, because parallel reads make the drive seek back and forth and can make the scan slower. Metadata extraction and artwork resolution still read the files one at a time; this setting only changes file discovery and content hashing.
How the scan reads the files depends on the hardware that stores them. A library whose content is on a solid state drive can discover and hash several files at the same time, so turn on **Content is on SSD** for it. A library on a mechanical hard drive should keep that setting off, because parallel reads make the drive seek back and forth and can make the scan slower. Metadata extraction and artwork resolution still read the files one at a time; this setting only changes file discovery and content hashing. Even when the parallel reads are on, Lumina leaves one processor free, so the rest of the application keeps responding while it scans.
Scans run on the server in the background, so once one starts you can close the browser and come back later.
While a scan is running, its progress is shown live: how far along it is overall, how many items it has processed, and the operation it is currently on. A scan is queued before it starts, then runs, and ends up finished, cancelled or failed. You can cancel a running scan at any time, for one library or for all of them.
While a scan is waiting in the queue it is shown as queued, and while it is running its progress is shown live: how far along it is overall, how many items it has processed, and the operation it is currently on. A scan is queued before it starts, then runs, and ends up finished, cancelled or failed. You can cancel a scan while it is queued or running, for one library or for all of them; the library shows that its cancellation is in progress until the server confirms it.
A disabled or locked library is refused a scan. Scans can also be driven by the scheduler, so libraries refresh themselves on a timetable without you lifting a finger.
A disabled or locked library is refused a scan, and a library whose type has no scanner yet ends its scan as failed instead of staying queued forever. Scans can also be driven by the scheduler, so libraries refresh themselves on a timetable without you lifting a finger.
## Editing a library
@@ -368,10 +368,27 @@ GET api/v1/libraries/scans/running
},
"status": "Running",
"overallProgressPercentage": 40.0
},
{
"scanId": "0b5f8d21-9c3e-4a7b-8f6d-1e2a3b4c5d6e",
"userId": "2cff0c0f-3a67-4e30-b94f-f4e948a3b64f",
"libraryId": "5d4c3b2a-1f0e-4d9c-8b7a-6f5e4d3c2b1a",
"totalJobs": 0,
"completedJobs": 0,
"currentJobProgress": {
"completedItems": 0,
"totalItems": 0,
"currentOperation": "Queued",
"progressPercentage": 0.0
},
"status": "Pending",
"overallProgressPercentage": 0.0
}
]
```
Queued scans are reported here too, with no jobs prepared yet, because media library scans are executed one at a time and a scan that was queued can wait for the scans ahead of it to finish before it starts.
### Get Library Scan Progress
#### Get Library Scan Progress Request
@@ -36,9 +36,6 @@ internal sealed class FileSystemDiscoveryJob : MediaLibraryScanJob, IFileSystemD
}
}
```
## Amendments
- **2026-10-09**: The generic `FileSystemDiscoveryJob` / `IFileSystemDiscoveryJob` was split into per-type discovery jobs (`BooksFileSystemDiscoveryJob`, `MusicFileSystemDiscoveryJob`), and the job execution contract is now `ExecuteAsync<TInput>(Guid id, TInput input, CancellationToken token)` on `IMediaLibraryScanJob`. The `IServiceScopeFactory` decision itself is unchanged.
## Consequences
@@ -105,3 +102,15 @@ services.AddDbContextFactory<LuminaDbContext>(options =>
```
**Rejected**: Would require a dependency on EF Core in the Application layer, and even when using an abstraction, would completely defeat the purpose of using the Repository + Unit of Work patterns used everywhere else in the Application layer.
## Amendment: Per-Type File System Discovery Jobs (2026-10-09)
The generic `FileSystemDiscoveryJob` / `IFileSystemDiscoveryJob` was split into per-type discovery jobs (`BooksFileSystemDiscoveryJob`, `MusicFileSystemDiscoveryJob`), and the job execution contract is now `ExecuteAsync<TInput>(Guid id, TInput input, CancellationToken token)` on `IMediaLibraryScanJob`. The `IServiceScopeFactory` decision itself is unchanged.
## Amendment: Serialized Media Library Scans (2026-10-31)
The execution model described above changed in a way that directly affects how scoped services are obtained. Until this amendment, the unit placed on the in-memory queue was the root job of a scan job graph, and the processor launched each root job in a fire-and-forget manner. Scanning several media libraries at once, which is the normal outcome of a "scan all libraries" action or of a scheduled run, therefore started one complete job chain per library at the same time. Each chain fans out to `Environment.ProcessorCount` parallel file reads for file system discovery and content hashing, and every job performed its work on the same shared thread pool that the web server uses to handle requests, so above a certain number of libraries the thread pool was starved and the API could stop answering requests for as long as the scans were running. The queue therefore carries the media library scans themselves now, and `MediaLibraryScanProcessorJob` awaits a scan completely before it dequeues the next one.
Waiting for a complete scan means the processor owns the execution for its whole duration, which can be hours. That is why the scope lifetime at this level matters, and why the processor does not keep a scope open around a scan: a unit of work and its `DbContext` resolved there would stay alive, and hold a database connection, for the entire scan. The processor uses one short lived scope to read the scan and a second short lived scope to prepare its job graph through `IMediaLibraryScanningService`, disposes both before the job graph starts, and executes the graph outside any scope. The jobs themselves keep creating their own scopes exactly as decided above, so the `IServiceScopeFactory` decision and the sample above are unchanged.
The scan's job graph still has exactly one top level job, and the scanner contract now makes that structural: `IMediaTypeScanner.CreateScanJobsForLibrary` returns `IMediaLibraryScanDiscoveryJob`, implemented by the file system discovery jobs, instead of a collection of jobs. Because every job awaits its children, awaiting that discovery job is what tells the processor that the scan, and with it the execution it took over from the queue, is over. Without that invariant, the processor could move on to the next scan while work of the previous one was still running and its scopes were already disposed.
@@ -40,3 +40,11 @@ Change the processor to await the root job completion and observe its exceptions
Keep the current behavior of swallowing unexpected exceptions.
**Rejected**: It leaves scans stuck in the `Running` state and leaks resources, which the whole point of this decision is to fix.
## Amendment: Awaiting the Scan in the Serialized Scan Processor (2026-10-31)
The rejection of alternative 1 rested on an assumption that no longer holds: that the queue consumer must never block. That assumption produced the defect that forced this amendment. Launching every root job in a fire-and-forget manner meant that scanning several libraries at once ran one complete job chain per library simultaneously, each of them fanning out to as many parallel file reads as the machine has processors, all on the thread pool shared with the web server, so the API could become unresponsive for the duration of the scans. Media library scans are now deliberately serialized, one at a time. The next queued scan must not start before the current one has stopped, which is only guaranteed if the processor waits for it.
Alternative 1 is therefore the decision, and the fire-and-forget launch is removed. The failure semantics decided here are unchanged: every job still catches its own failures and publishes the `LibraryScanFailedDomainEvent`, `OperationCanceledException` still marks the job as `Canceled`, and the handlers still release the scan processing resources.
Because the processor must keep draining the queue after a failed scan, a scan that fails must never affect the scans queued behind it. This matters the most for the action that starts several scans at once: a single library that cannot be scanned, for example because its type has no scanner yet, must not stop the libraries queued behind it. A failure that happens while a scan is being prepared now publishes the same failure event for that scan only, instead of being swallowed, and the processor moves on to the next queued scan.