Skip to content

Commit

Permalink
Add multiple step backs
Browse files Browse the repository at this point in the history
Also clean up the dev logs
  • Loading branch information
cheeaun committed Jan 18, 2022
1 parent ebdb91e commit ba4d59b
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions api/rainarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ const fetchRadar = (dt, opts) =>
if (e.statusCode == 404) {
reject(new Error('Page not found'));
} else {
console.error(e);
console.error({
message: e.message,
code: e.code,
});
reject(e);
}
});
Expand Down Expand Up @@ -221,6 +224,7 @@ const convertImageToData = (img) => {

const cachedOutput = {};
module.exports = async (req, res) => {
console.log('❇️ START');
console.time('RESPONSE');
try {
let dt, output;
Expand Down Expand Up @@ -248,12 +252,16 @@ module.exports = async (req, res) => {
try {
img = await fetchRadar(dt);
} catch (e) {
// Retry with older radar image
dt = datetimeStr(-5);
output = cachedOutput[dt];

if (!output) {
img = await fetchRadar(dt);
for (let i = 1; i <= 5; i++) {
// Step back 5 minutes every time
dt = datetimeStr(i * -5);
console.log('Retry with older image', dt);
output = cachedOutput[dt];
if (output) break;
try {
img = await fetchRadar(dt, { retry: 0 });
break;
} catch (e) {}
}
}
if (!output) {
Expand Down

1 comment on commit ba4d59b

@vercel
Copy link

@vercel vercel bot commented on ba4d59b Jan 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.