Skip to content

Commit

Permalink
lib/api: Revert API stats cleanup
Browse files Browse the repository at this point in the history
NeDB is not cleaning correctly on expiration date, so we need
to do it manually
  • Loading branch information
sogehige committed Apr 12, 2018
1 parent 75bf88f commit 8bbbe6d
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions libs/api.js
Expand Up @@ -34,6 +34,7 @@ class API {

this.getChannelSubscribersOldAPI() // remove this after twitch add total subscribers
this.getChannelDataOldAPI() // remove this after twitch game and status for new API
this.cleanupApiStats()

setInterval(async () => {
// we are in bounds of safe rate limit, wait until limit is refreshed
Expand All @@ -48,6 +49,22 @@ class API {
global.db.engine.update('api.current', { key: 'game' }, { value: await global.cache.gameCache() })
}

async cleanupApiStats () {
// nedb's expire doesn't work properly, need to expire it manually
let stats = _.filter(await global.db.engine.find('api.stats'), (o) => _.now() > parseInt(o.timestamp, 10) + (1000 * 60 * 60 * 2))
this.apiStatsCleanupProcess(stats)
}

async apiStatsCleanupProcess (stats) {
if (stats.length > 0) {
let stat = stats.shift()
await global.db.engine.remove('api.stats', { _id: stat._id.toString() })
setTimeout(() => this.apiStatsCleanupProcess(stats), 100)
} else {
setTimeout(() => this.cleanupApiStats(), 60000)
}
}

async getChannelID () {
var request
const url = `https://api.twitch.tv/kraken/users?login=${config.settings.broadcaster_username}`
Expand Down

0 comments on commit 8bbbe6d

Please sign in to comment.