forked from tapd8/apig
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.js
More file actions
121 lines (102 loc) · 2.95 KB
/
monitor.js
File metadata and controls
121 lines (102 loc) · 2.95 KB
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
const log = require('./dist').log.monitor;
const request = require('request');
const appConfig = require('./config');
const hashCode = require('hashcode').hashCode;
const path = require('path');
const fs = require('fs');
const makeDir = require('make-dir');
const getToken = require('./tapdata').getToken;
/**
* 最后配置信息的 hashCode,用于比较配置文件是否更新
* @type {null}
*/
let lastHashCode = null,
intervalId;
/**
* 加载配置文件
*/
const
__listeners = {},
loadConfig = function (token) {
// log.debug('download load config from tapDataServer ' + appConfig.tapDataServer.url);
request.get(appConfig.tapDataServer.url + '?access_token=' + token, function (err, response, body) {
if (err) {
log.error('download config fail.', err);
} else {
// log.debug('download config success.');
body = body.trim();
/*if( ! (/^\{.*\}$/.test(body) || /^\[.*\]$/.test(body)) ){
log.error('the configuration file is not in the expected JSON format.', body);
return;
}*/
// 计算 hashCode 比较是否有修改
let newHashCode = hashCode().value(body);
// log.info(`old config hash code: ${lastHashCode}, new config hash code: ${newHashCode}`);
if (newHashCode !== lastHashCode) {
lastHashCode = newHashCode;
log.info('tap data config is changed, cache remote config to local.');
// 保存到本地缓存目录
fs.writeFileSync(getCacheConfig(), body + "\n");
try {
let config = JSON.parse(body);
// 通知配置文件更新了
const msg = {
type: 'changed',
data: config
};
if (__listeners['message']) {
__listeners['message'].forEach((l) => {
if (typeof l === 'function')
l(msg);
})
}
} catch (e) {
log.error('parse config error: \n', e);
}
}
}
});
},
/**
* 系统启动时,初始化一些配置信息
* @private
*/
__init = function (cb) {
const localConfigFilePath = getCacheConfig();
if (fs.existsSync(localConfigFilePath)) {
let config = fs.readFileSync(localConfigFilePath).toString();
lastHashCode = hashCode().value(config.trim());
}
cb();
},
/**
* 获取本地缓存配置文件路径
*/
getCacheConfig = function () {
const cacheDirPath = appConfig.cacheDir.startsWith('/') ? appConfig.cacheDir : path.join(__dirname, appConfig.cacheDir);
if (!fs.existsSync(cacheDirPath)) {
log.info(`create cache dir ${cacheDirPath}`);
makeDir.sync(cacheDirPath);
}
return path.resolve(`${cacheDirPath}/tap_data_server_download_config.json`);
};
exports.on = function (type, listener) {
if (!__listeners[type])
__listeners[type] = [];
__listeners[type].push(listener);
};
exports.start = function () {
__init(() => {
intervalId = setInterval(() => {
getToken(token => {
if (token) {
loadConfig(token);
}
})
}, appConfig.intervals);
});
};
exports.stop = function () {
if (intervalId)
clearInterval(intervalId);
};