From 1ed056dcea5f5c5aa759b675f0ccfa1206784a86 Mon Sep 17 00:00:00 2001 From: James Ketrenos Date: Tue, 27 Apr 2021 17:17:22 -0700 Subject: [PATCH] Cache check older than one day working in whisky.js Signed-off-by: James Ketrenos --- .gitignore | 2 +- index.js | 1 - whisky.js | 36 +++++++++++++++++++++++------------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index e5ac572..dd2c37a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ -node_modules cache whiskies.db +node_modules diff --git a/index.js b/index.js index f535c85..4ae9577 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,6 @@ const cors = require("cors"); app.use(cors()); app.use("/graphql", graphqlHTTP({ schema: schema.schema, graphiql: true})); - /* mutation { createWhisky( diff --git a/whisky.js b/whisky.js index 8afa5a6..213e11c 100644 --- a/whisky.js +++ b/whisky.js @@ -22,12 +22,16 @@ const client = new GraphQLClient("http://localhost:4000/graphql", { headers: {} const getWhiskyInfo = async (whisky) => { const cacheFile = `cache/${whisky.code}`; return fs.stat(cacheFile).then(async stat => { - if (moment().subtract(1, 'day').isBefore(stat.mtime)) { - console.log(`${cacheFile} is newer than one day ${moment(stat.mtime).calendar()}`); - } else { - console.log(`${cacheFile} is older than one day ${moment(stat.mtime).calendar()}`); + const last = moment(stat.mtime), + yesterday = moment().subtract(1, 'day');; + + if (last.isBefore(yesterday)) { + console.log(`${cacheFile} ${last.calendar()} is older than yesterday at this time: ${yesterday.calendar()}`); await fs.unlink(cacheFile); + } else { + console.log(`${cacheFile} ${last.calendar()} is newer than yesterday at this time: ${yesterday.calendar()}`); } + return fs.readFile(cacheFile); }) .catch(async (error) => { @@ -290,14 +294,19 @@ fs.stat("cache") .then(async () => { /* Attempt to read the whisky cache data. * If it is older than 1 day, fetch new data */ - const stat = await fs.stat("cache/whiskies"); - if (moment().subtract(1, 'day').isBefore(stat.mtime)) { - console.log(`cache/whiskies is newer than one day ${moment(stat.mtime).calendar()}`); + const cacheFile = "cache/whiskies", + stat = await fs.stat(cacheFile), + last = moment(stat.mtime), + yesterday = moment().subtract(1, 'day'); + + if (last.isBefore(yesterday)) { + console.log(`${cacheFile} ${last.calendar()} is older than yesterday at this time: ${yesterday.calendar()}`); + await fs.unlink(cacheFile); } else { - console.log(`cache/whiskies is older than one day ${moment(stat.mtime).calendar()}`); - await fs.unlink('cache/whiskies'); + console.log(`${cacheFile} ${last.calendar()} is newer than yesterday at this time: ${yesterday.calendar()}`); } - const data = await fs.readFile("cache/whiskies"); + + const data = await fs.readFile(cacheFile); return fs.readFile("cache/cookies") .then(cookieData => { JSON.parse(cookieData).forEach(cookie => cookies.push(cookie)) @@ -307,6 +316,7 @@ fs.stat("cache") }); }).catch(async (error) => { if (criticalFailure) throw error; + console.error(error); console.log(`Cache or cookies not found--loading fresh whisky list and cookies.`); await rateLimit(); return fetch("http://oregonliquorsearch.com/", { @@ -356,10 +366,10 @@ fs.stat("cache") }) .then(response => response.text()); }) - .then(dom => { + .then(async (dom) => { try { -// console.log(`Writing to "cache"...`); - fs.writeFile("cache/whiskies", dom); + console.log(`Writing to "cache"...`); + await fs.writeFile("cache/whiskies", dom); } catch(_error) { if (criticalFailure) throw _error; console.log(`Write failed: ${_error}`);