forked from eshengsky/node-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.js
120 lines (114 loc) · 5.09 KB
/
schedule.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* @Author: Sky.Sun
* @Date: 2018-02-07 16:09:43
* @Last Modified by: Sky.Sun
* @Last Modified time: 2019-05-14 10:34:25
*/
const schedule = require('node-schedule');
const configPath = require('./getConfigPath')();
const config = require(configPath);
const job = config.job;
const routeModel = require('./models/routes').routeModel;
const serverModel = require('./models/servers').serverModel;
const permissionModel = require('./models/permissions').permissionModel;
const cacheModel = require('./models/caches').cacheModel;
const domainModel = require('./models/domains').domainModel;
const serverlog = require('serverlog-node');
const logger = serverlog.getLogger('proxy-schedule');
const common = require('./utilities/common');
const settings = require('./settings');
/**
* 从数据库获取最新数据,若有变更则更新内存中的数组
*/
const updateSettings = () => {
Promise.all([
routeModel.find({ deleted: false }, '-createTime -createUser -modifyTime -modifyUser').sort({ sequence: 1 }).exec(),
serverModel.find({ deleted: false }, '-createTime -createUser -modifyTime -modifyUser').sort({ sequence: 1 }).exec(),
permissionModel.find({ deleted: false }, '-createTime -createUser -modifyTime -modifyUser').sort({ sequence: 1 }).exec(),
cacheModel.find({ deleted: false }, '-createTime -createUser -modifyTime -modifyUser').sort({ sequence: 1 }).exec(),
domainModel.find({ deleted: false }, '-createTime -createUser -modifyTime -modifyUser').sort({ sequence: 1 }).exec()
]).then(values => {
const routes = values[0];
const servers = values[1];
const permissions = values[2];
const caches = values[3];
const domains = values[4];
if (Array.isArray(routes)) {
if (routes.length > 0) {
if (JSON.stringify(settings.getRoutes()) === JSON.stringify(routes)) {
// logger.info('获取到的routes无变化,不做更新操作!');
} else {
settings.setRoutes(routes);
logger.info('成功获取并更新routes!');
}
}
} else {
logger.error('获取到的routes数据异常,不做更新操作!routes:', routes);
}
if (Array.isArray(servers)) {
if (JSON.stringify(settings.getServers()) === JSON.stringify(servers)) {
// logger.info('获取到的servers无变化,不做更新操作!');
} else {
settings.setServers(servers);
logger.info('成功获取并更新servers!');
}
} else {
logger.error('获取到的servers数据异常,不做更新操作!servers:', servers);
}
if (Array.isArray(permissions)) {
if (permissions.length > 0) {
// 给排除项加上 active/deleted 两项
permissions.forEach(permission => {
if (permission.excludes.length > 0) {
permission.excludes.forEach(exc => {
exc.active = true;
exc.deleted = false;
});
}
});
if (JSON.stringify(settings.getPermissions()) === JSON.stringify(permissions)) {
// logger.info('获取到的permissions无变化,不做更新操作!');
} else {
settings.setPermissions(permissions);
logger.info('成功获取并更新permissions!');
}
}
} else {
logger.error('获取到的permissions数据异常,不做更新操作!permissions:', permissions);
}
if (Array.isArray(caches)) {
if (caches.length > 0) {
if (JSON.stringify(settings.getCaches()) === JSON.stringify(caches)) {
// logger.info('获取到的caches无变化,不做更新操作!');
} else {
settings.setCaches(caches);
logger.info('成功获取并更新caches!');
}
}
} else {
logger.error('获取到的caches数据异常,不做更新操作!caches:', caches);
}
if (Array.isArray(domains)) {
if (domains.length > 0) {
if (JSON.stringify(settings.getDomains()) === JSON.stringify(domains)) {
// logger.info('获取到的domains无变化,不做更新操作!');
} else {
settings.setDomains(domains);
logger.info('成功获取并更新domains!');
}
}
} else {
logger.error('获取到的domains数据异常,不做更新操作!domains:', domains);
}
}).catch(err => {
logger.error('获取routes/servers/permissions/caches/domains出错!', err);
});
};
const startJob = () => {
updateSettings();
schedule.scheduleJob(job, () => {
updateSettings();
});
};
exports.startJob = startJob;
exports.updateSettings = updateSettings;