Update FolderWatcher to singleton

This commit is contained in:
advplyr 2024-11-07 16:32:05 -06:00
parent a8ec07cfc9
commit a5ebd89817
6 changed files with 17 additions and 19 deletions

View File

@ -62,7 +62,6 @@ class Server {
fs.mkdirSync(global.MetadataPath) fs.mkdirSync(global.MetadataPath)
} }
this.watcher = new Watcher()
this.auth = new Auth() this.auth = new Auth()
// Managers // Managers
@ -70,7 +69,7 @@ class Server {
this.backupManager = new BackupManager() this.backupManager = new BackupManager()
this.abMergeManager = new AbMergeManager() this.abMergeManager = new AbMergeManager()
this.playbackSessionManager = new PlaybackSessionManager() this.playbackSessionManager = new PlaybackSessionManager()
this.podcastManager = new PodcastManager(this.watcher) this.podcastManager = new PodcastManager()
this.audioMetadataManager = new AudioMetadataMangaer() this.audioMetadataManager = new AudioMetadataMangaer()
this.rssFeedManager = new RssFeedManager() this.rssFeedManager = new RssFeedManager()
this.cronManager = new CronManager(this.podcastManager, this.playbackSessionManager) this.cronManager = new CronManager(this.podcastManager, this.playbackSessionManager)
@ -147,9 +146,9 @@ class Server {
if (Database.serverSettings.scannerDisableWatcher) { if (Database.serverSettings.scannerDisableWatcher) {
Logger.info(`[Server] Watcher is disabled`) Logger.info(`[Server] Watcher is disabled`)
this.watcher.disabled = true Watcher.disabled = true
} else { } else {
this.watcher.initWatcher(libraries) Watcher.initWatcher(libraries)
} }
} }
@ -435,7 +434,7 @@ class Server {
*/ */
async stop() { async stop() {
Logger.info('=== Stopping Server ===') Logger.info('=== Stopping Server ===')
await this.watcher.close() Watcher.close()
Logger.info('Watcher Closed') Logger.info('Watcher Closed')
return new Promise((resolve) => { return new Promise((resolve) => {

View File

@ -409,4 +409,4 @@ class FolderWatcher extends EventEmitter {
}, 5000) }, 5000)
} }
} }
module.exports = FolderWatcher module.exports = new FolderWatcher()

View File

@ -17,6 +17,7 @@ const naturalSort = createNewSortInstance({
const LibraryScanner = require('../scanner/LibraryScanner') const LibraryScanner = require('../scanner/LibraryScanner')
const Scanner = require('../scanner/Scanner') const Scanner = require('../scanner/Scanner')
const Database = require('../Database') const Database = require('../Database')
const Watcher = require('../Watcher')
const libraryFilters = require('../utils/queries/libraryFilters') const libraryFilters = require('../utils/queries/libraryFilters')
const libraryItemsPodcastFilters = require('../utils/queries/libraryItemsPodcastFilters') const libraryItemsPodcastFilters = require('../utils/queries/libraryItemsPodcastFilters')
const authorFilters = require('../utils/queries/authorFilters') const authorFilters = require('../utils/queries/authorFilters')
@ -158,7 +159,7 @@ class LibraryController {
SocketAuthority.emitter('library_added', library.toOldJSON(), userFilter) SocketAuthority.emitter('library_added', library.toOldJSON(), userFilter)
// Add library watcher // Add library watcher
this.watcher.addLibrary(library) Watcher.addLibrary(library)
res.json(library.toOldJSON()) res.json(library.toOldJSON())
} }
@ -440,7 +441,7 @@ class LibraryController {
req.library.libraryFolders = await req.library.getLibraryFolders() req.library.libraryFolders = await req.library.getLibraryFolders()
// Update watcher // Update watcher
this.watcher.updateLibrary(req.library) Watcher.updateLibrary(req.library)
hasUpdates = true hasUpdates = true
} }
@ -466,7 +467,7 @@ class LibraryController {
*/ */
async delete(req, res) { async delete(req, res) {
// Remove library watcher // Remove library watcher
this.watcher.removeLibrary(req.library) Watcher.removeLibrary(req.library)
// Remove collections for library // Remove collections for library
const numCollectionsRemoved = await Database.collectionModel.removeAllForLibrary(req.library.id) const numCollectionsRemoved = await Database.collectionModel.removeAllForLibrary(req.library.id)

View File

@ -5,6 +5,7 @@ const fs = require('../libs/fsExtra')
const Logger = require('../Logger') const Logger = require('../Logger')
const SocketAuthority = require('../SocketAuthority') const SocketAuthority = require('../SocketAuthority')
const Database = require('../Database') const Database = require('../Database')
const Watcher = require('../Watcher')
const libraryItemFilters = require('../utils/queries/libraryItemFilters') const libraryItemFilters = require('../utils/queries/libraryItemFilters')
const patternValidation = require('../libs/nodeCron/pattern-validation') const patternValidation = require('../libs/nodeCron/pattern-validation')
@ -557,10 +558,10 @@ class MiscController {
switch (type) { switch (type) {
case 'add': case 'add':
this.watcher.onFileAdded(libraryId, path) Watcher.onFileAdded(libraryId, path)
break break
case 'unlink': case 'unlink':
this.watcher.onFileRemoved(libraryId, path) Watcher.onFileRemoved(libraryId, path)
break break
case 'rename': case 'rename':
const oldPath = req.body.oldPath const oldPath = req.body.oldPath
@ -568,7 +569,7 @@ class MiscController {
Logger.error(`[MiscController] Invalid request body for updateWatchedPath. oldPath is required for rename.`) Logger.error(`[MiscController] Invalid request body for updateWatchedPath. oldPath is required for rename.`)
return res.sendStatus(400) return res.sendStatus(400)
} }
this.watcher.onFileRename(libraryId, oldPath, path) Watcher.onFileRename(libraryId, oldPath, path)
break break
default: default:
Logger.error(`[MiscController] Invalid type for updateWatchedPath. type: "${type}"`) Logger.error(`[MiscController] Invalid type for updateWatchedPath. type: "${type}"`)

View File

@ -1,6 +1,7 @@
const Logger = require('../Logger') const Logger = require('../Logger')
const SocketAuthority = require('../SocketAuthority') const SocketAuthority = require('../SocketAuthority')
const Database = require('../Database') const Database = require('../Database')
const Watcher = require('../Watcher')
const fs = require('../libs/fsExtra') const fs = require('../libs/fsExtra')
@ -23,9 +24,7 @@ const AudioFile = require('../objects/files/AudioFile')
const LibraryItem = require('../objects/LibraryItem') const LibraryItem = require('../objects/LibraryItem')
class PodcastManager { class PodcastManager {
constructor(watcher) { constructor() {
this.watcher = watcher
this.downloadQueue = [] this.downloadQueue = []
this.currentDownload = null this.currentDownload = null
@ -97,7 +96,7 @@ class PodcastManager {
} }
// Ignores all added files to this dir // Ignores all added files to this dir
this.watcher.addIgnoreDir(this.currentDownload.libraryItem.path) Watcher.addIgnoreDir(this.currentDownload.libraryItem.path)
// Make sure podcast library item folder exists // Make sure podcast library item folder exists
if (!(await fs.pathExists(this.currentDownload.libraryItem.path))) { if (!(await fs.pathExists(this.currentDownload.libraryItem.path))) {
@ -151,7 +150,7 @@ class PodcastManager {
SocketAuthority.emitter('episode_download_finished', this.currentDownload.toJSONForClient()) SocketAuthority.emitter('episode_download_finished', this.currentDownload.toJSONForClient())
SocketAuthority.emitter('episode_download_queue_updated', this.getDownloadQueueDetails()) SocketAuthority.emitter('episode_download_queue_updated', this.getDownloadQueueDetails())
this.watcher.removeIgnoreDir(this.currentDownload.libraryItem.path) Watcher.removeIgnoreDir(this.currentDownload.libraryItem.path)
this.currentDownload = null this.currentDownload = null
if (this.downloadQueue.length) { if (this.downloadQueue.length) {
this.startPodcastEpisodeDownload(this.downloadQueue.shift()) this.startPodcastEpisodeDownload(this.downloadQueue.shift())

View File

@ -45,8 +45,6 @@ class ApiRouter {
this.abMergeManager = Server.abMergeManager this.abMergeManager = Server.abMergeManager
/** @type {import('../managers/BackupManager')} */ /** @type {import('../managers/BackupManager')} */
this.backupManager = Server.backupManager this.backupManager = Server.backupManager
/** @type {import('../Watcher')} */
this.watcher = Server.watcher
/** @type {import('../managers/PodcastManager')} */ /** @type {import('../managers/PodcastManager')} */
this.podcastManager = Server.podcastManager this.podcastManager = Server.podcastManager
/** @type {import('../managers/AudioMetadataManager')} */ /** @type {import('../managers/AudioMetadataManager')} */