Cache check older than one day working in whisky.js

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2021-04-27 17:17:22 -07:00
parent 33131716ce
commit 1ed056dcea
3 changed files with 24 additions and 15 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
node_modules
cache
whiskies.db
node_modules

View File

@ -7,7 +7,6 @@ const cors = require("cors");
app.use(cors());
app.use("/graphql", graphqlHTTP({ schema: schema.schema, graphiql: true}));
/*
mutation {
createWhisky(

View File

@ -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}`);