From c7cc9945322eb0eb35ce5baf111a11da147a0d21 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 31 Mar 2024 14:57:55 -0500 Subject: [PATCH] Fix:Handle enabling/disabling library watchers #2775 --- server/Watcher.js | 25 +++++++++++++++++++++---- server/controllers/LibraryController.js | 7 +++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/server/Watcher.js b/server/Watcher.js index 99318a7e..de372865 100644 --- a/server/Watcher.js +++ b/server/Watcher.js @@ -103,15 +103,28 @@ class FolderWatcher extends EventEmitter { this.buildLibraryWatcher(library) } + /** + * + * @param {import('./objects/Library')} library + */ updateLibrary(library) { - if (this.disabled || library.settings.disableWatcher) return - var libwatcher = this.libraryWatchers.find(lib => lib.id === library.id) + if (this.disabled) return + + const libwatcher = this.libraryWatchers.find(lib => lib.id === library.id) if (libwatcher) { + // Library watcher was disabled + if (library.settings.disableWatcher) { + Logger.info(`[Watcher] updateLibrary: Library "${library.name}" watcher disabled`) + libwatcher.watcher.close() + this.libraryWatchers = this.libraryWatchers.filter(lw => lw.id !== libwatcher.id) + return + } + libwatcher.name = library.name // If any folder paths were added or removed then re-init watcher - var pathsToAdd = library.folderPaths.filter(path => !libwatcher.paths.includes(path)) - var pathsRemoved = libwatcher.paths.filter(path => !library.folderPaths.includes(path)) + const pathsToAdd = library.folderPaths.filter(path => !libwatcher.paths.includes(path)) + const pathsRemoved = libwatcher.paths.filter(path => !library.folderPaths.includes(path)) if (pathsToAdd.length || pathsRemoved.length) { Logger.info(`[Watcher] Re-Initializing watcher for "${library.name}".`) @@ -119,6 +132,10 @@ class FolderWatcher extends EventEmitter { this.libraryWatchers = this.libraryWatchers.filter(lw => lw.id !== libwatcher.id) this.buildLibraryWatcher(library) } + } else if (!library.settings.disableWatcher) { + // Library watcher was enabled + Logger.info(`[Watcher] updateLibrary: Library "${library.name}" watcher enabled - initializing`) + this.buildLibraryWatcher(library) } } diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index a0233dd2..30c9c0d9 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -128,7 +128,14 @@ class LibraryController { res.json(libraryDownloadQueueDetails) } + /** + * PATCH: /api/libraries/:id + * + * @param {import('express').Request} req + * @param {import('express').Response} res + */ async update(req, res) { + /** @type {import('../objects/Library')} */ const library = req.library // Validate that the custom provider exists if given any