-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.js
42 lines (35 loc) · 1.22 KB
/
scheduler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const schedule = require('node-schedule');
const app = require('./app');
const timer = require('./utilities/timer');
const logger = require('./utilities/logger');
const config = require('./config.json');
const offsetEnabled = config.timings.scheduler.offset.enabled;
const minOffset = config.timings.scheduler.offset.min * 1000;
const maxOffset = config.timings.scheduler.offset.max * 1000;
var nextOffset = generateRandomOffset();
var job = schedule.scheduleJob(config.timings.scheduler.expression, () => {
if (offsetEnabled) {
const offset = nextOffset;
nextOffset = generateRandomOffset();
setTimeout(() => {
runScript();
}, offset);
} else {
runScript();
}
});
async function runScript() {
logger.log('Running the script...');
await app.bumpServer();
logNextRun();
}
function generateRandomOffset() {
return Math.floor(Math.random() * (maxOffset - minOffset + 1) + minOffset);
}
function logNextRun() {
var nextInvocation = new Date(job.nextInvocation());
var time = timer.format(timer.getTimeAfterMs(nextOffset, nextInvocation));
logger.log(`The next run is scheduled for "${time}".`);
}
logger.log('Started the built-in scheduler.');
logNextRun();