mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-02-01 00:18:14 +01:00
scanLibrary fail and cancel handling round 2
This commit is contained in:
parent
1099dbe642
commit
f8034e1b78
@ -75,13 +75,14 @@ class LibraryScan {
|
|||||||
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
|
return date.format(new Date(), 'YYYY-MM-DD') + '_' + this.id + '.txt'
|
||||||
}
|
}
|
||||||
get scanResultsString() {
|
get scanResultsString() {
|
||||||
if (this.error) return this.error
|
|
||||||
const strs = []
|
const strs = []
|
||||||
if (this.resultsAdded) strs.push(`${this.resultsAdded} added`)
|
if (this.resultsAdded) strs.push(`${this.resultsAdded} added`)
|
||||||
if (this.resultsUpdated) strs.push(`${this.resultsUpdated} updated`)
|
if (this.resultsUpdated) strs.push(`${this.resultsUpdated} updated`)
|
||||||
if (this.resultsMissing) strs.push(`${this.resultsMissing} missing`)
|
if (this.resultsMissing) strs.push(`${this.resultsMissing} missing`)
|
||||||
if (!strs.length) return `Everything was up to date (${elapsedPretty(this.elapsed / 1000)})`
|
const changesDetected = strs.length > 0 ? strs.join(', ') : 'No changes detected'
|
||||||
return strs.join(', ') + ` (${elapsedPretty(this.elapsed / 1000)})`
|
const timeElapsed = `(${elapsedPretty(this.elapsed / 1000)})`
|
||||||
|
const error = this.error ? `${this.error}. ` : ''
|
||||||
|
return `${error}${changesDetected} ${timeElapsed}`
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON() {
|
toJSON() {
|
||||||
|
@ -81,52 +81,37 @@ class LibraryScanner {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const canceled = await this.scanLibrary(libraryScan, forceRescan)
|
const canceled = await this.scanLibrary(libraryScan, forceRescan)
|
||||||
|
|
||||||
if (canceled) {
|
|
||||||
Logger.info(`[LibraryScanner] Library scan canceled for "${libraryScan.libraryName}"`)
|
|
||||||
delete this.cancelLibraryScan[libraryScan.libraryId]
|
|
||||||
}
|
|
||||||
|
|
||||||
libraryScan.setComplete()
|
libraryScan.setComplete()
|
||||||
|
|
||||||
Logger.info(`[LibraryScanner] Library scan ${libraryScan.id} completed in ${libraryScan.elapsedTimestamp} | ${libraryScan.resultStats}`)
|
Logger.info(`[LibraryScanner] Library scan "${libraryScan.id}" ${canceled ? 'canceled after' : 'completed in'} ${libraryScan.elapsedTimestamp} | ${libraryScan.resultStats}`)
|
||||||
|
|
||||||
if (canceled && !libraryScan.totalResults) {
|
if (!canceled) {
|
||||||
task.setFinished('Scan canceled')
|
library.lastScan = Date.now()
|
||||||
TaskManager.taskFinished(task)
|
library.lastScanVersion = packageJson.version
|
||||||
|
if (library.isBook) {
|
||||||
const emitData = libraryScan.getScanEmitData
|
const newExtraData = library.extraData || {}
|
||||||
emitData.results = null
|
newExtraData.lastScanMetadataPrecedence = library.settings.metadataPrecedence
|
||||||
return
|
library.extraData = newExtraData
|
||||||
|
library.changed('extraData', true)
|
||||||
|
}
|
||||||
|
await library.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
library.lastScan = Date.now()
|
task.setFinished(`${canceled ? 'Canceled' : 'Completed'}. ${libraryScan.scanResultsString}`)
|
||||||
library.lastScanVersion = packageJson.version
|
|
||||||
if (library.isBook) {
|
|
||||||
const newExtraData = library.extraData || {}
|
|
||||||
newExtraData.lastScanMetadataPrecedence = library.settings.metadataPrecedence
|
|
||||||
library.extraData = newExtraData
|
|
||||||
library.changed('extraData', true)
|
|
||||||
}
|
|
||||||
await library.save()
|
|
||||||
|
|
||||||
task.setFinished(libraryScan.scanResultsString)
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
libraryScan.setComplete(err)
|
libraryScan.setComplete(err)
|
||||||
Logger.error(`[LibraryScanner] Library scan ${libraryScan.id} failed after ${libraryScan.elapsedTimestamp}.`, err)
|
|
||||||
|
|
||||||
if (this.cancelLibraryScan[libraryScan.libraryId]) delete this.cancelLibraryScan[libraryScan.libraryId]
|
Logger.error(`[LibraryScanner] Library scan ${libraryScan.id} failed after ${libraryScan.elapsedTimestamp} | ${libraryScan.resultStats}.`, err)
|
||||||
|
|
||||||
task.setFailed(`Scan failed: ${err.message}`)
|
task.setFailed(`Failed. ${libraryScan.scanResultsString}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (this.cancelLibraryScan[libraryScan.libraryId]) delete this.cancelLibraryScan[libraryScan.libraryId]
|
||||||
this.librariesScanning = this.librariesScanning.filter((ls) => ls.id !== library.id)
|
this.librariesScanning = this.librariesScanning.filter((ls) => ls.id !== library.id)
|
||||||
|
|
||||||
TaskManager.taskFinished(task)
|
TaskManager.taskFinished(task)
|
||||||
|
|
||||||
if (libraryScan.totalResults) {
|
libraryScan.saveLog()
|
||||||
libraryScan.saveLog()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -151,7 +136,7 @@ class LibraryScanner {
|
|||||||
libraryItemDataFound = libraryItemDataFound.concat(itemDataFoundInFolder)
|
libraryItemDataFound = libraryItemDataFound.concat(itemDataFoundInFolder)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
if (this.shouldCancelScan(libraryScan)) return true
|
||||||
|
|
||||||
const existingLibraryItems = await Database.libraryItemModel.findAll({
|
const existingLibraryItems = await Database.libraryItemModel.findAll({
|
||||||
where: {
|
where: {
|
||||||
@ -159,7 +144,7 @@ class LibraryScanner {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
if (this.shouldCancelScan(libraryScan)) return true
|
||||||
|
|
||||||
const libraryItemIdsMissing = []
|
const libraryItemIdsMissing = []
|
||||||
let oldLibraryItemsUpdated = []
|
let oldLibraryItemsUpdated = []
|
||||||
@ -227,7 +212,7 @@ class LibraryScanner {
|
|||||||
oldLibraryItemsUpdated = []
|
oldLibraryItemsUpdated = []
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
if (this.shouldCancelScan(libraryScan)) return true
|
||||||
}
|
}
|
||||||
// Emit item updates to client
|
// Emit item updates to client
|
||||||
if (oldLibraryItemsUpdated.length) {
|
if (oldLibraryItemsUpdated.length) {
|
||||||
@ -258,7 +243,7 @@ class LibraryScanner {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
if (this.shouldCancelScan(libraryScan)) return true
|
||||||
|
|
||||||
// Add new library items
|
// Add new library items
|
||||||
if (libraryItemDataFound.length) {
|
if (libraryItemDataFound.length) {
|
||||||
@ -282,7 +267,7 @@ class LibraryScanner {
|
|||||||
newOldLibraryItems = []
|
newOldLibraryItems = []
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.cancelLibraryScan[libraryScan.libraryId]) return true
|
if (this.shouldCancelScan(libraryScan)) return true
|
||||||
}
|
}
|
||||||
// Emit new items to client
|
// Emit new items to client
|
||||||
if (newOldLibraryItems.length) {
|
if (newOldLibraryItems.length) {
|
||||||
@ -293,6 +278,17 @@ class LibraryScanner {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
libraryScan.addLog(LogLevel.INFO, `Scan completed. ${libraryScan.resultStats}`)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
shouldCancelScan(libraryScan) {
|
||||||
|
if (this.cancelLibraryScan[libraryScan.libraryId]) {
|
||||||
|
libraryScan.addLog(LogLevel.INFO, `Scan canceled. ${libraryScan.resultStats}`)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user