mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-03-07 00:19:41 +00:00
Move scanner log purge into the LogManager.
This commit is contained in:
parent
ead270abbe
commit
f069475a4e
2 changed files with 38 additions and 40 deletions
|
|
@ -36,6 +36,10 @@ class LogManager {
|
||||||
return global.ServerSettings.loggerDailyLogsToKeep || 7
|
return global.ServerSettings.loggerDailyLogsToKeep || 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get loggerScannerLogsToKeep() {
|
||||||
|
return global.ServerSettings.loggerScannerLogsToKeep || 2
|
||||||
|
}
|
||||||
|
|
||||||
async enforceDailyLogRetention() {
|
async enforceDailyLogRetention() {
|
||||||
while (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
while (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||||
await this.removeLogFile(this.dailyLogFiles[0])
|
await this.removeLogFile(this.dailyLogFiles[0])
|
||||||
|
|
@ -184,5 +188,37 @@ class LogManager {
|
||||||
getMostRecentCurrentDailyLogs() {
|
getMostRecentCurrentDailyLogs() {
|
||||||
return this.currentDailyLog?.logs.slice(-5000) || ''
|
return this.currentDailyLog?.logs.slice(-5000) || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Keep the most recent N scan logs in metadata/logs/scans.
|
||||||
|
* Where N is the server setting `loggerScannerLogsToKeep`.
|
||||||
|
*
|
||||||
|
* @param {string} [logDir]
|
||||||
|
*/
|
||||||
|
async purgeOldScanLogs(logDir = this.ScanLogPath) {
|
||||||
|
const scanLogsToKeep = this.loggerScannerLogsToKeep
|
||||||
|
|
||||||
|
let scanFiles
|
||||||
|
try {
|
||||||
|
scanFiles = await fs.readdir(logDir)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.warn(TAG, `Failed to read scan log dir "${logDir}": ${error.message}`)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const scanLogFiles = (scanFiles || []).filter((f) => Path.extname(f) === '.txt').sort()
|
||||||
|
if (scanLogFiles.length <= scanLogsToKeep) return
|
||||||
|
|
||||||
|
const filesToRemove = scanLogFiles.slice(0, scanLogFiles.length - scanLogsToKeep)
|
||||||
|
for (const file of filesToRemove) {
|
||||||
|
const fullPath = Path.join(logDir, file)
|
||||||
|
try {
|
||||||
|
await fs.unlink(fullPath)
|
||||||
|
Logger.info(TAG, `Removed scan log "${fullPath}"`)
|
||||||
|
} catch (error) {
|
||||||
|
Logger.warn(TAG, `Failed to remove scan log "${fullPath}": ${error.message}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
module.exports = LogManager
|
module.exports = LogManager
|
||||||
|
|
|
||||||
|
|
@ -30,10 +30,6 @@ class LibraryScan {
|
||||||
this.logs = []
|
this.logs = []
|
||||||
}
|
}
|
||||||
|
|
||||||
get loggerScannerLogsToKeep() {
|
|
||||||
return global.ServerSettings?.loggerScannerLogsToKeep || 2
|
|
||||||
}
|
|
||||||
|
|
||||||
get libraryId() {
|
get libraryId() {
|
||||||
return this.library.id
|
return this.library.id
|
||||||
}
|
}
|
||||||
|
|
@ -127,9 +123,7 @@ class LibraryScan {
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveLog() {
|
async saveLog() {
|
||||||
const scanLogDir = Path.join(global.MetadataPath, 'logs', 'scans')
|
const scanLogDir = Logger.logManager.ScanLogPath
|
||||||
|
|
||||||
await fs.ensureDir(scanLogDir)
|
|
||||||
|
|
||||||
const outputPath = Path.join(scanLogDir, this.logFilename)
|
const outputPath = Path.join(scanLogDir, this.logFilename)
|
||||||
const logLines = [JSON.stringify(this.toJSON())]
|
const logLines = [JSON.stringify(this.toJSON())]
|
||||||
|
|
@ -140,39 +134,7 @@ class LibraryScan {
|
||||||
|
|
||||||
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
|
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
|
||||||
|
|
||||||
await this.purgeOldScanLogs(scanLogDir)
|
await Logger.logManager?.purgeOldScanLogs()
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Keep the most recent N scan logs in metadata/logs/scans.
|
|
||||||
* Where N is the server setting `loggerScannerLogsToKeep`.
|
|
||||||
*
|
|
||||||
* @param {string} scanLogDir
|
|
||||||
*/
|
|
||||||
async purgeOldScanLogs(scanLogDir) {
|
|
||||||
const scanLogsToKeep = this.loggerScannerLogsToKeep
|
|
||||||
|
|
||||||
let scanFiles
|
|
||||||
try {
|
|
||||||
scanFiles = await fs.readdir(scanLogDir)
|
|
||||||
} catch (error) {
|
|
||||||
Logger.warn(`[LibraryScan] Failed to read scan log dir "${scanLogDir}": ${error.message}`)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const scanLogFiles = (scanFiles || []).filter((f) => Path.extname(f) === '.txt').sort()
|
|
||||||
if (scanLogFiles.length <= scanLogsToKeep) return
|
|
||||||
|
|
||||||
const filesToRemove = scanLogFiles.slice(0, scanLogFiles.length - scanLogsToKeep)
|
|
||||||
for (const file of filesToRemove) {
|
|
||||||
const fullPath = Path.join(scanLogDir, file)
|
|
||||||
try {
|
|
||||||
await fs.unlink(fullPath)
|
|
||||||
Logger.info(`[LibraryScan] Removed scan log "${fullPath}"`)
|
|
||||||
} catch (error) {
|
|
||||||
Logger.warn(`[LibraryScan] Failed to remove scan log "${fullPath}": ${error.message}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
module.exports = LibraryScan
|
module.exports = LibraryScan
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue