Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
104 lines (82 loc) · 3.25 KB
/
Copy pathapp.js
File metadata and controls
104 lines (82 loc) · 3.25 KB
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
'use strict';
global.IS_PROD_MODE = !!process.env.RUN_MODE && process.env.RUN_MODE === 'PROD';
const os = require('os');
const cluster = require('cluster');
const grpc = require('grpc');
const CommonImport = require('./util/CommonImport');
/*
* Constants define.
*/
global.SERVICE_TAG = process.env.SERVICE_TAG;
global.CONSUL = require('microservice-consul');
global.RELATED_MONGODB_COLLECTIONS = {
usersCollectionName: 'Users',
companiesCollectionName: 'Companies'
};
if (cluster.isMaster) {
/*
* The master process should be kept as light as it can be, that is: only do the workers management jobs and some others very necessary jobs.
*/
const workerPortMap = {};
const numOfWorkers = os.cpus().length;
for (var i = 0; i < numOfWorkers; i++) {
const port = 53547 + i;
const worker = cluster.fork({
port: port
});
workerPortMap['' + worker.process.pid] = port;
}
cluster.on('exit', (worker, code, signal) => {
const oriKey = '' + worker.process.pid;
const newWorker = cluster.fork({
port: workerPortMap[oriKey]
});
workerPortMap[newWorker.process.pid] = workerPortMap[oriKey];
delete workerPortMap[oriKey];
});
} else {
/*
* Here the woker process will always be full featured.
*/
const buildUsersGrpcServer = () => {
const usersGrpcServer = new grpc.Server();
const users = grpc.load({root: CommonImport.protos.root, file: CommonImport.protos.users}).microservice.users;
usersGrpcServer.addService(users.Users.service, {
healthCheck: CommonImport.utils.healthCheck,
enterpriseUserSingleRegistV1: require('./api/v1/EnterpriseUserSingleRegistImpl').enterpriseUserSingleRegist,
enterpriseUserStreamBatchRegistV1: require('./api/v1/EnterpriseUserStreamBatchRegistImpl').enterpriseUserStreamBatchRegist,
addContactV1: require('./api/v1/AddContactImpl').addContact,
confirmAddingContactV1: require('./api/v1/ConfirmAddingContactImpl').confirmAddingContact,
removeContactV1: require('./api/v1/RemoveContactImpl').removeContact,
updateProfileV1: require('./api/v1/UpdateProfileImpl').updateProfile,
queryUsersV1: require('./api/v1/QueryUsersImpl').queryUsers
});
return usersGrpcServer;
};
CommonImport.Promise.join(
require('microservice-mongodb-conn-pools')(global.CONSUL.keys.mongodbGate).then((dbPools) => {
return dbPools;
}),
require('microservice-email')(global.CONSUL.keys.emailGate).then((mailerPool) => {
return mailerPool;
}),
CommonImport.utils.pickRandomly(global.CONSUL.agents).kv.get(global.CONSUL.keys['jwtGate']),
buildUsersGrpcServer(),
(dbPools, mailerPool, jwtGateOpts, usersGrpcServer) => {
if (dbPools.length === 0) {
throw new Error('None of the mongodb servers is available.');
}
if (mailerPool.length === 0) {
throw new Error('None of the email servers is available.');
}
if (!jwtGateOpts) {
throw new Error('Invalid gate JWT configurations.');
}
global.DB_POOLS = dbPools;
global.MAILER_POOL = mailerPool;
global.JWT_GATE_OPTS = JSON.parse(jwtGateOpts.Value);
usersGrpcServer.bind('0.0.0.0:' + process.env.port, grpc.ServerCredentials.createInsecure());
usersGrpcServer.start();
}
);
}