From 142f2a82c5920ece7d3513dbf6eeba79dd4a9376 Mon Sep 17 00:00:00 2001 From: Matt Isenhower Date: Sat, 22 Apr 2023 14:27:55 -0700 Subject: [PATCH] Fix a bug with outdated festival data --- app/common/ValueCache.mjs | 9 ++++++++- app/data/updaters/FestivalUpdater.mjs | 7 ++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/common/ValueCache.mjs b/app/common/ValueCache.mjs index b57a477..fffa391 100644 --- a/app/common/ValueCache.mjs +++ b/app/common/ValueCache.mjs @@ -38,8 +38,15 @@ export default class ValueCache return item && item.data; } + async getCachedAt() { + let item = await this._getRawItem(); + + return (item && item.cachedAt) ? new Date(item.cachedAt) : null; + } + async setData(data, expires = null) { - let serialized = JSON.stringify({ expires, data }, undefined, 2); + let cachedAt = new Date; + let serialized = JSON.stringify({ expires, data, cachedAt }, undefined, 2); await mkdirp(path.dirname(this.path)); await fs.writeFile(this.path, serialized); diff --git a/app/data/updaters/FestivalUpdater.mjs b/app/data/updaters/FestivalUpdater.mjs index 6b1aba1..3da6abd 100644 --- a/app/data/updaters/FestivalUpdater.mjs +++ b/app/data/updaters/FestivalUpdater.mjs @@ -81,13 +81,14 @@ export default class FestivalUpdater extends DataUpdater // We don't need to use the locale for this data // since localization data retrieval happens elsewhere. let data = await cache.getData(); + let cachedAt = await cache.getCachedAt(); // How long until this festival ends/ended? // We want to retrieve the latest data until 4 hours after the Splatfest ends - let diff = Date.now() - new Date(node.endTime); - let forceUpdate = (diff < 4 * 60 * 60 * 1000); + let cutoff = new Date(node.endTime); + cutoff.setHours(cutoff.getHours() + 4); - if (forceUpdate || !data) { + if (!data || cachedAt < cutoff) { this.console.info(`Getting festival details for ${node.id}`); data = await this.splatnet().getFestDetailData(node.id); await cache.setData(data);