flexbiz-server 12.3.44 → 12.3.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server/app.js +22 -316
- package/server/cluster.js +26 -365
- package/server/defaultConfigs.js +1 -23
- package/server/libs/mongoosePatch.js +1 -1
- package/server/libs/sessionContext.js +1 -1
- package/server/models/hd1.js +2 -2
- package/server/models/hd2.js +5 -5
- package/server/modules/vouchers/vo-hd1.js +15 -17
- package/server/modules/vouchers/vo-hd2.js +57 -59
- package/server/workers/inputWorker.js +6 -6
- package/server/workers/reportWorker.js +2 -2
package/package.json
CHANGED
package/server/app.js
CHANGED
|
@@ -1,316 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
require(
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
configs.database.url=process.env.MONGO_URI || configs.database.url;
|
|
24
|
-
|
|
25
|
-
const User = global.getModel('user');
|
|
26
|
-
//console.log("request size",configs.limitRequestSize,configs.limitFileSize)
|
|
27
|
-
|
|
28
|
-
if(!configs.admins) configs.admins = configs.adminUsers.map(u=>u.email);
|
|
29
|
-
if(!configs.supportUsers) configs.supportUsers =[...configs.admins];
|
|
30
|
-
|
|
31
|
-
if(!configs.public_token) configs.public_token = "flex.public.token";
|
|
32
|
-
const _port = options.port || configs.port || configs.PORT || 443;
|
|
33
|
-
configs.port = _port;
|
|
34
|
-
global.port = _port;
|
|
35
|
-
|
|
36
|
-
//set timezone
|
|
37
|
-
const moment = require('moment-timezone');
|
|
38
|
-
moment.tz.setDefault(configs.timezone || 'Asia/Ho_Chi_Minh');
|
|
39
|
-
//app
|
|
40
|
-
if(!app) app = express();
|
|
41
|
-
app.set('trust proxy', 1);
|
|
42
|
-
//check too busy
|
|
43
|
-
const toobusy = require('toobusy-js');
|
|
44
|
-
if(configs.maxLag) toobusy.maxLag(configs.maxLag);
|
|
45
|
-
|
|
46
|
-
/*toobusy.onLag(function(currentLag) {
|
|
47
|
-
console.log("Event loop lag detected! Latency: " + currentLag + "ms. maxLag",configs.maxLag);
|
|
48
|
-
});*/
|
|
49
|
-
app.use(function(req, res, next) {
|
|
50
|
-
if (toobusy()) {
|
|
51
|
-
console.error("Server is busy right now. This request has been cancel:",req.originalUrl);
|
|
52
|
-
res.status(503).send({error:"Server is busy right now, sorry."});
|
|
53
|
-
} else {
|
|
54
|
-
next();
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
//
|
|
58
|
-
const session = require('express-session');
|
|
59
|
-
app.use(session({secret: 'QV098PVT123456HLBN', resave: false, saveUninitialized: true,cookie: { secure: false }}));
|
|
60
|
-
|
|
61
|
-
//cookie uid
|
|
62
|
-
const cookieParser = require('cookie-parser')
|
|
63
|
-
app.use(cookieParser())
|
|
64
|
-
app.use(function (req, res, next) {
|
|
65
|
-
let uid = req.cookies.uid;
|
|
66
|
-
if(!uid){
|
|
67
|
-
uid = req.headers["uid"];
|
|
68
|
-
}
|
|
69
|
-
if(!uid){
|
|
70
|
-
uid = "uid:" + crypto.randomBytes(20).toString('hex')
|
|
71
|
-
res.cookie("uid",uid,{
|
|
72
|
-
expires: new Date(Date.now() + 365 * 24 * 3600000), // cookie will be removed after 24 hours,
|
|
73
|
-
sameSite: 'none',
|
|
74
|
-
secure:true
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
res.set('uid', uid);
|
|
78
|
-
//console.log("uid",uid,req.originalUrl,req.method);
|
|
79
|
-
req.cookies.uid = uid;
|
|
80
|
-
next();
|
|
81
|
-
})
|
|
82
|
-
//sử dụng mongoose session để rollback data nếu có lỗi
|
|
83
|
-
app.use(transactionMiddleware());
|
|
84
|
-
//compression
|
|
85
|
-
const compress = require('compression');
|
|
86
|
-
app.use(compress());
|
|
87
|
-
//allow upload file to server
|
|
88
|
-
const root_dir_uploads = configs.paths.uploads || (__dirname + '/uploads');
|
|
89
|
-
const limitFileSize = configs.limitFileSize || 1024 * 1024 * 1;
|
|
90
|
-
app.use(function (req, res, next) {
|
|
91
|
-
const size = req.headers['content-lenght'];
|
|
92
|
-
if(size && size> limitFileSize){
|
|
93
|
-
return res.status(400).send({error:"File too large"});
|
|
94
|
-
}
|
|
95
|
-
next()
|
|
96
|
-
})
|
|
97
|
-
const multer = require('multer')({
|
|
98
|
-
dest: root_dir_uploads,
|
|
99
|
-
limits: {files:1,fileSize: limitFileSize }
|
|
100
|
-
});
|
|
101
|
-
app.use(multer);
|
|
102
|
-
//allow cross domain
|
|
103
|
-
const corsOptions = {
|
|
104
|
-
credentials: true,
|
|
105
|
-
exposedHeaders: ["set-cookie","uid"],
|
|
106
|
-
origin: (origin, callback) => {
|
|
107
|
-
callback(null, true);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
app.use(cors(corsOptions));
|
|
111
|
-
if(!configs.paths) configs.paths={}
|
|
112
|
-
//static
|
|
113
|
-
if(options.lite!==true){
|
|
114
|
-
app.use('/', express.static(configs.paths.public || __dirname + '/public'));
|
|
115
|
-
app.use('/admin', express.static(configs.paths.admin || (__dirname + '/admin')));
|
|
116
|
-
app.use('/templates', express.static(configs.paths.templates|| (__dirname + '/templates')));
|
|
117
|
-
app.use('/images', express.static(configs.paths.images || (__dirname + '/images')));
|
|
118
|
-
}
|
|
119
|
-
//
|
|
120
|
-
app.use(bodyParser.json({limit: (configs.limitRequestSize||'1mb')}));
|
|
121
|
-
app.use(bodyParser.urlencoded({limit: (configs.limitRequestSize||'1mb'),extended: true}));
|
|
122
|
-
|
|
123
|
-
app.use(passport.initialize());
|
|
124
|
-
//log access
|
|
125
|
-
const morgan = require('morgan')
|
|
126
|
-
const rfs = require('rotating-file-stream')
|
|
127
|
-
const logDirectory = configs.paths.log || (__dirname + '/log');
|
|
128
|
-
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
|
|
129
|
-
const accessLogStream = rfs('access.log', {
|
|
130
|
-
interval: '1d', // rotate daily
|
|
131
|
-
path: logDirectory
|
|
132
|
-
})
|
|
133
|
-
app.use(morgan('combined', {
|
|
134
|
-
stream: accessLogStream,
|
|
135
|
-
skip: function(req, res) {
|
|
136
|
-
return res.statusCode < 400
|
|
137
|
-
}
|
|
138
|
-
}))
|
|
139
|
-
//handle error default
|
|
140
|
-
// eslint-disable-next-line no-unused-vars
|
|
141
|
-
app.use(function (err, req, res, next) {
|
|
142
|
-
console.error(err.stack)
|
|
143
|
-
res.status(500).send('Server Error!')
|
|
144
|
-
})
|
|
145
|
-
//connect to mongodb
|
|
146
|
-
console.log("[app] connect to mongodb",configs.database.url);
|
|
147
|
-
if(!configs.database.url){
|
|
148
|
-
console.error("[app] thiếu đường dẫn kết nối với database trong file config");
|
|
149
|
-
process.exit(1)
|
|
150
|
-
}
|
|
151
|
-
global.mongoose.connect(configs.database.url,{ useNewUrlParser: true }).then(async () => {
|
|
152
|
-
console.log("[app] Connected to Database");
|
|
153
|
-
const redis = require('redis');
|
|
154
|
-
const {retryStrategyRedis} = require("./libs/utils");
|
|
155
|
-
global.clientRedis = redis.createClient({
|
|
156
|
-
host: '127.0.0.1',
|
|
157
|
-
port: 6379,
|
|
158
|
-
retry_strategy: retryStrategyRedis
|
|
159
|
-
});
|
|
160
|
-
global.clientRedis.on("error", (err) => console.error("Redis Client Error", err));
|
|
161
|
-
global.clientRedis.on("end", () => {
|
|
162
|
-
console.error("[app] Redis connection closed.");
|
|
163
|
-
});
|
|
164
|
-
global.clientRedis.on("connect", function() {
|
|
165
|
-
console.log("[app] redis connected");
|
|
166
|
-
//Đăng ký nhận thông báo từ các process khác
|
|
167
|
-
User.initClientSockets();
|
|
168
|
-
//
|
|
169
|
-
if(options.createRedisCache!=false){
|
|
170
|
-
const redisCache = require('./libs/redis-cache');
|
|
171
|
-
redisCache.set();
|
|
172
|
-
}
|
|
173
|
-
//start abci. xu ly cac su kien lien quan den dau tu
|
|
174
|
-
if(options.start_abci_handler){
|
|
175
|
-
const assabcihandler = global.getModel("assabcihandler");
|
|
176
|
-
assabcihandler.start();
|
|
177
|
-
}
|
|
178
|
-
//
|
|
179
|
-
if(options.lite!==true){
|
|
180
|
-
//routes
|
|
181
|
-
const route = require('./route');
|
|
182
|
-
route(app,()=>{
|
|
183
|
-
//create server
|
|
184
|
-
if(options.createServer!==false){
|
|
185
|
-
const sticky = require('sticky-session');
|
|
186
|
-
let server;
|
|
187
|
-
if (configs.use_ssl) {
|
|
188
|
-
const sslConfig = require("./sslConfig");
|
|
189
|
-
server = https.createServer(sslConfig(options.sslDir), app);
|
|
190
|
-
} else {
|
|
191
|
-
server = http.createServer(app);
|
|
192
|
-
}
|
|
193
|
-
server.timeout = 10*60*1000;
|
|
194
|
-
//init socket
|
|
195
|
-
if(options.useSocket!==false){
|
|
196
|
-
User.initSocket(server);
|
|
197
|
-
}
|
|
198
|
-
//
|
|
199
|
-
if(options.cluster!==false){
|
|
200
|
-
//start server with cluster
|
|
201
|
-
if (!sticky.listen(server, global.port)) {
|
|
202
|
-
// Master code
|
|
203
|
-
server.once('listening', function() {
|
|
204
|
-
console.log('[app] server started on',global.port);
|
|
205
|
-
});
|
|
206
|
-
} else {
|
|
207
|
-
// Worker code
|
|
208
|
-
}
|
|
209
|
-
const cluster = require('cluster');
|
|
210
|
-
cluster.on('exit', function(worker, code, signal) {
|
|
211
|
-
console.error('[app] worker ' + worker.process.pid + ' died', code, signal);
|
|
212
|
-
});
|
|
213
|
-
cluster.on('online', function(worker) {
|
|
214
|
-
console.info('[app] worker ' + worker.process.pid + ' is online');
|
|
215
|
-
});
|
|
216
|
-
}else{
|
|
217
|
-
server.listen(global.port, () => {
|
|
218
|
-
console.log('[app] server start at ' + global.port + ' port');
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
//Tính toán số lượng worker cần thiết cho mỗi pool
|
|
223
|
-
const cpus = os.cpus().length;
|
|
224
|
-
const import_cpus = options.import_cpus || (options.start_import_data_pool?(!options.start_report_pool && !options.start_input_data_pool? Math.round(cpus/2):1):0)
|
|
225
|
-
const input_cpus = options.input_cpus || (options.start_input_data_pool?Math.max(Math.round(cpus/2),2):0);
|
|
226
|
-
const report_cpus = options.report_cpus || (options.start_import_data_pool?Math.max(Math.min(cpus -input_cpus -import_cpus,Math.round(cpus/2))-1,2):0);
|
|
227
|
-
//khởi tạo các pool
|
|
228
|
-
if(input_cpus){
|
|
229
|
-
const StaticPool = require("./libs/WorkerStaticPool");
|
|
230
|
-
global.inputMainPool = new StaticPool(
|
|
231
|
-
__dirname + '/workers/inputWorker.js',//worker file
|
|
232
|
-
options.max_queue_imports||0,//limit queue
|
|
233
|
-
input_cpus,//concurrency
|
|
234
|
-
5*60*1000,//timeout 5 minutes
|
|
235
|
-
input_cpus,//auto start workers
|
|
236
|
-
"input pool"//name of pool
|
|
237
|
-
);
|
|
238
|
-
global.inputMainPool.exec({
|
|
239
|
-
load:true,
|
|
240
|
-
configs: JSON.stringify(configs),
|
|
241
|
-
},()=>{
|
|
242
|
-
console.log("[app] Crud pool ready");
|
|
243
|
-
})
|
|
244
|
-
}
|
|
245
|
-
if(import_cpus){
|
|
246
|
-
const StaticPool = require("./libs/WorkerStaticPool");
|
|
247
|
-
global.importDataMainPool = new StaticPool(
|
|
248
|
-
__dirname + '/workers/inputWorker.js',//worker file
|
|
249
|
-
options.max_queue_imports||0,//limit queue
|
|
250
|
-
import_cpus,//concurrency
|
|
251
|
-
1*60*60*1000,//timeout 1 hour
|
|
252
|
-
1,
|
|
253
|
-
"import pool"//name of pool
|
|
254
|
-
);
|
|
255
|
-
global.importDataMainPool.exec({
|
|
256
|
-
load:true,
|
|
257
|
-
configs: JSON.stringify(configs),
|
|
258
|
-
},()=>{
|
|
259
|
-
console.log("[app] Import pool ready");
|
|
260
|
-
})
|
|
261
|
-
}
|
|
262
|
-
if(report_cpus){
|
|
263
|
-
const StaticPool = require("./libs/WorkerStaticPool");
|
|
264
|
-
global.reportMainPool = new StaticPool(
|
|
265
|
-
__dirname + '/workers/reportWorker.js',
|
|
266
|
-
options.max_queue_reports||0,
|
|
267
|
-
report_cpus,//concurrency
|
|
268
|
-
5*60*1000,//timeout 5 minutes
|
|
269
|
-
2,
|
|
270
|
-
"report pool"//name of pool
|
|
271
|
-
);
|
|
272
|
-
global.reportMainPool.exec({
|
|
273
|
-
load:true,
|
|
274
|
-
configs: JSON.stringify(configs),
|
|
275
|
-
},()=>{
|
|
276
|
-
console.log("[app] Report pool ready");
|
|
277
|
-
})
|
|
278
|
-
}
|
|
279
|
-
if(callbackServer) callbackServer();
|
|
280
|
-
});
|
|
281
|
-
}else{
|
|
282
|
-
//routes
|
|
283
|
-
const route = require('./route');
|
|
284
|
-
route(app,()=>{
|
|
285
|
-
if(callbackServer) callbackServer();
|
|
286
|
-
},true);
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
}).catch((err) => {
|
|
290
|
-
console.log("[app] Not Connected to Database ERROR! ", err);
|
|
291
|
-
});
|
|
292
|
-
return app;
|
|
293
|
-
}
|
|
294
|
-
process.on("SIGINT", async () => {
|
|
295
|
-
if(global.sharedRedisConnection) await global.sharedRedisConnection.quit(); // Đóng kết nối Redis
|
|
296
|
-
process.exit(0);
|
|
297
|
-
});
|
|
298
|
-
process.on('uncaughtException', function (err) {
|
|
299
|
-
console.error((new Date).toUTCString() + ' uncaughtException:', err.message);
|
|
300
|
-
console.error(err.stack);
|
|
301
|
-
let error = `
|
|
302
|
-
Error: ${err.message}
|
|
303
|
-
Stack: ${err.stack}
|
|
304
|
-
`
|
|
305
|
-
try{
|
|
306
|
-
const logDirectory = (__dirname + '/log');
|
|
307
|
-
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
|
|
308
|
-
fs.writeFile(`${logDirectory}/error-${new Date().getTime()}.txt`,error,()=>{
|
|
309
|
-
console.log("[app] wrote log error");
|
|
310
|
-
});
|
|
311
|
-
}catch(e){
|
|
312
|
-
console.log(e);
|
|
313
|
-
}
|
|
314
|
-
process.exit(1)
|
|
315
|
-
})
|
|
316
|
-
module.exports = mainServer
|
|
1
|
+
'use strict';require("events").EventEmitter.defaultMaxListeners=1E7;
|
|
2
|
+
const os=require("os"),express=require("express"),bodyParser=require("body-parser"),crypto=require("crypto"),passport=require("passport"),https=require("https"),http=require("http"),fs=require("fs"),cors=require("cors"),_global=require("./global"),{transactionMiddleware}=require("./transactionMiddleware.js"),defaultConfigs=require("./defaultConfigs"),mainServer=function($app$$,$options$$={cluster:!0,port:443,useSocket:!0},$callbackServer$$=null){const $configs$$=global.configs={...defaultConfigs,
|
|
3
|
+
...$options$$.configs,lite:$options$$.lite};$configs$$.database.url=process.env.MONGO_URI||$configs$$.database.url;const $User$$=global.getModel("user");$configs$$.admins||($configs$$.admins=$configs$$.adminUsers.map($u$$=>$u$$.email));$configs$$.supportUsers||($configs$$.supportUsers=[...$configs$$.admins]);$configs$$.public_token||($configs$$.public_token="flex.public.token");var $_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=$options$$.port||$configs$$.port||$configs$$.PORT||
|
|
4
|
+
443;$configs$$.port=$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$;global.port=$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$;require("moment-timezone").tz.setDefault($configs$$.timezone||"Asia/Ho_Chi_Minh");$app$$||($app$$=express());$app$$.set("trust proxy",1);const $toobusy$$=require("toobusy-js");$configs$$.maxLag&&$toobusy$$.maxLag($configs$$.maxLag);$app$$.use(function($req$$,$res$$,$next$$){$toobusy$$()?(console.error("Server is busy right now. This request has been cancel:",
|
|
5
|
+
$req$$.originalUrl),$res$$.status(503).send({error:"Server is busy right now, sorry."})):$next$$()});$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=require("express-session");$app$$.use($_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$({secret:"QV098PVT123456HLBN",resave:!1,saveUninitialized:!0,cookie:{secure:!1}}));$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=require("cookie-parser");$app$$.use($_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$());
|
|
6
|
+
$app$$.use(function($req$$,$res$$,$next$$){let $uid$$=$req$$.cookies.uid;$uid$$||($uid$$=$req$$.headers.uid);$uid$$||($uid$$="uid:"+crypto.randomBytes(20).toString("hex"),$res$$.cookie("uid",$uid$$,{expires:new Date(Date.now()+31536E6),sameSite:"none",secure:!0}));$res$$.set("uid",$uid$$);$req$$.cookies.uid=$uid$$;$next$$()});$app$$.use(transactionMiddleware());$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=require("compression");$app$$.use($_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$());
|
|
7
|
+
$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=$configs$$.paths.uploads||__dirname+"/uploads";const $limitFileSize$$=$configs$$.limitFileSize||1048576;$app$$.use(function($req$jscomp$2_size$$,$res$$,$next$$){if(($req$jscomp$2_size$$=$req$jscomp$2_size$$.headers["content-lenght"])&&$req$jscomp$2_size$$>$limitFileSize$$)return $res$$.status(400).send({error:"File too large"});$next$$()});$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=require("multer")({dest:$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$,
|
|
8
|
+
limits:{files:1,fileSize:$limitFileSize$$}});$app$$.use($_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$);$app$$.use(cors({credentials:!0,exposedHeaders:["set-cookie","uid"],origin:($origin$$,$callback$$)=>{$callback$$(null,!0)}}));$configs$$.paths||($configs$$.paths={});!0!==$options$$.lite&&($app$$.use("/",express.static($configs$$.paths.public||__dirname+"/public")),$app$$.use("/admin",express.static($configs$$.paths.admin||__dirname+"/admin")),$app$$.use("/templates",express.static($configs$$.paths.templates||
|
|
9
|
+
__dirname+"/templates")),$app$$.use("/images",express.static($configs$$.paths.images||__dirname+"/images")));$app$$.use(bodyParser.json({limit:$configs$$.limitRequestSize||"1mb"}));$app$$.use(bodyParser.urlencoded({limit:$configs$$.limitRequestSize||"1mb",extended:!0}));$app$$.use(passport.initialize());$_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$=require("morgan");var $accessLogStream_rfs$$=require("rotating-file-stream");const $logDirectory$$=$configs$$.paths.log||__dirname+
|
|
10
|
+
"/log";fs.existsSync($logDirectory$$)||fs.mkdirSync($logDirectory$$);$accessLogStream_rfs$$=$accessLogStream_rfs$$("access.log",{interval:"1d",path:$logDirectory$$});$app$$.use($_port_compress_cookieParser_morgan_multer_root_dir_uploads_session$$("combined",{stream:$accessLogStream_rfs$$,skip:function($req$$,$res$$){return 400>$res$$.statusCode}}));$app$$.use(function($err$$,$req$$,$res$$,$next$$){console.error($err$$.stack);$res$$.status(500).send("Server Error!")});console.log("[app] connect to mongodb",
|
|
11
|
+
$configs$$.database.url);$configs$$.database.url||(console.error("[app] thi\u1ebfu \u0111\u01b0\u1eddng d\u1eabn k\u1ebft n\u1ed1i v\u1edbi database trong file config"),process.exit(1));global.mongoose.connect($configs$$.database.url,{useNewUrlParser:!0}).then(async()=>{console.log("[app] Connected to Database");const $redis$$=require("redis"),{retryStrategyRedis:$retryStrategyRedis$$}=require("./libs/utils");global.clientRedis=$redis$$.createClient({host:"127.0.0.1",port:6379,retry_strategy:$retryStrategyRedis$$});
|
|
12
|
+
global.clientRedis.on("error",$err$$=>console.error("Redis Client Error",$err$$));global.clientRedis.on("end",()=>{console.error("[app] Redis connection closed.")});global.clientRedis.on("connect",function(){console.log("[app] redis connected");$User$$.initClientSockets();0!=$options$$.createRedisCache&&require("./libs/redis-cache").set();$options$$.start_abci_handler&&global.getModel("assabcihandler").start();!0!==$options$$.lite?require("./route")($app$$,()=>{if(!1!==$options$$.createServer){var $StaticPool$jscomp$0$$=
|
|
13
|
+
require("sticky-session");$configs$$.use_ssl?($cpus_report_cpus_server_sslConfig$$=require("./sslConfig"),$cpus_report_cpus_server_sslConfig$$=https.createServer($cpus_report_cpus_server_sslConfig$$($options$$.sslDir),$app$$)):$cpus_report_cpus_server_sslConfig$$=http.createServer($app$$);$cpus_report_cpus_server_sslConfig$$.timeout=6E5;!1!==$options$$.useSocket&&$User$$.initSocket($cpus_report_cpus_server_sslConfig$$);if(!1!==$options$$.cluster){if(!$StaticPool$jscomp$0$$.listen($cpus_report_cpus_server_sslConfig$$,
|
|
14
|
+
global.port))$cpus_report_cpus_server_sslConfig$$.once("listening",function(){console.log("[app] server started on",global.port)});$StaticPool$jscomp$0$$=require("cluster");$StaticPool$jscomp$0$$.on("exit",function($worker$$,$code$$,$signal$$){console.error("[app] worker "+$worker$$.process.pid+" died",$code$$,$signal$$)});$StaticPool$jscomp$0$$.on("online",function($worker$$){console.info("[app] worker "+$worker$$.process.pid+" is online")})}else $cpus_report_cpus_server_sslConfig$$.listen(global.port,
|
|
15
|
+
()=>{console.log("[app] server start at "+global.port+" port")})}var $cpus_report_cpus_server_sslConfig$$=os.cpus().length;$StaticPool$jscomp$0$$=$options$$.import_cpus||($options$$.start_import_data_pool?$options$$.start_report_pool||$options$$.start_input_data_pool?1:Math.round($cpus_report_cpus_server_sslConfig$$/2):0);var $StaticPool$jscomp$1$$=$options$$.input_cpus||($options$$.start_input_data_pool?Math.max(Math.round($cpus_report_cpus_server_sslConfig$$/2),2):0);$cpus_report_cpus_server_sslConfig$$=
|
|
16
|
+
$options$$.report_cpus||($options$$.start_import_data_pool?Math.max(Math.min($cpus_report_cpus_server_sslConfig$$-$StaticPool$jscomp$1$$-$StaticPool$jscomp$0$$,Math.round($cpus_report_cpus_server_sslConfig$$/2))-1,2):0);if($StaticPool$jscomp$1$$){const $StaticPool$$=require("./libs/WorkerStaticPool");global.inputMainPool=new $StaticPool$$(__dirname+"/workers/inputWorker.js",$options$$.max_queue_imports||0,$StaticPool$jscomp$1$$,3E5,$StaticPool$jscomp$1$$,"input pool");global.inputMainPool.exec({load:!0,
|
|
17
|
+
configs:JSON.stringify($configs$$)},()=>{console.log("[app] Crud pool ready")})}$StaticPool$jscomp$0$$&&($StaticPool$jscomp$1$$=require("./libs/WorkerStaticPool"),global.importDataMainPool=new $StaticPool$jscomp$1$$(__dirname+"/workers/inputWorker.js",$options$$.max_queue_imports||0,$StaticPool$jscomp$0$$,36E5,1,"import pool"),global.importDataMainPool.exec({load:!0,configs:JSON.stringify($configs$$)},()=>{console.log("[app] Import pool ready")}));$cpus_report_cpus_server_sslConfig$$&&($StaticPool$jscomp$0$$=
|
|
18
|
+
require("./libs/WorkerStaticPool"),global.reportMainPool=new $StaticPool$jscomp$0$$(__dirname+"/workers/reportWorker.js",$options$$.max_queue_reports||0,$cpus_report_cpus_server_sslConfig$$,3E5,2,"report pool"),global.reportMainPool.exec({load:!0,configs:JSON.stringify($configs$$)},()=>{console.log("[app] Report pool ready")}));$callbackServer$$&&$callbackServer$$()}):require("./route")($app$$,()=>{$callbackServer$$&&$callbackServer$$()},!0)})}).catch($err$$=>{console.log("[app] Not Connected to Database ERROR! ",
|
|
19
|
+
$err$$)});return $app$$};process.on("SIGINT",async()=>{global.sharedRedisConnection&&await global.sharedRedisConnection.quit();process.exit(0)});process.on("uncaughtException",function($err$jscomp$3_error$$){console.error((new Date).toUTCString()+" uncaughtException:",$err$jscomp$3_error$$.message);console.error($err$jscomp$3_error$$.stack);$err$jscomp$3_error$$=`
|
|
20
|
+
Error: ${$err$jscomp$3_error$$.message}
|
|
21
|
+
Stack: ${$err$jscomp$3_error$$.stack}
|
|
22
|
+
`;try{const $logDirectory$$=__dirname+"/log";fs.existsSync($logDirectory$$)||fs.mkdirSync($logDirectory$$);fs.writeFile(`${$logDirectory$$}/error-${(new Date).getTime()}.txt`,$err$jscomp$3_error$$,()=>{console.log("[app] wrote log error")})}catch($e$$){console.log($e$$)}process.exit(1)});module.exports=mainServer;
|