From 7e05804bcfc5f878fcfe9c7b0052a38ea1cba8a2 Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 31 Aug 2022 17:39:02 -0500 Subject: [PATCH] Update:Lock file update scans from watcher and queue file updates so that 2 watcher scans never occur simultaneously #906 --- server/scanner/Scanner.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/server/scanner/Scanner.js b/server/scanner/Scanner.js index a67e0589..9a886cce 100644 --- a/server/scanner/Scanner.js +++ b/server/scanner/Scanner.js @@ -28,6 +28,10 @@ class Scanner { this.cancelLibraryScan = {} this.librariesScanning = [] + // Watcher file update scan vars + this.pendingFileUpdatesToScan = [] + this.scanningFilesChanged = false + this.bookFinder = new BookFinder() } @@ -494,7 +498,16 @@ class Scanner { } async scanFilesChanged(fileUpdates) { - if (!fileUpdates.length) return + if (!fileUpdates || !fileUpdates.length) return + + // If already scanning files from watcher then add these updates to queue + if (this.scanningFilesChanged) { + this.pendingFileUpdatesToScan.push(fileUpdates) + Logger.debug(`[Scanner] Already scanning files from watcher - file updates pushed to queue (size ${this.pendingFileUpdatesToScan.length})`) + return + } + this.scanningFilesChanged = true + // files grouped by folder var folderGroups = this.getFileUpdatesGrouped(fileUpdates) @@ -520,6 +533,13 @@ class Scanner { var folderScanResults = await this.scanFolderUpdates(library, folder, fileUpdateGroup) Logger.debug(`[Scanner] Folder scan results`, folderScanResults) } + + this.scanningFilesChanged = false + + if (this.pendingFileUpdatesToScan.length) { + Logger.debug(`[Scanner] File updates finished scanning with more updates in queue (${this.pendingFileUpdatesToScan.length})`) + this.scanFilesChanged(this.pendingFileUpdatesToScan.shift()) + } } async scanFolderUpdates(library, folder, fileUpdateGroup) {