mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-18 17:59:37 +00:00
Update:Remove node-cron dependency
This commit is contained in:
parent
26ef275ab4
commit
b7e546f2f5
19 changed files with 686 additions and 18 deletions
49
server/libs/nodeCron/scheduler.js
Normal file
49
server/libs/nodeCron/scheduler.js
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
'use strict';
|
||||
|
||||
const EventEmitter = require('events');
|
||||
const TimeMatcher = require('./time-matcher');
|
||||
|
||||
class Scheduler extends EventEmitter{
|
||||
constructor(pattern, timezone, autorecover){
|
||||
super();
|
||||
this.timeMatcher = new TimeMatcher(pattern, timezone);
|
||||
this.autorecover = autorecover;
|
||||
}
|
||||
|
||||
start(){
|
||||
// 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);
|
||||
|
||||
for(let i = missedExecutions; i >= 0; i--){
|
||||
const date = new Date(new Date().getTime() - i * 1000);
|
||||
let date_tmp = this.timeMatcher.apply(date);
|
||||
if(lastExecution.getTime() < date_tmp.getTime() && (i === 0 || this.autorecover) && this.timeMatcher.match(date)){
|
||||
this.emit('scheduled-time-matched', date_tmp);
|
||||
date_tmp.setMilliseconds(0);
|
||||
lastExecution = date_tmp;
|
||||
}
|
||||
}
|
||||
lastCheck = process.hrtime();
|
||||
this.timeout = setTimeout(matchTime, delay);
|
||||
};
|
||||
matchTime();
|
||||
}
|
||||
|
||||
stop(){
|
||||
if(this.timeout){
|
||||
clearTimeout(this.timeout);
|
||||
}
|
||||
this.timeout = null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Scheduler;
|
||||
Loading…
Add table
Add a link
Reference in a new issue