-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
55 lines (49 loc) · 1.25 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Created by yuancheng.yuan on 2017/8/8.
*/
const schedule = require("node-schedule");
const getCityList = require("./citys/getCityList.js");
const getData = require("./getData/getData.js");
const databaseUtil = require("./dbHandler/databaseUtil.js");
let $CONFIG = {
count: 0,
rule: ['minute', ['6']],//规定每小时的第几分钟开始运行
period: 2000
};
/**
* 每小时进行一次
*/
function startSchedule() {
let rule = new schedule.RecurrenceRule();
rule[$CONFIG.rule[0]] = $CONFIG.rule[1];
console.log('start');
let j = schedule.scheduleJob(rule, function () {
startCollectAll();
});
}
/**
* 每小时启动一次,开始收集所有城市数据
*/
function startCollectAll() {
console.log("第" + ++$CONFIG.count + "次拉取");
let temp = $CONFIG.CITYS;
temp.forEach((city, index) => {
setTimeout(() => {
startCollectOne(city);
}, index * $CONFIG.period);
})
}
function startCollectOne(city) {
getData(city).then((data) => {
databaseUtil.saveData(data);
});
}
function init() {
getCityList().then((city) => {
$CONFIG.CITYS = city;
startSchedule();
}).catch((res) => {
console.log(JSON.stringify(res));
})
}
init();