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

@@ -41,7 +41,7 @@ public class CancelLibrariesScanEndpoint : BaseEndpoint<EmptyRequest, IResult>
}
/// <summary>
/// Cancels the running scans of all the media libraries.
/// Cancels the running and queued scans of all the media libraries.
/// </summary>
/// <param name="request">The request object.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -19,7 +19,7 @@ public class CancelLibrariesScanEndpointSummary : Summary<CancelLibrariesScanEnd
public CancelLibrariesScanEndpointSummary()
{
Summary = "Cancels the scan of all the media libraries.";
Description = "Cancels the running scans of all the media libraries.";
Description = "Cancels the running and queued scans of all the media libraries.";
Response(200, "The scan of all media libraries is cancelled.", example: new SuccessResponse(true));
}
@@ -42,7 +42,7 @@ public class CancelLibraryScanEndpoint : BaseEndpoint<CancelLibraryScanRequest,
}
/// <summary>
/// Cancels a running scan of a media library.
/// Cancels a running or queued scan of a media library.
/// </summary>
/// <param name="request">The request containing the Ids of the media library and of the scan to cancel.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
@@ -20,7 +20,7 @@ public class CancelLibraryScanEndpointSummary : Summary<CancelLibraryScanEndpoin
public CancelLibraryScanEndpointSummary()
{
Summary = "Cancels the scan of a media library.";
Description = "Cancels a running scan of the media library identified by the route.";
Description = "Cancels a running or queued scan of the media library identified by the route.";
RequestParam(r => r.LibraryId, "The unique identifier of the media library whose scan is cancelled. Required.");
RequestParam(r => r.ScanId, "The Id of the scan to cancel. Required.");
@@ -20,7 +20,7 @@ public class GetRunningLibraryScansEndpointSummary : Summary<GetRunningLibrarySc
public GetRunningLibraryScansEndpointSummary()
{
Summary = "Retrieves the running media library scans.";
Description = "Retrieves the collection of ongoing media library scans.";
Description = "Retrieves the collection of ongoing media library scans, including the scans waiting in the scan queue.";
Response(200, "The collection of ongoing media library scans is returned.", example: new SuccessResponse<LibraryScanProgressDto[]>(true, default));
}
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>إلغاء الفحص</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>جارٍ الإلغاء</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>كتاب هزلي</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>برنامج إذاعي</value>
</data>
<data name="Queued" xml:space="preserve">
<value>في قائمة الانتظار</value>
</data>
<data name="Remix" xml:space="preserve">
<value>ريمكس</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Scan Abbrechen</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Wird abgebrochen</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Comic-Heft</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Radiosendung</value>
</data>
<data name="Queued" xml:space="preserve">
<value>In Warteschlange</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Neuabmischung</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Cancel Scan</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Cancelling</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Comic Book</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Radio Show</value>
</data>
<data name="Queued" xml:space="preserve">
<value>Queued</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Remix</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Cancelar Escaneo</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Cancelando</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Cómic</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Programa de Radio</value>
</data>
<data name="Queued" xml:space="preserve">
<value>En cola</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Remezcla</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Annuler Le Scan</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Annulation en cours</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Bande Dessinée</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Émission Radio</value>
</data>
<data name="Queued" xml:space="preserve">
<value>En file d'attente</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Remixage</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Annulla Scansione</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Annullamento in corso</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Fumetto</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Programma Radiofonico</value>
</data>
<data name="Queued" xml:space="preserve">
<value>In coda</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Remissaggio</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>スキャンを中止</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>キャンセル中</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>漫画</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>ラジオ番組</value>
</data>
<data name="Queued" xml:space="preserve">
<value>待機中</value>
</data>
<data name="Remix" xml:space="preserve">
<value>リミックス</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Cancel Scan</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Cancelling</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Comic Book</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Radio Show</value>
</data>
<data name="Queued" xml:space="preserve">
<value>Queued</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Remix</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Anulează Scanarea</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Se anulează</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Bandă Desenată</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Emisiune Radio</value>
</data>
<data name="Queued" xml:space="preserve">
<value>În așteptare</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Remix</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>Скасувати Сканування</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>Скасування</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>Комікс</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>Радіопередача</value>
</data>
<data name="Queued" xml:space="preserve">
<value>У черзі</value>
</data>
<data name="Remix" xml:space="preserve">
<value>Ремікс</value>
</data>
@@ -141,6 +141,9 @@
<data name="CancelScan" xml:space="preserve">
<value>取消扫描</value>
</data>
<data name="Cancelling" xml:space="preserve">
<value>正在取消</value>
</data>
<data name="ComicBook" xml:space="preserve">
<value>漫画</value>
</data>
@@ -249,6 +252,9 @@
<data name="RadioShow" xml:space="preserve">
<value>广播节目</value>
</data>
<data name="Queued" xml:space="preserve">
<value>排队中</value>
</data>
<data name="Remix" xml:space="preserve">
<value>混音版</value>
</data>
@@ -168,7 +168,8 @@
const jsLocalizedLibraryScanStatuses = {
Completed: '@_localizer["Completed"]',
Failed: '@_localizer["Failed"]',
Canceled: '@_localizer["Canceled"]'
Canceled: '@_localizer["Canceled"]',
Cancelling: '@_localizer["Cancelling"]'
};
const jsLocalizedLibraryScanJobsStatuses = {
DiscoveringFiles: '@_localizer["DiscoveringFiles"]',
@@ -178,7 +179,8 @@
SavingScanData: '@_localizer["SavingScanData"]',
InvalidatingStaleEnrichment: '@_localizer["InvalidatingStaleEnrichment"]',
EnrichingMetadata: '@_localizer["EnrichingMetadata"]',
ResolvingArtwork: '@_localizer["ResolvingArtwork"]'
ResolvingArtwork: '@_localizer["ResolvingArtwork"]',
Queued: '@_localizer["Queued"]'
};
const jsLocalizedLibraryTypes = {
Book: '@_localizer["Book"]',
@@ -307,12 +309,17 @@
row.setAttribute('data-scan-id', scan.scanId);
statusSpan.textContent = jsLocalizedLibraryScanJobsStatuses[scan.currentJobProgress.currentOperation];
// A queued scan has no progress to show yet, so only its waiting state is displayed.
const currentOperation = scan.currentJobProgress.currentOperation;
const isQueued = currentOperation === 'Queued';
statusSpan.textContent = jsLocalizedLibraryScanJobsStatuses[currentOperation] ?? currentOperation;
statusSpan.classList.remove('hidden');
scanProgress.classList.remove('hidden');
totalProgressDiv.style.width = `${scan.overallProgressPercentage}%`;
jobProgressDiv.style.width = `${scan.currentJobProgress.progressPercentage}%`;
scanProgress.classList.toggle('hidden', isQueued);
if (!isQueued) {
totalProgressDiv.style.width = `${scan.overallProgressPercentage}%`;
jobProgressDiv.style.width = `${scan.currentJobProgress.progressPercentage}%`;
}
scanIcon.classList.add('hidden');
cancelScanIcon.classList.remove('hidden');
@@ -369,6 +376,12 @@
scanIcon.classList.add('hidden');
cancelScanIcon.classList.remove('hidden');
// The scan is queued until its turn comes, so it has no progress to show yet.
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = jsLocalizedLibraryScanJobsStatuses['Queued'];
statusSpan.classList.remove('hidden');
row.querySelector('.scan-progress').classList.add('hidden');
const scanId = scan.scanId;
await signalRConnection.invoke('SubscribeToScan', scanId);
}
@@ -401,6 +414,12 @@
scanIcon.classList.add('hidden');
cancelScanIcon.classList.remove('hidden');
// The scan is queued until its turn comes, so it has no progress to show yet.
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = jsLocalizedLibraryScanJobsStatuses['Queued'];
statusSpan.classList.remove('hidden');
row.querySelector('.scan-progress').classList.add('hidden');
const scanId = success.scanId;
await signalRConnection.invoke('SubscribeToScan', scanId);
}
@@ -410,29 +429,20 @@
// On cancel all libraries scan button click.
const handleCancelLibrariesScanClick = async function(e) {
await callApiPostAsync('@_urlService.GetAbsoluteUrl(WebRoutes.LibraryManagement.CANCEL_LIBRARIES_SCAN)', {});
btnCancelLibrariesScan.style.display = 'none';
btnScanAllLibraries.style.display = 'block';
// Toggle icons for all active scan rows.
// The cancellations are only applied once the server confirms each one, so the cancel button stays hidden meanwhile.
btnCancelLibrariesScan.style.display = 'none';
// Ask all active scan rows to show that their cancellation is in progress.
const rows = document.querySelectorAll('tr[data-scan-id]');
rows.forEach(row => {
// Remove the scan Id, its no longer being scanned.
row.removeAttribute('data-scan-id');
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = `${jsLocalizedLibraryScanStatuses['Canceled']}.`;
const scanProgress = row.querySelector('.scan-progress');
scanProgress.classList.add('hidden');
// Show scan status icon.
const completedIdon = row.querySelector('img[src*="yellow.png"]');
completedIdon.classList.remove('hidden');
statusSpan.textContent = `${jsLocalizedLibraryScanStatuses['Cancelling']}.`;
statusSpan.classList.remove('hidden');
// Toggle scan action icons.
const scanIcon = row.querySelector('img[src*="trigger.svg"]');
const cancelScanIcon = row.querySelector('img[src*="stop.svg"]');
scanIcon.classList.remove('hidden');
cancelScanIcon.classList.add('hidden');
});
};
@@ -441,6 +451,7 @@
const handleCancelLibraryScanClick = async function(e) {
if (e.target.matches('img[src*="stop.svg"]')) {
const row = e.target.closest('tr');
const scanId = row.getAttribute('data-scan-id');
const url = '@_urlService.GetAbsoluteUrl(WebRoutes.LibraryManagement.CANCEL_LIBRARY_SCAN, new { libraryId = default(Guid), scanId = default(Guid) })';
@@ -460,29 +471,18 @@
).join('/');
await callApiPostAsync(finalUrl);
// Remove the scan Id, its no longer being scanned.
row.removeAttribute('data-scan-id');
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = `${jsLocalizedLibraryScanStatuses['Canceled']}.`;
const scanProgress = row.querySelector('.scan-progress');
scanProgress.classList.add('hidden');
// The cancellation is only applied once the server confirms it, so the scan keeps its Id and is shown as being cancelled meanwhile.
// The server might have confirmed it already, in which case the row no longer carries the Id and is left as it is.
if (row.getAttribute('data-scan-id') === scanId) {
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = `${jsLocalizedLibraryScanStatuses['Cancelling']}.`;
statusSpan.classList.remove('hidden');
// Show scan status icon.
const completedIdon = row.querySelector('img[src*="yellow.png"]');
completedIdon.classList.remove('hidden');
// Toggle scan action icons.
const scanIcon = row.querySelector('img[src*="trigger.svg"]');
const cancelScanIcon = row.querySelector('img[src*="stop.svg"]');
scanIcon.classList.remove('hidden');
cancelScanIcon.classList.add('hidden');
// Toggle scan action buttons.
const hasActiveScans = document.querySelectorAll('tr[data-scan-id]').length > 0;
btnCancelLibrariesScan.style.display = hasActiveScans ? 'block' : 'none';
btnScanAllLibraries.style.display = hasActiveScans ? 'none' : 'block';
// Toggle scan action icons.
const cancelScanIcon = row.querySelector('img[src*="stop.svg"]');
cancelScanIcon.classList.add('hidden');
}
}
};
@@ -502,17 +502,21 @@
const rows = document.querySelectorAll(`tr[data-scan-id="${progress.scanId}"]`);
rows.forEach(row => {
// Update status text.
const currentOperation = progress.currentJobProgress.currentOperation;
const isQueued = currentOperation === 'Queued';
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = jsLocalizedLibraryScanJobsStatuses[progress.currentJobProgress.currentOperation];
statusSpan.textContent = jsLocalizedLibraryScanJobsStatuses[currentOperation] ?? currentOperation;
statusSpan.classList.remove('hidden');
// Update progress bars.
// Update progress bars. A queued scan has no progress to show yet.
const scanProgress = row.querySelector('.scan-progress');
const totalProgressDiv = row.querySelector('.scan-progress-total > div');
const jobProgressDiv = row.querySelector('.scan-progress-job > div');
scanProgress.classList.remove('hidden');
totalProgressDiv.style.width = `${progress.overallProgressPercentage}%`;
jobProgressDiv.style.width = `${progress.currentJobProgress.progressPercentage}%`;
scanProgress.classList.toggle('hidden', isQueued);
if (!isQueued) {
totalProgressDiv.style.width = `${progress.overallProgressPercentage}%`;
jobProgressDiv.style.width = `${progress.currentJobProgress.progressPercentage}%`;
}
// Toggle scan action icons.
const scanIcon = row.querySelector('img[src*="trigger.svg"]');
@@ -576,10 +580,38 @@
btnScanAllLibraries.style.display = hasActiveScans ? 'none' : 'block';
};
// Scan cancellation handler.
const cancelledHandler = (progress) => {
const rows = document.querySelectorAll(`tr[data-scan-id="${progress.scanId}"]`);
rows.forEach(row => {
// Remove the scan Id, its no longer being scanned.
row.removeAttribute('data-scan-id');
const statusSpan = row.querySelector('.scan-job-status');
statusSpan.textContent = `${jsLocalizedLibraryScanStatuses[progress.status]}.`;
const scanProgress = row.querySelector('.scan-progress');
scanProgress.classList.add('hidden');
// Show scan status icon.
const canceledIdon = row.querySelector('img[src*="yellow.png"]');
canceledIdon.classList.remove('hidden');
// Toggle scan action icons.
const scanIcon = row.querySelector('img[src*="trigger.svg"]');
const cancelScanIcon = row.querySelector('img[src*="stop.svg"]');
scanIcon.classList.remove('hidden');
cancelScanIcon.classList.add('hidden');
});
const hasActiveScans = document.querySelectorAll('tr[data-scan-id]').length > 0;
btnCancelLibrariesScan.style.display = hasActiveScans ? 'block' : 'none';
btnScanAllLibraries.style.display = hasActiveScans ? 'none' : 'block';
};
// Subscribe to events.
signalRConnection.on("libraryScanProgressUpdateEvent", progressHandler);
signalRConnection.on("libraryScanFinishedEvent", completionHandler);
signalRConnection.on("libraryScanFailedEvent", failureHandler);
signalRConnection.on("libraryScanCancelledEvent", cancelledHandler);
signalRConnection.onclose(onClose);
signalRConnection.onreconnecting(onReconnecting);
signalRConnection.onreconnected(onReconnected);
@@ -588,6 +620,7 @@
signalRConnection.off("libraryScanProgressUpdateEvent", progressHandler);
signalRConnection.off("libraryScanFinishedEvent", completionHandler);
signalRConnection.off("libraryScanFailedEvent", failureHandler);
signalRConnection.off("libraryScanCancelledEvent", cancelledHandler);
signalRConnection.off("close", onClose);
signalRConnection.off("reconnecting", onReconnecting);
signalRConnection.off("reconnected", onReconnected);