diff --git a/server/Server.js b/server/Server.js index 69061573..9b164291 100644 --- a/server/Server.js +++ b/server/Server.js @@ -74,7 +74,7 @@ class Server { this.podcastManager = new PodcastManager(this.watcher, this.notificationManager) this.audioMetadataManager = new AudioMetadataMangaer() this.rssFeedManager = new RssFeedManager() - this.cronManager = new CronManager(this.podcastManager) + this.cronManager = new CronManager(this.podcastManager, this.playbackSessionManager) this.apiCacheManager = new ApiCacheManager() this.binaryManager = new BinaryManager() diff --git a/server/controllers/ShareController.js b/server/controllers/ShareController.js index 0770bfa4..0dbec374 100644 --- a/server/controllers/ShareController.js +++ b/server/controllers/ShareController.js @@ -233,6 +233,7 @@ class ShareController { } playbackSession.currentTime = Math.min(currentTime, playbackSession.duration) + playbackSession.updatedAt = Date.now() Logger.debug(`[ShareController] Update share playback session ${req.cookies.share_session_id} currentTime: ${playbackSession.currentTime}`) res.sendStatus(204) } diff --git a/server/managers/CronManager.js b/server/managers/CronManager.js index 961bf2eb..b35cf804 100644 --- a/server/managers/CronManager.js +++ b/server/managers/CronManager.js @@ -4,9 +4,14 @@ const Logger = require('../Logger') const Database = require('../Database') const LibraryScanner = require('../scanner/LibraryScanner') +const ShareManager = require('./ShareManager') + class CronManager { - constructor(podcastManager) { + constructor(podcastManager, playbackSessionManager) { + /** @type {import('./PodcastManager')} */ this.podcastManager = podcastManager + /** @type {import('./PlaybackSessionManager')} */ + this.playbackSessionManager = playbackSessionManager this.libraryScanCrons = [] this.podcastCrons = [] @@ -19,10 +24,26 @@ class CronManager { * @param {import('../objects/Library')[]} libraries */ async init(libraries) { + this.initOpenSessionCleanupCron() this.initLibraryScanCrons(libraries) await this.initPodcastCrons() } + /** + * Initialize open session cleanup cron + * Runs every day at 00:30 + * Closes open share sessions that have not been updated in 24 hours + * Closes open playback sessions that have not been updated in 36 hours + * TODO: Clients should re-open the session if it is closed so that stale sessions can be closed sooner + */ + initOpenSessionCleanupCron() { + cron.schedule('30 0 * * *', async () => { + Logger.debug('[CronManager] Open session cleanup cron executing') + ShareManager.closeStaleOpenShareSessions() + await this.playbackSessionManager.closeStaleOpenSessions() + }) + } + /** * Initialize library scan crons * @param {import('../objects/Library')[]} libraries diff --git a/server/managers/PlaybackSessionManager.js b/server/managers/PlaybackSessionManager.js index 414f7f71..73a04324 100644 --- a/server/managers/PlaybackSessionManager.js +++ b/server/managers/PlaybackSessionManager.js @@ -21,6 +21,8 @@ class PlaybackSessionManager { this.StreamsPath = Path.join(global.MetadataPath, 'streams') this.oldPlaybackSessionMap = {} // TODO: Remove after updated mobile versions + + /** @type {PlaybackSession[]} */ this.sessions = [] } @@ -346,6 +348,10 @@ class PlaybackSessionManager { } } + /** + * + * @param {string} sessionId + */ async removeSession(sessionId) { const session = this.sessions.find((s) => s.id === sessionId) if (!session) return @@ -378,5 +384,18 @@ class PlaybackSessionManager { Logger.error(`[PlaybackSessionManager] cleanOrphanStreams failed`, error) } } + + /** + * Close all open sessions that have not been updated in the last 36 hours + */ + async closeStaleOpenSessions() { + const updatedAtTimeCutoff = Date.now() - 1000 * 60 * 60 * 36 + const staleSessions = this.sessions.filter((session) => session.updatedAt < updatedAtTimeCutoff) + for (const session of staleSessions) { + const sessionLastUpdate = new Date(session.updatedAt) + Logger.info(`[PlaybackSessionManager] Closing stale session "${session.displayTitle}" (${session.id}) last updated at ${sessionLastUpdate}`) + await this.removeSession(session.id) + } + } } module.exports = PlaybackSessionManager diff --git a/server/managers/ShareManager.js b/server/managers/ShareManager.js index f6827973..8e771dab 100644 --- a/server/managers/ShareManager.js +++ b/server/managers/ShareManager.js @@ -162,5 +162,18 @@ class ShareManager { destroyMediaItemShare(mediaItemShareId) { return Database.models.mediaItemShare.destroy({ where: { id: mediaItemShareId } }) } + + /** + * Close open share sessions that have not been updated in the last 24 hours + */ + closeStaleOpenShareSessions() { + const updatedAtTimeCutoff = Date.now() - 1000 * 60 * 60 * 24 + const staleSessions = this.openSharePlaybackSessions.filter((session) => session.updatedAt < updatedAtTimeCutoff) + for (const session of staleSessions) { + const sessionLastUpdate = new Date(session.updatedAt) + Logger.info(`[PlaybackSessionManager] Closing stale session "${session.displayTitle}" (${session.id}) last updated at ${sessionLastUpdate}`) + this.closeSharePlaybackSession(session) + } + } } module.exports = new ShareManager()