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 | var qoper8;
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | qoper8 = require('ewd-qoper8');
|
75 |
|
76 | var sockets = require('./sockets');
|
77 | var configureMicroServices = require('./microServices');
|
78 | var fs = require('fs');
|
79 |
|
80 | var q;
|
81 | var app;
|
82 | var qx;
|
83 |
|
84 |
|
85 | function start(params, routes) {
|
86 | params = params || {};
|
87 | var maxq = params.max_queue_length || 20000;
|
88 | q = new qoper8.masterProcess(maxq);
|
89 |
|
90 | console.log('Double ended queue max length set to ' + maxq);
|
91 |
|
92 | var emptyMap = !params.moduleMap;
|
93 | var moduleMap = params.moduleMap || {};
|
94 | if (routes) {
|
95 | routes.forEach(function(route) {
|
96 | var path = route.path;
|
97 | if (path.charAt(0) === '/') {
|
98 | path = path.substring(1);
|
99 | }
|
100 | var module = route.module || path;
|
101 | moduleMap[path] = module;
|
102 | emptyMap = false;
|
103 | });
|
104 | }
|
105 | if (emptyMap) moduleMap = false;
|
106 |
|
107 | var config = {
|
108 | managementPassword: params.managementPassword || 'keepThisSecret',
|
109 | serverName: params.serverName || 'qewd',
|
110 | port: params.port || 8080,
|
111 | poolSize: params.poolSize || 1,
|
112 | poolPrefork: params.poolPrefork || false,
|
113 | idleLimit: params.idleLimit || 3600000,
|
114 | webServer: params.webServer || 'express',
|
115 | webServerRootPath: params.webServerRootPath || process.cwd() + '/www/',
|
116 | no_sockets: params.no_sockets || false,
|
117 | webSockets: params.webSockets || {module: 'socket.io'},
|
118 | ssl: params.ssl || false,
|
119 | cors: params.cors || false,
|
120 | masterProcessPid: process.pid,
|
121 | database: params.database,
|
122 | errorLogFile: params.errorLogFile || false,
|
123 | mode: params.mode || 'production',
|
124 | bodyParser: params.bodyParser || false,
|
125 | addMiddleware: params.addMiddleware || false,
|
126 | addMiddlewareUp: params.addMiddlewareUp || false,
|
127 | initialSessionTimeout: params.initialSessionTimeout || 300,
|
128 | sessionDocumentName: params.sessionDocumentName || 'CacheTempEWDSession',
|
129 | moduleMap: moduleMap,
|
130 | lockSession: params.lockSession || false,
|
131 | resilientMode: params.resilientMode || false,
|
132 | customSocketModule: params.customSocketModule || false,
|
133 | jwt: params.jwt || false,
|
134 | qewd_up: params.qewd_up || false,
|
135 | permit_application_switch: params.permit_application_switch,
|
136 | service_name: params.service_name || false
|
137 | };
|
138 | if (params.resilientMode) {
|
139 | config.resilientMode = {
|
140 | documentName: params.resilientMode.queueDocumentName || 'ewdQueue',
|
141 | keepPeriod: params.resilientMode.keepPeriod || 3600
|
142 | }
|
143 | }
|
144 |
|
145 | if (config.webServer !== 'express' && config.webServer !== 'koa') config.webServer = 'express';
|
146 |
|
147 | if (config.jwt) {
|
148 | q.jwt = config.jwt;
|
149 | q.jwt.handlers = require('./jwtHandler');
|
150 | }
|
151 |
|
152 | if (params.u_services) {
|
153 | configureMicroServices.call(q, params.u_services);
|
154 | }
|
155 |
|
156 | qx = require('ewd-qoper8-' + config.webServer);
|
157 | var wsConfig = require('./master-' + config.webServer);
|
158 | qx.addTo(q);
|
159 | config.qxBuild = qx.build;
|
160 |
|
161 | app = wsConfig(config, routes, q, qx);
|
162 |
|
163 | q.on('start', function() {
|
164 | this.worker.module = 'qewd.worker';
|
165 |
|
166 | this.setWorkerPoolSize(config.poolSize);
|
167 | this.setWorkerIdleLimit(config.idleLimit);
|
168 |
|
169 | if (params.database && params.resilientMode) {
|
170 |
|
171 |
|
172 |
|
173 | console.log('starting resilient mode..');
|
174 | this.resilientMode = {
|
175 | documentName: config.resilientMode.documentName
|
176 | };
|
177 | var db = params.database;
|
178 | var type = db.type;
|
179 |
|
180 | if (type === 'cache') db.params.lock = '1';
|
181 | if (type === 'cache' || type === 'gtm' || type === 'redis') {
|
182 | console.log('connecting master process to ' + type);
|
183 | this.on('dbOpened', function() {
|
184 | if (type !== 'redis') {
|
185 | console.log(this.db.version());
|
186 | }
|
187 | else {
|
188 | this.db.version(function(version) {
|
189 | console.log(version);
|
190 | });
|
191 | }
|
192 | });
|
193 | this.on('dbClosed', function() {
|
194 | console.log('Connection to ' + db.type + ' closed');
|
195 | });
|
196 | if (type === 'redis') {
|
197 | if (!db.params) db.params = {};
|
198 |
|
199 | var redisParams = {
|
200 | host: db.params.host,
|
201 | port: db.params.port,
|
202 | integer_padding: db.params.integer_padding,
|
203 | key_separator: db.params.key_separator,
|
204 | async: true
|
205 | };
|
206 | console.log('starting ' + type + ' in master process for resilient mode: ' + JSON.stringify(redisParams));
|
207 | require('ewd-qoper8-redis')(this, redisParams);
|
208 | return;
|
209 | }
|
210 | require('ewd-qoper8-' + type)(this, db.params);
|
211 | }
|
212 | }
|
213 | });
|
214 |
|
215 | q.on('started', function() {
|
216 | if (!this.userDefined) this.userDefined = {};
|
217 | this.userDefined.config = config;
|
218 | var q = this;
|
219 | var io;
|
220 | var server;
|
221 |
|
222 | function startServer() {
|
223 | if (!config.ssl) {
|
224 | server = app.listen(config.port);
|
225 | }
|
226 | else {
|
227 | var https = require('https');
|
228 |
|
229 | var options = {
|
230 | key: fs.readFileSync(config.ssl.keyFilePath),
|
231 | cert: fs.readFileSync(config.ssl.certFilePath)
|
232 | };
|
233 | server = https.createServer(options, app).listen(config.port);
|
234 | }
|
235 | if (!config.no_sockets) {
|
236 |
|
237 |
|
238 |
|
239 | if (!config.webSockets.module) config.webSockets.module = 'socket.io';
|
240 | if (config.webSockets.module === 'socket.io') {
|
241 | if (!config.webSockets.engine) {
|
242 | io = require('socket.io')(server, {wsEngine: 'ws'});
|
243 | }
|
244 | else {
|
245 | var engine = config.webSockets.engine;
|
246 | if (engine !== 'ws' && engine !== 'uws') engine = 'ws';
|
247 | io = require('socket.io')(server, {wsEngine: engine});
|
248 | }
|
249 | }
|
250 | }
|
251 |
|
252 |
|
253 | if (io) sockets(q, io, config.customSocketModule);
|
254 |
|
255 | console.log('========================================================');
|
256 | console.log('QEWD.js is listening on port ' + config.port);
|
257 | console.log('========================================================');
|
258 |
|
259 | q.on('response', function(messageObj) {
|
260 |
|
261 | if (messageObj.socketId) {
|
262 |
|
263 | var id = messageObj.socketId;
|
264 | delete messageObj.socketId;
|
265 | delete messageObj.finished;
|
266 | io.to(id).emit('ewdjs', messageObj);
|
267 | }
|
268 | });
|
269 | }
|
270 |
|
271 | if (config.poolPrefork) {
|
272 | var workersStarted = 0;
|
273 | q.on('workerStarted', function(workerPid, customQueue) {
|
274 | var worker = q.worker.process[workerPid];
|
275 |
|
276 |
|
277 |
|
278 | if (worker) { worker.isAvailable = false; }
|
279 | workersStarted++;
|
280 | if (workersStarted === q.worker.poolSize) {
|
281 |
|
282 | startServer();
|
283 |
|
284 | for (var pid in q.worker.process) {
|
285 | worker = q.worker.process[pid];
|
286 | worker.isAvailable = true;
|
287 | }
|
288 |
|
289 | if (customQueue || q.queue.length > 0) q.processQueue(customQueue);
|
290 | }
|
291 | });
|
292 |
|
293 | for (var i = 0; i < q.worker.poolSize; i++) {
|
294 | q.startWorker();
|
295 | }
|
296 | }
|
297 | else {
|
298 | startServer();
|
299 | }
|
300 |
|
301 | });
|
302 |
|
303 |
|
304 | q.start();
|
305 | return q;
|
306 | }
|
307 |
|
308 | function intercept() {
|
309 | return {
|
310 | app: app,
|
311 | q: q,
|
312 | qx: qx
|
313 | };
|
314 | }
|
315 |
|
316 | module.exports = {
|
317 | intercept: intercept,
|
318 | start: start
|
319 | };
|
320 |
|