-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
28 lines (26 loc) · 1.03 KB
/
index.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
const moment = require('moment');
const { scheduleJob } = require('node-schedule');
const { initialize } = require('./crawler');
const { tasks } = require('./config');
const logger = require('./logger');
const { killChrome, runChromeHeadless } = require('./command');
function run() {
logger.info(`Find ${tasks.length} tasks`);
const startTime = moment().add(5, 'seconds');
const startMinute = startTime.minute();
const startSecond = startTime.second();
let startHour = startTime.hour();
tasks.forEach((task, index) => {
if (index > 0) {
startHour = (startHour + tasks[index - 1].maxDurationInHour) % 24
}
logger.info(`Task ${task.startUrl} created, startTime: ${startHour}:${startMinute}:${startSecond}`);
scheduleJob(`${startSecond} ${startMinute} ${startHour} * * *`, async function () {
await killChrome();
await runChromeHeadless();
logger.info(`task ${JSON.stringify(task)} start!`);
await initialize(task)
});
})
}
run();