From e506ac15b0dfbf8a49e7f999f017295b069e1e53 Mon Sep 17 00:00:00 2001 From: vexorian Date: Wed, 9 Sep 2020 15:32:16 -0400 Subject: [PATCH] Fix Memory usage peaking during TV Guide Generation --- index.js | 20 +++++++++++++++----- src/tv-guide-service.js | 9 ++++++++- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 241f5e3..1fe19e6 100644 --- a/index.js +++ b/index.js @@ -67,19 +67,29 @@ let xmltvInterval = { interval: null, lastRefresh: null, updateXML: async () => { - let channels = []; - try { + let getChannelsCached = async() => { let channelNumbers = await channelDB.getAllChannelNumbers(); - channels = await Promise.all( channelNumbers.map( async (x) => { - return await channelCache.getChannelConfig(channelDB, x); + return await Promise.all( channelNumbers.map( async (x) => { + return (await channelCache.getChannelConfig(channelDB, x))[0]; }) ); + } + + let channels = []; + + try { + channels = await getChannelsCached(); let xmltvSettings = db['xmltv-settings'].find()[0]; - await guideService.refresh( await channelDB.getAllChannels(), xmltvSettings.cache*60*60*1000 ); + let t = guideService.prepareRefresh(channels, xmltvSettings.cache*60*60*1000); + channels = null; + + await guideService.refresh(t); xmltvInterval.lastRefresh = new Date() console.log('XMLTV Updated at ', xmltvInterval.lastRefresh.toLocaleString()); } catch (err) { console.error("Unable to update TV guide?", err); + return; } + channels = await getChannelsCached(); let plexServers = db['plex-servers'].find() for (let i = 0, l = plexServers.length; i < l; i++) { // Foreach plex server diff --git a/src/tv-guide-service.js b/src/tv-guide-service.js index 515f9e9..841f6b6 100644 --- a/src/tv-guide-service.js +++ b/src/tv-guide-service.js @@ -28,12 +28,16 @@ class TVGuideService return this.cached; } - async refresh(inputChannels, limit) { + prepareRefresh(inputChannels, limit) { let t = (new Date()).getTime(); this.updateTime = t; this.updateLimit = t + limit; let channels = inputChannels; this.updateChannels = channels; + return t; + } + + async refresh(t) { while( this.lastUpdate < t) { if (this.currentUpdate == -1) { this.currentUpdate = this.updateTime; @@ -47,6 +51,9 @@ class TVGuideService } async makeAccumulated(channel) { + if (typeof(channel.programs) === 'undefined') { + throw Error( JSON.stringify(channel).slice(0,200) ); + } let n = channel.programs.length; let arr = new Array( channel.programs.length + 1); arr[0] = 0;