log cron execution time

This commit is contained in:
HF 2022-07-12 13:15:20 +02:00
parent 393369a9b8
commit fc8be3d07a

View File

@ -25,16 +25,18 @@ class Cron {
this.functions = []; this.functions = [];
const ct = new Date(); const ct = new Date();
const msToNextFullHour = 3600000 const msToNextFullHour = HOUR
- (ct.getUTCMinutes() * 60 + ct.getUTCSeconds()) * 1000; - (ct.getUTCMinutes() * 60 + ct.getUTCSeconds()) * 1000;
this.timeout = setTimeout(this.checkForExecution, msToNextFullHour); this.timeout = setTimeout(this.checkForExecution, msToNextFullHour);
} }
checkForExecution() { checkForExecution() {
const curTime = Date.now(); const curDate = new Date();
const curTime = curDate.getTime();
this.timeout = setTimeout(this.checkForExecution, HOUR); this.timeout = setTimeout(this.checkForExecution, HOUR);
if (curTime + 10000 > this.lastRun + this.interval * HOUR) { if (curTime + 120000 > this.lastRun + this.interval * HOUR) {
logger.info(`Run cron events for interval: ${this.interval}h`); // eslint-disable-next-line max-len
logger.info(`${curDate.toUTCString()}> Run cron events for interval: ${this.interval}h`);
this.lastRun = curTime; this.lastRun = curTime;
this.functions.forEach(async (item) => { this.functions.forEach(async (item) => {
try { try {
@ -57,7 +59,7 @@ function initializeDailyCron() {
// make it first run at midnight // make it first run at midnight
const lastRun = now.getTime() const lastRun = now.getTime()
- now.getUTCHours() * HOUR - now.getUTCHours() * HOUR
- (now.getUTCMinutes() * 60 - now.getUTCSeconds()) * 1000; - (now.getUTCMinutes() * 60 + now.getUTCSeconds()) * 1000;
const cron = new Cron(24, lastRun); const cron = new Cron(24, lastRun);
return cron; return cron;
} }