mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-08-01 08:44:40 +02:00
Update watcher scanner to show task notification
This commit is contained in:
parent
bef6549805
commit
d7264f8c22
3 changed files with 53 additions and 8 deletions
|
@ -13,6 +13,7 @@ const TaskManager = require('../managers/TaskManager')
|
|||
const LibraryItemScanner = require('./LibraryItemScanner')
|
||||
const LibraryScan = require('./LibraryScan')
|
||||
const LibraryItemScanData = require('./LibraryItemScanData')
|
||||
const Task = require('../objects/Task')
|
||||
|
||||
class LibraryScanner {
|
||||
constructor() {
|
||||
|
@ -20,7 +21,7 @@ class LibraryScanner {
|
|||
this.librariesScanning = []
|
||||
|
||||
this.scanningFilesChanged = false
|
||||
/** @type {import('../Watcher').PendingFileUpdate[][]} */
|
||||
/** @type {[import('../Watcher').PendingFileUpdate[], Task][]} */
|
||||
this.pendingFileUpdatesToScan = []
|
||||
}
|
||||
|
||||
|
@ -335,18 +336,25 @@ class LibraryScanner {
|
|||
/**
|
||||
* Scan files changed from Watcher
|
||||
* @param {import('../Watcher').PendingFileUpdate[]} fileUpdates
|
||||
* @param {Task} pendingTask
|
||||
*/
|
||||
async scanFilesChanged(fileUpdates) {
|
||||
async scanFilesChanged(fileUpdates, pendingTask) {
|
||||
if (!fileUpdates?.length) return
|
||||
|
||||
// If already scanning files from watcher then add these updates to queue
|
||||
if (this.scanningFilesChanged) {
|
||||
this.pendingFileUpdatesToScan.push(fileUpdates)
|
||||
this.pendingFileUpdatesToScan.push([fileUpdates, pendingTask])
|
||||
Logger.debug(`[LibraryScanner] Already scanning files from watcher - file updates pushed to queue (size ${this.pendingFileUpdatesToScan.length})`)
|
||||
return
|
||||
}
|
||||
this.scanningFilesChanged = true
|
||||
|
||||
const results = {
|
||||
added: 0,
|
||||
updated: 0,
|
||||
removed: 0
|
||||
}
|
||||
|
||||
// files grouped by folder
|
||||
const folderGroups = this.getFileUpdatesGrouped(fileUpdates)
|
||||
|
||||
|
@ -377,17 +385,42 @@ class LibraryScanner {
|
|||
const folderScanResults = await this.scanFolderUpdates(library, folder, fileUpdateGroup)
|
||||
Logger.debug(`[LibraryScanner] Folder scan results`, folderScanResults)
|
||||
|
||||
// Tally results to share with client
|
||||
let resetFilterData = false
|
||||
Object.values(folderScanResults).forEach((scanResult) => {
|
||||
if (scanResult === ScanResult.ADDED) {
|
||||
resetFilterData = true
|
||||
results.added++
|
||||
} else if (scanResult === ScanResult.REMOVED) {
|
||||
resetFilterData = true
|
||||
results.removed++
|
||||
} else if (scanResult === ScanResult.UPDATED) {
|
||||
resetFilterData = true
|
||||
results.updated++
|
||||
}
|
||||
})
|
||||
|
||||
// If something was updated then reset numIssues filter data for library
|
||||
if (Object.values(folderScanResults).some(scanResult => scanResult !== ScanResult.NOTHING && scanResult !== ScanResult.UPTODATE)) {
|
||||
if (resetFilterData) {
|
||||
await Database.resetLibraryIssuesFilterData(libraryId)
|
||||
}
|
||||
}
|
||||
|
||||
// Complete task and send results to client
|
||||
const resultStrs = []
|
||||
if (results.added) resultStrs.push(`${results.added} added`)
|
||||
if (results.updated) resultStrs.push(`${results.updated} updated`)
|
||||
if (results.removed) resultStrs.push(`${results.removed} missing`)
|
||||
let scanResultStr = 'Scan finished with no changes'
|
||||
if (resultStrs.length) scanResultStr = resultStrs.join(', ')
|
||||
pendingTask.setFinished(scanResultStr)
|
||||
TaskManager.taskFinished(pendingTask)
|
||||
|
||||
this.scanningFilesChanged = false
|
||||
|
||||
if (this.pendingFileUpdatesToScan.length) {
|
||||
Logger.debug(`[LibraryScanner] File updates finished scanning with more updates in queue (${this.pendingFileUpdatesToScan.length})`)
|
||||
this.scanFilesChanged(this.pendingFileUpdatesToScan.shift())
|
||||
this.scanFilesChanged(...this.pendingFileUpdatesToScan.shift())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue