-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathindex.js
192 lines (177 loc) · 6.16 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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* @Author: horan
* @Date: 2017-07-09 10:24:53
* @Last Modified by: horan
* @Last Modified time: 2019-10-22 15:41:23
* @Api
*/
var _ = require('underscore');
var fs = require('fs');
var path = require("path");
var moment = require('moment');
var appRoot = require('app-root-path');
var logger = require('./lib/util/logger.js');
var main = require('./lib/main');
var ase = require("./lib/util/ase");
var util = require('./lib/util/util');
var filePath = path.join(appRoot.path, "crawlerData");
var start = function () {
var getFileList = util.readFileList(filePath, [], ".json");
main.init(getFileList, filePath);
};
var addWatcher = function (fileName, obj, isAse) {
var flag = false;
try {
var objStr = "";
if (isAse) {
objStr = ase.aesEncrypt(JSON.stringify(obj), "pwd");
} else {
objStr = JSON.stringify(obj);
}
fs.writeFileSync(path.join(filePath, fileName + '.json'), objStr);
if (fs.existsSync(path.join(filePath, fileName + '.timestamp'))) {
fs.unlinkSync(path.join(filePath, fileName + '.timestamp'));
}
logger.logMethod('info',
'',
'',
'******************************Add config file******************************');
logger.logMethod('info',
'',
'',
'File added: ' + fileName + '.json');
var currentFile = {};
currentFile.Filename = fileName + '.json';
currentFile.Content = JSON.parse(fs.readFileSync(path.join(filePath, fileName + '.json')));
util.updateInfoArray(obj.elasticsearch.e_connection.e_server, obj.elasticsearch.e_index, '', 'I');
main.singlePipe(currentFile, filePath);
flag = true;
return flag;
} catch (error) {
logger.logMethod('error',
obj.elasticsearch.e_connection.e_server,
obj.elasticsearch.e_index,
'AddWatcher error: ' + JSON.stringify(error).substring(0, 200));
return false;
}
};
var updateWatcher = function (fileName, obj, isAse) {
var flag = false;
try {
if (isAse) {
objStr = ase.aesEncrypt(JSON.stringify(obj), "pwd");
} else {
objStr = JSON.stringify(obj);
}
fs.writeFileSync(path.join(filePath, fileName + '.json'), objStr);
if (fs.existsSync(path.join(filePath, fileName + '.timestamp'))) {
fs.unlinkSync(path.join(filePath, fileName + '.timestamp'));
}
logger.logMethod('info',
'',
'',
'******************************update config file******************************');
logger.logMethod('info',
'',
'',
'File changed: ' + fileName + '.json');
var currentFile = {};
currentFile.Filename = fileName + '.json';
currentFile.Content = JSON.parse(fs.readFileSync(path.join(filePath, fileName + '.json')));
util.updateInfoArray(obj.elasticsearch.e_connection.e_server, obj.elasticsearch.e_index, '', 'I');
main.singlePipe(currentFile, filePath);
flag = true;
return flag;
} catch (error) {
logger.logMethod('error',
obj.elasticsearch.e_connection.e_server,
obj.elasticsearch.e_index,
'UpdateWatcher error: ' + JSON.stringify(error).substring(0, 200));
return false;
}
};
var deleteWatcher = function (fileName) {
var flag = false;
var newArray = [];
try {
if (isExistWatcher(fileName)) {
var currentFileContent = require(path.join(filePath, fileName + '.json'));
if (global.infoArray && global.infoArray.length > 0) {
_.find(global.infoArray, function (file) {
if (file.cluster === currentFileContent.elasticsearch.e_connection.e_server &&
file.index === currentFileContent.elasticsearch.e_index) {
return;
} else {
newArray.push(file);
}
});
}
global.infoArray = newArray;
fs.unlinkSync(path.join(filePath, fileName + '.json'));
if (fs.existsSync(path.join(filePath, fileName + '.timestamp'))) {
fs.unlinkSync(path.join(filePath, fileName + '.timestamp'));
}
logger.logMethod('info',
'',
'',
'******************************Delete config file******************************');
logger.logMethod('info',
'',
'',
'File deleted: ' + fileName + '.json');
flag = true;
return flag;
}
return flag;
} catch (error) {
logger.logMethod('error',
'',
'',
'DeleteWatcher error: ' + JSON.stringify(error).substring(0, 200));
}
};
var isExistWatcher = function (fileName) {
var flag = false;
try {
if (fs.existsSync(path.join(filePath, fileName + '.json'))) {
flag = true;
}
return flag;
} catch (error) {
logger.logMethod('error',
'',
'',
'IsExistWatcher error: ' + JSON.stringify(error).substring(0, 200));
}
};
var getInfoArray = function () {
if (global.infoArray) {
var newDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss');
var filePath = path.join(appRoot.path, "logs/status.log");
var jsonData = newDate + " " + JSON.stringify(global.infoArray);
fs.writeFileSync(filePath, jsonData);
return global.infoArray;
} else {
return null;
}
};
var startTrace = function () {
return global.isTrace = true;
}
var stropTrace = function () {
return global.isTrace = false;
}
var readLog = function (type, date, e_server, e_index) {
return require('./lib/util/util').readLog(type, date, e_server, e_index);
}
module.exports = {
start: start,
addWatcher: addWatcher,
updateWatcher: updateWatcher,
deleteWatcher: deleteWatcher,
isExistWatcher: isExistWatcher,
getInfoArray: getInfoArray,
startTrace: startTrace,
stropTrace: stropTrace,
readLog: readLog
};