-
Notifications
You must be signed in to change notification settings - Fork 4
/
start.js
445 lines (383 loc) · 19.3 KB
/
start.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
The MIT License (MIT)
Copyright (c) 2014 microServiceBus.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/* jshint node: true */
/* jshint esversion: 6 */
/* jshint strict:false */
'use strict';
require('colors');
var util = require('./lib/Utils.js');
var pjson = require('./package.json');
var checkVersion = require('package-json');
var fs = require('fs');
var os = require('os');
var async = require('async');
let network = require('msb-network');
let minimist = require('minimist');
console.log(`PROCESS: ${require('process').pid}`)
process.on('unhandledRejection', err => {
console.log("SERIOUS ERROR: Caught unhandledRejection. ", err);
});
var _ipAddress;
var maxWidth = 75;
var debugPort = 9230;
let debug = minimist(process.argv.slice(2)).inspect || minimist(process.execArgv).inspect;
var args = process.argv.slice(1);
if (debug)
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
else {
network.get_active_interface(function (err, nw) {
_ipAddress = nw.ip_address;
});
}
//process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// Load settings
var SettingsHelper = require("./lib/SettingsHelper.js");
var settingsHelper = new SettingsHelper();
if (debug) {
console.log("Start with debug");
start(true);
}
else {
startWithoutDebug();
}
function startWithoutDebug() {
var cluster = require('cluster');
var fixedExecArgv = [];
if (cluster.isMaster) {
var worker = cluster.fork(process.env);
cluster.on('exit', function (worker, code, signal) {
console.log('Exit called. code:' + signal);
worker = cluster.fork(process.env);
console.log('CODE:' + code.bgGreen.white.bold);
if (code === 99) { // Controlled exit
console.log("Controlled exit");
process.exit(0);
}
else if (cluster.settings.execArgv.find(function (e) { return e.startsWith('--inspect'); }) !== undefined) {
console.log();
console.log(util.padRight("", maxWidth, ' ').bgGreen.white.bold);
console.log(util.padRight(" IN DEBUG", maxWidth, ' ').bgGreen.white.bold);
console.log(util.padRight("", maxWidth, ' ').bgGreen.white.bold);
console.log();
}
else {
console.log();
console.log(util.padRight(" NORMAL START", maxWidth, ' ').bgGreen.white.bold);
console.log();
}
});
cluster.on('message', function (worker, message, handle) {
try {
//console.log(util.padRight(" MESSAGE CALLED:" + JSON.stringify(message) + ".", maxWidth, ' ').bgGreen.white.bold);
if (message.cmd === 'START-DEBUG') {
console.log(util.padRight(" DEBUG MODE", maxWidth, ' ').bgGreen.white.bold);
fixedExecArgv.push('--inspect=' + _ipAddress + ':' + debugPort);
cluster.setupMaster({
execArgv: fixedExecArgv
});
}
else if (message.cmd === 'SHUTDOWN') {
process.exit(99);
}
else {
cluster.setupMaster({
execArgv: []
});
for (var id in cluster.workers) {
console.log(util.padRight(" Killing", maxWidth, ' ').bgGreen.white.bold);
cluster.workers[id].process.disconnect();
cluster.workers[id].process.kill('SIGTERM');
}
}
}
catch (gerr) {
console.log("ERROR (on message): ".bgRed.white + gerr);
}
});
}
if (cluster.isWorker) {
console.log("start worker");
start();
}
process.on('uncaughtException', function (err) {
if (err.errno === 'ECONNREFUSED') {
for (var id in cluster.workers) {
console.log(util.padRight(" Killing", maxWidth, ' ').bgRed.white.bold);
cluster.workers[id].process.disconnect();
cluster.workers[id].process.kill('SIGTERM');
}
}
});
}
function start(testFlag) {
var started = false;
console.log();
console.log(util.padRight("", maxWidth, ' ').bgBlue.white.bold);
console.log(util.padRight(" microServicebus.com", maxWidth, ' ').bgBlue.white.bold);
console.log(util.padRight(" Node.js version : " + process.version, maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" NPM package version : " + pjson.version, maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" Architecture : " + process.arch, maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" Agent host : " + settingsHelper.agentHost, maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" Home directory : " + settingsHelper.homeDirectory, maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" NPM directory : " + settingsHelper.nodePackagePath, maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" Licences : https://microservicebus.com/licenses", maxWidth, ' ').bgBlue.white);
console.log(util.padRight(" GIT repository : https://github.com/axians/microservicebus-node", maxWidth, ' ').bgBlue.white);
console.log(util.padRight("", maxWidth, ' ').bgBlue.white.bold);
console.log(util.padRight(" For more information visit: http://microservicebus.com", maxWidth, ' ').bgBlue.white);
console.log(util.padRight("", maxWidth, ' ').bgBlue.white.bold);
console.log();
checkVersionsAndStart(async (err) => {
if (err) {
if ((err.code === "ECONNREFUSED" ||
err.code === "EACCES" ||
err.code === "ENOTFOUND" ||
err.code === "EAI_AGAIN") &&
settingsHelper.settings.policies &&
settingsHelper.settings.policies.disconnectPolicy.offlineMode) {
console.log("Testing internet connection...")
let internetConnectinon = await callUri("https://8.8.8.8");
console.log(`...internet connection: ${internetConnectinon}`);
console.log("Testing DNS...")
let dnsResolve = await callUri("https://registry.npmjs.org");
console.log(`...DNS: ${dnsResolve}`);
if (!internetConnectinon) {
console.log('Starting in offline mode'.red);
try {
var MicroServiceBusHost = require("microservicebus-core").Host;
var microServiceBusHost = new MicroServiceBusHost(settingsHelper);
microServiceBusHost.OnStarted(function (loadedCount, exceptionCount) {
});
microServiceBusHost.OnStopped(function () {
});
microServiceBusHost.OnUpdatedItineraryComplete(function () {
});
microServiceBusHost.Start(testFlag);
}
catch (ex) {
// This can happen if core has not been installed (or has been removed) and the gateway does not have internet access by the time
// the snap starts up
process.kill(process.pid, 'SIGKILL');
}
}
else {
console.log("Not able to resolve DNS...rebooting in 5 minutes");
setTimeout(() => {
util.reboot();
}, 5 * 60 * 1000);
}
}
else {
console.log('Retrying...');
setTimeout(() => {
process.kill(process.pid, 'SIGKILL');
}, 5000);
}
}
});
var callUri = function (uri) {
return new Promise((resolve) => {
try {
const req = https.get(uri);
req.on('data', function (err) {
resolve(true);
});
req.on('error', function (err) {
resolve(false);
});
} catch (error) {
resolve(false);
}
});
}
function checkVersionsAndStart(done) {
async.waterfall([
function (callback) { // Check version of microservicebus-node
checkVersion(pjson.name)
.then(function (rawData) {
var latest = rawData['dist-tags'].latest;
if (util.compareVersion(pjson.version, latest) < 0) {
console.log();
console.log(util.padRight("", maxWidth, ' ').bgRed.white.bold);
console.log(util.padRight("There is a new version of microservicebus-node: " + latest, maxWidth, ' ').bgRed.white.bold);
console.log(util.padRight("type: 'npm update microservicebus-node' ", maxWidth, ' ').bgRed.gray.bold);
console.log(util.padRight(" from the root folder to get the latest version", maxWidth, ' ').bgRed.gray.bold);
console.log(util.padRight("", maxWidth, ' ').bgRed.white.bold);
console.log();
}
console.log("Checked version from microservicebus-node");
callback();
})
.catch(function (err) {
console.log("General error checking version from microservicebus-node: " + err);
callback();
});
},
function (callback) { // Check version of microservicebus-core
console.log("Check version of microservicebus-core");
let microservicebusCore = "microservicebus-core";
if (pjson.config && pjson.config.microservicebusCore) {
microservicebusCore = pjson.config.microservicebusCore;
//console.log("mSB.core: " + microservicebusCore);
}
checkVersion(microservicebusCore)
.then(function (rawData) {
try {
let path = require("path");
let packagePath;
let packageFile;
// Check if node is started as Snap
if (process.env["SNAP_USER_DATA"] != null) {
console.log("Loading microservicebus-core/package.json for snap");
console.log("nodePackagePath: " + settingsHelper.nodePackagePath)
packageFile = path.resolve(settingsHelper.nodePackagePath, 'microservicebus-core/package.json');
}
else {
try {
packagePath = require.resolve(rawData.name);
packagePath = path.dirname(packagePath);
packageFile = path.resolve(packagePath, 'package.json');
}
catch (e) { }
}
var corePjson;
if (fs.existsSync(packageFile)) {
corePjson = util.requireNoCache(packageFile);
console.log(`microservicebus-core version: ${corePjson.version}`);
}
var isBeta = args.find(function (a) {
return a === "--beta";
});
var latest = rawData['dist-tags'].latest;
var beta = rawData['dist-tags'].beta;
var experimental = rawData['dist-tags'].experimental;
var coreVersion = latest;
if (isBeta) {
coreVersion = rawData['dist-tags'].beta;
console.log('RUNNING IN BETA MODE'.yellow);
}
else if (settingsHelper.settings.coreVersion) {
switch (settingsHelper.settings.coreVersion) {
case "latest":
coreVersion = latest;
break;
case "beta":
coreVersion = beta;
break;
case "experimental":
coreVersion = experimental;
break;
case "ignore":
coreVersion = corePjson.version;
default:
coreVersion = settingsHelper.settings.coreVersion;
break;
}
}
console.log(`Running ${coreVersion} mode`);
if (corePjson === undefined || util.compareVersion(corePjson.version, coreVersion) !== 0) {
var version = corePjson === undefined ? "NONE" : corePjson.version;
console.log();
console.log(util.padRight("", maxWidth, ' ').bgGreen.white.bold);
console.log(util.padRight(" New version of Core available. Performing update, please wait...", maxWidth, ' ').bgGreen.white.bold);
console.log(util.padRight(" Current version: " + version + ". New version: " + coreVersion, maxWidth, ' ').bgGreen.white.bold);
console.log(util.padRight("", maxWidth, ' ').bgGreen.white.bold);
console.log();
let corePkg = isBeta ? rawData.name + "@beta" : rawData.name + "@" + coreVersion;
let successfulUpdate = true;
let updateComplete = false;
setTimeout(() => {
if (updateComplete) return;
successfulUpdate = false;
console.log(`Unable to install ${coreVersion} version of microservicebus-core. Restarting process.`.red);
process.exit();
}, 10 * 60 * 1000); // Installation should complete in 5 min
util.addNpmPackage(corePkg, true, function (err) {
updateComplete = true;
if (!successfulUpdate) {
// The prossess should have started with old config
return;
}
if (err) {
console.log("Unable to install core update".bgRed.white);
console.log("Error: " + err);
callback(err);
}
else {
console.log("Core installed successfully".bgGreen.white);
callback();
}
});
}
else {
console.log(`Running latest version`);
callback();
}
}
catch (err) {
console.log("Error checking version from microservicebus-core" + err);
callback(err);
}
})
.catch(function (err) {
console.log("General error checking version from microservicebus-core: " + err);
callback(err);
});
}
],
function (err) { // Starting microServiceBusHost
console.log("Starting microServiceBusHost");
// Add path to home directory to global paths
require('app-module-path').addPath(settingsHelper.nodePackagePath);
require('module').globalPaths.push(settingsHelper.nodePackagePath);
if (err) {
console.log('ERROR: ' + err);
done(err);
}
else {
if (process.env["MSB_USE_IMEI"] == 'true') {
console.log("MSB_USE_IMEI === true");
process.argv.push("--imei");
}
let microservicebusCore = "microservicebus-core";
if (pjson.config && pjson.config.microservicebusCore) {
microservicebusCore = pjson.config.microservicebusCore;
}
console.log("Starting ".grey + microservicebusCore.grey);
let cachedFiles = Object.keys(require.cache).filter((c) => {
return true;//c.indexOf(microservicebusCore) >= 0;
});
cachedFiles.forEach((c) => {
delete require.cache[c];
});
console.log('require cache has been cleared'.grey);
var MicroServiceBusHost = require(microservicebusCore).Host;
var microServiceBusHost = new MicroServiceBusHost(settingsHelper);
microServiceBusHost.OnStarted(function (loadedCount, exceptionCount) {
});
microServiceBusHost.OnStopped(function () {
});
microServiceBusHost.OnUpdatedItineraryComplete(function () {
});
microServiceBusHost.Start(testFlag);
done();
}
});
}
}