This repository has been archived by the owner on Apr 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathfh-mbaas.js
executable file
·411 lines (361 loc) · 12.5 KB
/
fh-mbaas.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
#!/usr/bin/env node
var TITLE = "fh-mbaas";
process.env.component = TITLE;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
if (!process.env.conf_file) {
process.env.conf_file = process.argv[2];
}
var util = require('util');
var args = require('optimist').argv;
var fs = require('fs');
var path = require('path');
var express = require('express');
var cors = require('cors');
var bodyParser = require('body-parser');
var fhconfig = require('fh-config');
var forms = require('fh-forms');
var fhmbaasMiddleware = require('fh-mbaas-middleware');
var fhServiceAuth = require('fh-service-auth');
var requiredvalidation = require('./lib/util/requiredvalidation.js');
var log = require('./lib/util/logger');
var scheduler;
var logger;
var fhComponentMetrics = require('fh-component-metrics');
var fhcluster = require('fh-cluster');
var cluster = require('cluster');
var formsUpdater = require('./lib/formsUpdater');
var fhlogger = require('fh-logger');
var amqp = require('./lib/util/amqp.js');
var mongoUtils = require('./lib/util/mongo');
var async = require('async');
var models = require('./lib/models');
var mongoose = require('mongoose');
var mongooseConnection;
var START_AGENDA = "startAgenda";
var initModulesCalled = false;
// args and usage
function usage() {
/* eslint-disable no-console */
console.log("Usage: " + args.$0 + " <config file> [-d] (debug) --master-only --workers=[int] \n --master-only will override --workers so should not be used together");
/* eslint-enable no-console */
process.exit(0);
}
if (args.h) {
usage();
}
if (args._.length < 1) {
usage();
}
//Loading The Config First
loadConfig(function() {
if (args.d === true || args["master-only"] === true) {
/* eslint-disable no-console */
console.log("starting single master process");
/* eslint-enable no-console */
startWorker();
initializeScheduler();
} else {
var preferredWorkerId = fhconfig.value('agenda.preferredWorkerId');
// Note: if required as a module, its up to the user to call start();
if (require.main === module) {
var numWorkers = args["workers"];
fhcluster(startWorker, numWorkers, undefined, [
{
workerFunction: initializeScheduler,
startEventId: START_AGENDA,
preferredWorkerId: preferredWorkerId
}
]);
}
}
});
/**
* Print out the given message and error, and exit the process with error code.
*/
function printErrorAndExit(message, err) {
/* eslint-disable no-console */
console.error(message);
console.error(err);
/* eslint-enable no-console */
process.exit(-1);
}
/**
* Loading Module Config From File System
* @param cb
*/
function loadConfig(cb) {
// read our config file
var configFile = process.env.conf_file || args._[0];
fhconfig.init(configFile, requiredvalidation, function(err) {
if (err) {
return printErrorAndExit("Problems reading config file: " + configFile, err);
}
createAndSetLogger();
//we call the ensureFormsUserExists function here as we only want to call it once. No point to call it in every worker.
ensureFormsUserExists(function(err) {
if (err) {
return printErrorAndExit("failed creating mongodb user for forms", err);
}
return cb();
});
});
}
function createAndSetLogger() {
// We are overwriting any logger created in fhconfig.init and replacing
// it with an fh-logger instance to allow the underlying logger to be
// controlled (setting log levels for example)
logger = fhlogger.createLogger(fhconfig.getConfig().rawConfig.logger);
log.setLogger(logger);
//Setting up namespace for the logger. This allows the logger to track request IDs
//when mongoose queries have completed.
var clsMongoose = require('fh-cls-mongoose');
var loggerNamespace = logger.getLoggerNamespace();
clsMongoose(loggerNamespace, mongoose);
fhconfig.setLogger(logger);
}
/**
* Make sure the mongodb user for forms exists.
*/
function ensureFormsUserExists(cb) {
var appConfig = fhconfig.getConfig().rawConfig;
var mongoConfig = appConfig.mongo;
var mongoConfigKey;
if (mongoUtils.hasUserSpaceDb()) {
mongoConfig = appConfig.mongo_userdb;
mongoConfigKey = 'mongo_userdb';
}
var mongoAdminDbUrl = mongoUtils.getAdminDbUrl(fhconfig.mongoConnectionString(mongoConfigKey), mongoConfig.admin_auth);
mongoUtils.createDbUser(mongoAdminDbUrl, {username: mongoConfig.form_user_auth.user, password: mongoConfig.form_user_auth.pass, roles: ['readWriteAnyDatabase']}, cb);
}
/**
* Initialise the fh-forms module.
*/
function initFormsModule(formsModule, logger, cb) {
appConfig = fhconfig.getConfig().rawConfig;
var mongoConfig = appConfig.mongo;
var mongoConfigKey;
if (mongoUtils.hasUserSpaceDb()) {
mongoConfig = appConfig.mongo_userdb;
mongoConfigKey = 'mongo_userdb';
}
//Setting logger for fh-forms
formsModule.init(logger);
formsModule.setupSharedMongoConnections(logger, fhconfig.mongoConnectionString(mongoConfigKey),{auth: mongoConfig.form_user_auth, poolSize: mongoConfig.poolSize}, function(err, sharedConnections) {
if (err) {
logger.error("failed to setup shared mongo connections for fh-forms", {error: err});
return cb(err);
}
//Setting global formsModule config
logger.debug("minsPerBackOffIndex", fhconfig.int('fhmbaas.dsMinsPerBackOffIndex'));
formsModule.core.setSharedConnections(sharedConnections);
formsModule.core.setConfig({
minsPerBackOffIndex: fhconfig.int('fhmbaas.dsMinsPerBackOffIndex')
});
return cb();
});
}
/**
* Initialising The Scheduler. This is bound to a single worker using fh-cluster.
* @param clusterWorker
*/
function initializeScheduler(clusterWorker) {
//Ensuring that the config is loaded.
initModules(clusterWorker, getMbaasMiddlewareConfig(), function() {
if (clusterWorker !== undefined) {
logger.info("Initialising scheduler ", clusterWorker.id, clusterWorker.process.pid);
} else {
logger.info("Initialising scheduler.");
}
scheduler = formsUpdater.scheduler(logger, fhconfig.getConfig().rawConfig, fhconfig.mongoConnectionString());
logger.info("Initialised scheduler", scheduler);
var appDataExportAgenda = require('./lib/export');
appDataExportAgenda.scheduler().start();
});
}
function startWorker(clusterWorker) {
// Note: location/order of these required for istanbul code coverage is important.
if (fhconfig.bool('fhmbaas.code_coverage_enabled')) {
var coverage = require('istanbul-middleware');
coverage.hookLoader(__dirname);
}
setupUncaughtExceptionHandler(logger, clusterWorker);
setupFhconfigReloadHandler(fhconfig);
if (fhconfig.bool('component_metrics.enabled')) {
initComponentMetrics(fhconfig.value('component_metrics'));
}
initModules(clusterWorker, getMbaasMiddlewareConfig(), startApp);
}
function initComponentMetrics(metricsConf) {
var metrics = fhComponentMetrics(metricsConf);
metrics.memory(TITLE, { interval: 2000 }, function(err) {
if (err) {
logger.warn(err);
}
});
metrics.cpu(TITLE, { interval: 1000 }, function(err) {
if (err) {
logger.warn(err);
}
});
}
function getMbaasMiddlewareConfig() {
var conf = fhconfig.getConfig();
var jsonConfig = {
mongoUrl: fhconfig.mongoConnectionString(),
mongo : {
host: conf.rawConfig.mongo.host,
port: conf.rawConfig.mongo.port,
name: conf.rawConfig.mongo.name,
admin_auth: {
user: conf.rawConfig.mongo.admin_auth.user,
pass: conf.rawConfig.mongo.admin_auth.pass
},
replicaSet: conf.rawConfig.mongo.replicaset_name
},
crash_monitor: conf.rawConfig.crash_monitor,
email: conf.rawConfig.email,
fhamqp: conf.rawConfig.fhamqp,
logger: logger
};
if (conf.rawConfig.mongo_userdb) {
jsonConfig.mongoUserUrl = fhconfig.mongoConnectionString('mongo_userdb');
jsonConfig.mongo_userdb = {
host: conf.rawConfig.mongo_userdb.host,
port: conf.rawConfig.mongo_userdb.port,
name: conf.rawConfig.mongo_userdb.name,
admin_auth: {
user: conf.rawConfig.mongo_userdb.admin_auth.user,
pass: conf.rawConfig.mongo_userdb.admin_auth.pass
},
replicaSet: conf.rawConfig.mongo_userdb.replicaset_name
};
}
logger.debug('JSON Config ', jsonConfig);
return jsonConfig;
}
function initModules(clusterWorker, jsonConfig, cb) {
if (initModulesCalled) {
return process.nextTick(() => cb());
}
initModulesCalled = true;
async.parallel([
async.apply(async.waterfall, [
async.constant(jsonConfig),
fhmbaasMiddleware.init
]),
function initialiseModels(cb) {
//The models should not be initialised on the mongoose connection for fh-mbaas-middleware. The document instanceof checks will not
//pass and none of the returned models will have the correct schemas attached.
mongooseConnection = mongoose.createConnection(fhconfig.mongoConnectionString());
handleMongoConnectionEvents(mongooseConnection);
models.init(mongooseConnection, cb);
},
async.apply(initFormsModule, forms, logger),
async.apply(initAmqp, jsonConfig),
async.apply(fhServiceAuth.init, {logger: logger})
], function(err) {
if (!err) {
return cb();
}
initModulesCalled = false;
logger.error(err);
if (clusterWorker) {
clusterWorker.kill();
} else {
process.exit(1);
}
});
}
function initAmqp(config, cb) {
var deployStatusHandler = require('./lib/messageHandlers/deployStatusHandler.js');
var amqpConnection = amqp.connect(config);
deployStatusHandler.listenToDeployStatus(amqpConnection, config, function() {
cb();
});
}
function startApp( ) {
var app = express();
app.use(logger.requestIdMiddleware);
if (fhconfig.bool('component_metrics.enabled')) {
app.use(fhComponentMetrics.timingMiddleware(TITLE, fhconfig.value('component_metrics')));
}
// Enable CORS for all requests
app.use(cors());
// Request logging
app.use(require('express-bunyan-logger')({ logger: logger, parseUA: false }));
// Parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}));
// Parse JSON payloads
app.use(bodyParser.json({limit: fhconfig.value('fhmbaas.maxpayloadsize') || "20mb"}));
app.use('/sys', require('./lib/handlers/sys.js')());
app.use('/api/mbaas', require('./lib/handlers/api.js'));
app.use('/api/app', require('./lib/handlers/app.js'));
app.use('/api/storage', require('./lib/storage').router);
var port = fhconfig.int('fhmbaas.port');
app.listen(port, function() {
// Get our version number from package.json
var pkg = JSON.parse(fs.readFileSync(path.join(__dirname, './package.json'), "utf8"));
/* eslint-disable no-console */
console.log("Started " + TITLE + " version: " + pkg.version + " at: " + new Date() + " on port: " + port);
/* eslint-enable no-console */
});
}
function setupFhconfigReloadHandler(fhconfig) {
if (process.env.FHDEV === 'true') {
return;
}
process.on(fhconfig.RELOAD_CONFIG_SIGNAL, function() {
fhconfig.reloadRawConfig(function(err) {
if (err) {
/* eslint-disable no-console */
console.error("Config not reloaded");
console.error(err);
console.error("Please fix and try again!!");
/* eslint-enable no-console */
}
createAndSetLogger();
});
});
}
//Closing a mongoose connection if needed.
function closeMongooseConnection() {
if (mongooseConnection) {
mongooseConnection.close();
}
}
function handleMongoConnectionEvents(conn) {
if (conn) {
conn.on('error', function(err) {
logger.error('Mongo connection error: ' + err);
throw err;
});
conn.on('disconnected', function() {
logger.error('Mongo connection lost. Socket closed');
throw new Error('Mongo close even emitted');
});
}
}
function setupUncaughtExceptionHandler(logger, worker) {
// handle uncaught exceptions
process.on('uncaughtException', function(err) {
logger.error("FATAL: UncaughtException, please report: " + util.inspect(err));
/* eslint-disable no-console */
console.error(new Date().toString() + " FATAL: UncaughtException, please report: " + util.inspect(err));
/* eslint-enable no-console */
if (err !== undefined && err.stack !== undefined) {
logger.error(util.inspect(err.stack));
/* eslint-disable no-console */
console.trace(err.stack);
/* eslint-enable no-console */
}
if (worker && worker.process) {
worker.process.exit(1);
} else {
process.exit(1);
}
});
// If the Node process ends, close the Mongoose connection
process.on('SIGINT', closeMongooseConnection).on('SIGTERM', closeMongooseConnection);
}