Skip to content

Commit

Permalink
Fix:Automatic library scans using stale copy of library object result…
Browse files Browse the repository at this point in the history
…ing in reverting saved changes to it advplyr#3079 advplyr#2894
  • Loading branch information
advplyr authored and nichwall committed Jun 21, 2024
1 parent b4a4483 commit dd0ffd6
Showing 1 changed file with 32 additions and 21 deletions.
53 changes: 32 additions & 21 deletions server/managers/CronManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class CronManager {

/**
* Initialize library scan crons & podcast download crons
* @param {oldLibrary[]} libraries
* @param {import('../objects/Library')[]} libraries
*/
async init(libraries) {
this.initLibraryScanCrons(libraries)
Expand All @@ -25,7 +25,7 @@ class CronManager {

/**
* Initialize library scan crons
* @param {oldLibrary[]} libraries
* @param {import('../objects/Library')[]} libraries
*/
initLibraryScanCrons(libraries) {
for (const library of libraries) {
Expand All @@ -35,27 +35,37 @@ class CronManager {
}
}

startCronForLibrary(library) {
Logger.debug(`[CronManager] Init library scan cron for ${library.name} on schedule ${library.settings.autoScanCronExpression}`)
const libScanCron = cron.schedule(library.settings.autoScanCronExpression, () => {
Logger.debug(`[CronManager] Library scan cron executing for ${library.name}`)
LibraryScanner.scan(library)
/**
* Start cron schedule for library
*
* @param {import('../objects/Library')} _library
*/
startCronForLibrary(_library) {
Logger.debug(`[CronManager] Init library scan cron for ${_library.name} on schedule ${_library.settings.autoScanCronExpression}`)
const libScanCron = cron.schedule(_library.settings.autoScanCronExpression, async () => {
const library = await Database.libraryModel.getOldById(_library.id)
if (!library) {
Logger.error(`[CronManager] Library not found for scan cron ${_library.id}`)
} else {
Logger.debug(`[CronManager] Library scan cron executing for ${library.name}`)
LibraryScanner.scan(library)
}
})
this.libraryScanCrons.push({
libraryId: library.id,
expression: library.settings.autoScanCronExpression,
libraryId: _library.id,
expression: _library.settings.autoScanCronExpression,
task: libScanCron
})
}

removeCronForLibrary(library) {
Logger.debug(`[CronManager] Removing library scan cron for ${library.name}`)
this.libraryScanCrons = this.libraryScanCrons.filter(lsc => lsc.libraryId !== library.id)
this.libraryScanCrons = this.libraryScanCrons.filter((lsc) => lsc.libraryId !== library.id)
}

updateLibraryScanCron(library) {
const expression = library.settings.autoScanCronExpression
const existingCron = this.libraryScanCrons.find(lsc => lsc.libraryId === library.id)
const existingCron = this.libraryScanCrons.find((lsc) => lsc.libraryId === library.id)

if (!expression && existingCron) {
if (existingCron.task.stop) existingCron.task.stop()
Expand Down Expand Up @@ -128,7 +138,7 @@ class CronManager {
}

async executePodcastCron(expression) {
const podcastCron = this.podcastCrons.find(cron => cron.expression === expression)
const podcastCron = this.podcastCrons.find((cron) => cron.expression === expression)
if (!podcastCron) {
Logger.error(`[CronManager] Podcast cron not found for expression ${expression}`)
return
Expand All @@ -144,7 +154,7 @@ class CronManager {
const libraryItem = await Database.libraryItemModel.getOldById(libraryItemId)
if (!libraryItem) {
Logger.error(`[CronManager] Library item ${libraryItemId} not found for episode check cron ${expression}`)
podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter(lid => lid !== libraryItemId) // Filter it out
podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter((lid) => lid !== libraryItemId) // Filter it out
} else {
libraryItems.push(libraryItem)
}
Expand All @@ -153,8 +163,9 @@ class CronManager {
// Run episode checks
for (const libraryItem of libraryItems) {
const keepAutoDownloading = await this.podcastManager.runEpisodeCheck(libraryItem)
if (!keepAutoDownloading) { // auto download was disabled
podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter(lid => lid !== libraryItem.id) // Filter it out
if (!keepAutoDownloading) {
// auto download was disabled
podcastCron.libraryItemIds = podcastCron.libraryItemIds.filter((lid) => lid !== libraryItem.id) // Filter it out
}
}

Expand All @@ -165,28 +176,28 @@ class CronManager {
}

Logger.debug(`[CronManager] Finished executing podcast cron ${expression} for ${libraryItems.length} item(s)`)
this.podcastCronExpressionsExecuting = this.podcastCronExpressionsExecuting.filter(exp => exp !== expression)
this.podcastCronExpressionsExecuting = this.podcastCronExpressionsExecuting.filter((exp) => exp !== expression)
}

removePodcastEpisodeCron(podcastCron) {
Logger.info(`[CronManager] Stopping & removing podcast episode cron for ${podcastCron.expression}`)
if (podcastCron.task) podcastCron.task.stop()
this.podcastCrons = this.podcastCrons.filter(pc => pc.expression !== podcastCron.expression)
this.podcastCrons = this.podcastCrons.filter((pc) => pc.expression !== podcastCron.expression)
}

checkUpdatePodcastCron(libraryItem) {
// Remove from old cron by library item id
const existingCron = this.podcastCrons.find(pc => pc.libraryItemIds.includes(libraryItem.id))
const existingCron = this.podcastCrons.find((pc) => pc.libraryItemIds.includes(libraryItem.id))
if (existingCron) {
existingCron.libraryItemIds = existingCron.libraryItemIds.filter(lid => lid !== libraryItem.id)
existingCron.libraryItemIds = existingCron.libraryItemIds.filter((lid) => lid !== libraryItem.id)
if (!existingCron.libraryItemIds.length) {
this.removePodcastEpisodeCron(existingCron)
}
}

// Add to cron or start new cron
if (libraryItem.media.autoDownloadEpisodes && libraryItem.media.autoDownloadSchedule) {
const cronMatchingExpression = this.podcastCrons.find(pc => pc.expression === libraryItem.media.autoDownloadSchedule)
const cronMatchingExpression = this.podcastCrons.find((pc) => pc.expression === libraryItem.media.autoDownloadSchedule)
if (cronMatchingExpression) {
cronMatchingExpression.libraryItemIds.push(libraryItem.id)
Logger.info(`[CronManager] Added podcast "${libraryItem.media.metadata.title}" to auto dl episode cron "${cronMatchingExpression.expression}"`)
Expand All @@ -196,4 +207,4 @@ class CronManager {
}
}
}
module.exports = CronManager
module.exports = CronManager

0 comments on commit dd0ffd6

Please sign in to comment.