core: fix scheduler (#1259)

This commit is contained in:
Brett Jia
2024-01-12 12:10:31 -05:00
committed by GitHub
parent b8d233f08d
commit ea78b7f59e

View File

@@ -47,37 +47,45 @@ export class Scheduler {
const day = future.getDay();
if (!days[day])
continue;
source.log.i(`event will fire at ${future.toLocaleString()}`);
return future;
}
source.log.w('event will never fire');
}
const when = reschedule();
if (!when) {
return {
removeListener() {
}
}
}
const delay = when.getTime() - Date.now();
source.log.i(`event will fire in ${Math.round(delay / 60 / 1000)} minutes.`);
let timeout = setTimeout(() => {
reschedule();
let timeout: NodeJS.Timeout = null;
let when: Date = null;
function timerCb() {
timeout = null;
const prevWhen = when;
setupTimer();
callback(ret, {
eventId: undefined,
eventInterface: 'Scheduler',
eventTime: Date.now(),
}, when)
}, delay);
}, prevWhen)
}
function setupTimer() {
when = reschedule();
if (when) {
const delay = when.getTime() - Date.now();
source.log.i(`event will fire in ${Math.round(delay / 60 / 1000)} minutes.`);
timeout = setTimeout(timerCb, delay);
}
}
setupTimer();
return {
removeListener() {
clearTimeout(timeout);
if (timeout) {
clearTimeout(timeout);
}
timeout = null;
when = null;
}
}
}