2022-06-08 03:04:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const EventEmitter = require('events');
|
|
|
|
const TimeMatcher = require('./time-matcher');
|
|
|
|
|
2022-08-18 01:44:21 +02:00
|
|
|
class Scheduler extends EventEmitter {
|
|
|
|
constructor(pattern, timezone, autorecover) {
|
2022-06-08 03:04:51 +02:00
|
|
|
super();
|
|
|
|
this.timeMatcher = new TimeMatcher(pattern, timezone);
|
|
|
|
this.autorecover = autorecover;
|
|
|
|
}
|
|
|
|
|
2022-08-18 01:44:21 +02:00
|
|
|
start() {
|
2022-06-08 03:04:51 +02:00
|
|
|
// clear timeout if exists
|
|
|
|
this.stop();
|
|
|
|
|
|
|
|
let lastCheck = process.hrtime();
|
|
|
|
let lastExecution = this.timeMatcher.apply(new Date());
|
|
|
|
|
|
|
|
const matchTime = () => {
|
|
|
|
const delay = 1000;
|
|
|
|
const elapsedTime = process.hrtime(lastCheck);
|
|
|
|
const elapsedMs = (elapsedTime[0] * 1e9 + elapsedTime[1]) / 1e6;
|
|
|
|
const missedExecutions = Math.floor(elapsedMs / 1000);
|
2022-08-18 01:44:21 +02:00
|
|
|
|
|
|
|
for (let i = missedExecutions; i >= 0; i--) {
|
2022-06-08 03:04:51 +02:00
|
|
|
const date = new Date(new Date().getTime() - i * 1000);
|
|
|
|
let date_tmp = this.timeMatcher.apply(date);
|
2022-08-18 01:44:21 +02:00
|
|
|
if (lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)) {
|
2022-06-08 03:04:51 +02:00
|
|
|
this.emit('scheduled-time-matched', date_tmp);
|
|
|
|
date_tmp.setMilliseconds(0);
|
|
|
|
lastExecution = date_tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lastCheck = process.hrtime();
|
|
|
|
this.timeout = setTimeout(matchTime, delay);
|
|
|
|
};
|
|
|
|
matchTime();
|
|
|
|
}
|
|
|
|
|
2022-08-18 01:44:21 +02:00
|
|
|
stop() {
|
|
|
|
if (this.timeout) {
|
2022-06-08 03:04:51 +02:00
|
|
|
clearTimeout(this.timeout);
|
|
|
|
}
|
|
|
|
this.timeout = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Scheduler;
|