Skip to content

Commit

Permalink
try to request 5 times before exit
Browse files Browse the repository at this point in the history
  • Loading branch information
DerArkeN committed Oct 20, 2022
1 parent 1156b94 commit 9cb589c
Show file tree
Hide file tree
Showing 2 changed files with 1,123 additions and 564 deletions.
61 changes: 39 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const logger = require('./logger.js');
const google = require('./google.js');
const { parse, startOfDay } = require('date-fns');

(async () => {
let run = async() => {
console.log('Started untis-google.');

let args = process.argv.slice(2);
Expand All @@ -17,26 +17,43 @@ const { parse, startOfDay } = require('date-fns');
if(running) return;
if(untis == null) return;
running = true;
try {
var curT = await untis.getTimetable();
}catch(err) {
logger.error(err, {time: `${new Date()}`});
running = false;
return;
}

//Check if update occured
if(JSON.stringify(oldT) !== JSON.stringify(curT)) {
logger.info('Update received', {time: `${new Date()}`});
console.log('Update received');
//Add new events
await untis.addNew(oldT, curT);
//Update events
await untis.update(new Date());
//Update oldT to curT
oldT = curT;

// Request 5 times max
let success = false;
let retry = 0;
while(!success) {
try {
var curT = await untis.getTimetable();
success = true;

// Check if update occured
if(JSON.stringify(oldT) !== JSON.stringify(curT)) {
logger.info('Update received', {time: `${new Date()}`});
console.log('Update received');
// Add new events
await untis.addNew(oldT, curT);
// Update events
await untis.update(new Date());
// Update oldT to curT
oldT = curT;
}

running = false;
}catch(err) {
if(retry > 5) {
console.log(err);
logger.error(err, {time: `${new Date()}`});
running = false;
clearInterval(intervalID);
break;
}
retry++;
}
}

running = false;

}, 30 * 60 * 1000);
})();
}

(async() => {
run();
})();
Loading

0 comments on commit 9cb589c

Please sign in to comment.