Move scanner log purge into the LogManager.

This commit is contained in:
sir-wilhelm 2026-02-16 14:45:54 -06:00
parent ead270abbe
commit f069475a4e
2 changed files with 38 additions and 40 deletions

View file

@ -36,6 +36,10 @@ class LogManager {
return global.ServerSettings.loggerDailyLogsToKeep || 7
}
get loggerScannerLogsToKeep() {
return global.ServerSettings.loggerScannerLogsToKeep || 2
}
async enforceDailyLogRetention() {
while (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
await this.removeLogFile(this.dailyLogFiles[0])
@ -184,5 +188,37 @@ class LogManager {
getMostRecentCurrentDailyLogs() {
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

View file

@ -30,10 +30,6 @@ class LibraryScan {
this.logs = []
}
get loggerScannerLogsToKeep() {
return global.ServerSettings?.loggerScannerLogsToKeep || 2
}
get libraryId() {
return this.library.id
}
@ -127,9 +123,7 @@ class LibraryScan {
}
async saveLog() {
const scanLogDir = Path.join(global.MetadataPath, 'logs', 'scans')
await fs.ensureDir(scanLogDir)
const scanLogDir = Logger.logManager.ScanLogPath
const outputPath = Path.join(scanLogDir, this.logFilename)
const logLines = [JSON.stringify(this.toJSON())]
@ -140,39 +134,7 @@ class LibraryScan {
Logger.info(`[LibraryScan] Scan log saved "${outputPath}"`)
await this.purgeOldScanLogs(scanLogDir)
}
/**
* 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}`)
}
}
await Logger.logManager?.purgeOldScanLogs()
}
}
module.exports = LibraryScan