Cache check older than one day working in whisky.js
Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
parent
33131716ce
commit
1ed056dcea
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1,3 @@
|
|||||||
node_modules
|
|
||||||
cache
|
cache
|
||||||
whiskies.db
|
whiskies.db
|
||||||
|
node_modules
|
||||||
|
1
index.js
1
index.js
@ -7,7 +7,6 @@ const cors = require("cors");
|
|||||||
app.use(cors());
|
app.use(cors());
|
||||||
app.use("/graphql", graphqlHTTP({ schema: schema.schema, graphiql: true}));
|
app.use("/graphql", graphqlHTTP({ schema: schema.schema, graphiql: true}));
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
mutation {
|
mutation {
|
||||||
createWhisky(
|
createWhisky(
|
||||||
|
36
whisky.js
36
whisky.js
@ -22,12 +22,16 @@ const client = new GraphQLClient("http://localhost:4000/graphql", { headers: {}
|
|||||||
const getWhiskyInfo = async (whisky) => {
|
const getWhiskyInfo = async (whisky) => {
|
||||||
const cacheFile = `cache/${whisky.code}`;
|
const cacheFile = `cache/${whisky.code}`;
|
||||||
return fs.stat(cacheFile).then(async stat => {
|
return fs.stat(cacheFile).then(async stat => {
|
||||||
if (moment().subtract(1, 'day').isBefore(stat.mtime)) {
|
const last = moment(stat.mtime),
|
||||||
console.log(`${cacheFile} is newer than one day ${moment(stat.mtime).calendar()}`);
|
yesterday = moment().subtract(1, 'day');;
|
||||||
} else {
|
|
||||||
console.log(`${cacheFile} is older than one day ${moment(stat.mtime).calendar()}`);
|
if (last.isBefore(yesterday)) {
|
||||||
|
console.log(`${cacheFile} ${last.calendar()} is older than yesterday at this time: ${yesterday.calendar()}`);
|
||||||
await fs.unlink(cacheFile);
|
await fs.unlink(cacheFile);
|
||||||
|
} else {
|
||||||
|
console.log(`${cacheFile} ${last.calendar()} is newer than yesterday at this time: ${yesterday.calendar()}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return fs.readFile(cacheFile);
|
return fs.readFile(cacheFile);
|
||||||
})
|
})
|
||||||
.catch(async (error) => {
|
.catch(async (error) => {
|
||||||
@ -290,14 +294,19 @@ fs.stat("cache")
|
|||||||
.then(async () => {
|
.then(async () => {
|
||||||
/* Attempt to read the whisky cache data.
|
/* Attempt to read the whisky cache data.
|
||||||
* If it is older than 1 day, fetch new data */
|
* If it is older than 1 day, fetch new data */
|
||||||
const stat = await fs.stat("cache/whiskies");
|
const cacheFile = "cache/whiskies",
|
||||||
if (moment().subtract(1, 'day').isBefore(stat.mtime)) {
|
stat = await fs.stat(cacheFile),
|
||||||
console.log(`cache/whiskies is newer than one day ${moment(stat.mtime).calendar()}`);
|
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 {
|
} else {
|
||||||
console.log(`cache/whiskies is older than one day ${moment(stat.mtime).calendar()}`);
|
console.log(`${cacheFile} ${last.calendar()} is newer than yesterday at this time: ${yesterday.calendar()}`);
|
||||||
await fs.unlink('cache/whiskies');
|
|
||||||
}
|
}
|
||||||
const data = await fs.readFile("cache/whiskies");
|
|
||||||
|
const data = await fs.readFile(cacheFile);
|
||||||
return fs.readFile("cache/cookies")
|
return fs.readFile("cache/cookies")
|
||||||
.then(cookieData => {
|
.then(cookieData => {
|
||||||
JSON.parse(cookieData).forEach(cookie => cookies.push(cookie))
|
JSON.parse(cookieData).forEach(cookie => cookies.push(cookie))
|
||||||
@ -307,6 +316,7 @@ fs.stat("cache")
|
|||||||
});
|
});
|
||||||
}).catch(async (error) => {
|
}).catch(async (error) => {
|
||||||
if (criticalFailure) throw error;
|
if (criticalFailure) throw error;
|
||||||
|
console.error(error);
|
||||||
console.log(`Cache or cookies not found--loading fresh whisky list and cookies.`);
|
console.log(`Cache or cookies not found--loading fresh whisky list and cookies.`);
|
||||||
await rateLimit();
|
await rateLimit();
|
||||||
return fetch("http://oregonliquorsearch.com/", {
|
return fetch("http://oregonliquorsearch.com/", {
|
||||||
@ -356,10 +366,10 @@ fs.stat("cache")
|
|||||||
})
|
})
|
||||||
.then(response => response.text());
|
.then(response => response.text());
|
||||||
})
|
})
|
||||||
.then(dom => {
|
.then(async (dom) => {
|
||||||
try {
|
try {
|
||||||
// console.log(`Writing to "cache"...`);
|
console.log(`Writing to "cache"...`);
|
||||||
fs.writeFile("cache/whiskies", dom);
|
await fs.writeFile("cache/whiskies", dom);
|
||||||
} catch(_error) {
|
} catch(_error) {
|
||||||
if (criticalFailure) throw _error;
|
if (criticalFailure) throw _error;
|
||||||
console.log(`Write failed: ${_error}`);
|
console.log(`Write failed: ${_error}`);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user