forked from Juniper/contrail-web-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjobServerStart.js
276 lines (252 loc) · 8.51 KB
/
jobServerStart.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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
var assert = require('assert');
var cluster = require('cluster');
var args = process.argv.slice(2);
var argsCnt = args.length;
var configFile = null;
for (var i = 0; i < argsCnt; i++) {
if (('--c' == args[i]) || ('--conf_file' == args[i])) {
if (null == args[i + 1]) {
console.error('Config file not provided');
assert(0);
} else {
configFile = args[i + 1];
try {
var tmpConfig = require(configFile);
if ((null == tmpConfig) || (typeof tmpConfig !== 'object')) {
console.error('Config file ' + configFile + ' is not valid');
assert(0);
}
break;
} catch(e) {
console.error('Config file ' + configFile + ' not found');
assert(0);
}
}
}
}
/* Set corePath before loading any other contrail module */
var corePath = process.cwd();
var config =
require('./src/serverroot/common/config.utils').compareAndMergeDefaultConfig(configFile);
exports.corePath = corePath;
exports.config = config;
var redisUtils = require('./src/serverroot/utils/redis.utils');
var global = require('./src/serverroot/common/global');
var jobsUtils = require('./src/serverroot/common/jobs.utils');
var commonUtils = require('./src/serverroot/utils/common.utils');
var async = require('async')
var clusterUtils = require('./src/serverroot/utils/cluster.utils');
var server_port = (config.redis_server_port) ?
config.redis_server_port : global.DFLT_REDIS_SERVER_PORT;
var server_ip = (config.redis_server_ip) ?
config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
function loadJobServer () {
jobsUtils.jobKueEventEmitter.on('kueReady', function() {
/* Now start real server processing */
if (false == process.kueReinitReqd) {
return;
}
var purgeRedisClient = redisUtils.createRedisClient();//createDefRedisClientAndWait(function(purgeRedisClient) {
jobServerPurgeAndStart(purgeRedisClient, function() {
startServers();
process.kueReinitReqd = false;
});
});
var axon = require('axon')
, assert = require('assert')
, redisPub = require('./src/serverroot/jobs/core/redisPub')
, kue = require('kue')
, logutils = require('./src/serverroot/utils/log.utils')
, discServ = require('./src/serverroot/jobs/core/discoveryservice.api')
, fs = require('fs')
, jobsApi = require('./src/serverroot/jobs/core/jobs.api')
, jsonPath = require('JSONPath').eval;
var hostName = config.jobServer.server_ip
, port = config.jobServer.server_port;
var workerSock = axon.socket('pull');
var myIdentity = global.service.MIDDLEWARE;
var discServEnable = ((null != config.discoveryService) &&
(null != config.discoveryService.enable)) ?
config.discoveryService.enable : true;
var pkgList = commonUtils.mergeAllPackageList(global.service.MIDDLEWARE);
assert(pkgList);
exports.myIdentity = myIdentity;
exports.discServEnable = discServEnable;
exports.pkgList = pkgList;
/* Function: processMsg
Handler for message processing for messages coming from main Server
*/
processMsg = function (msg) {
jobsApi.createJobByMsgObj(msg);
}
/* Function: connectToMainServer
This function is used to connect to main Server on host and port
defined in config.global.js
*/
var resetDone = false;
connectToMainServer = function () {
if (true == resetDone) {
return;
}
var connectURL = 'tcp://' + hostName + ":" + port;
workerSock.connect(connectURL);
logutils.logger.info('Job Server connected to port ' + port);
resetDone = true;
workerSock.on('message', function (msg) {
/* Now based on the message type, act */
processMsg(msg);
});
}
kueJobListen = function() {
/* kue UI listening port */
var kuePort = config.kue.ui_port || 3002;
kue.app.listen(kuePort, '127.0.0.1');
}
function createVRouterSummaryJob ()
{
var appData = {};
appData['addGen'] = true;
var jobObj = {};
var url = '/virtual-routers';
jobObj['jobName'] = global.STR_GET_VROUTERS_SUMMARY;
jobObj['url'] = url;
jobObj['firstRunDelay'] = global.VROUTER_SUMM_JOB_REFRESH_TIME;
jobObj['runCount'] = 0;
jobObj['nextRunDelay'] = global.VROUTER_SUMM_JOB_REFRESH_TIME;
jobObj['orchModel'] = 'openstack';
jobObj['appData'] = appData;
jobsApi.createJobAtInit(jobObj);
}
function createVRouterGeneratorsJob ()
{
var url = '/virtual-routers';
var jobObj = {};
jobObj['jobName'] = global.STR_GET_VROUTERS_GENERATORS;
jobObj['url'] = url;
jobObj['firstRunDelay'] = global.VROUTER_SUMM_JOB_REFRESH_TIME;
jobObj['runCount'] = 0;
jobObj['nextRunDelay'] = global.VROUTER_GENR_JOB_REFRESH_TIME;
jobObj['orchModel'] = 'openstack';
jobsApi.createJobAtInit(jobObj);
}
function createJobsAtInit ()
{
var authApi = require('./src/serverroot/common/auth.api');
if (true == authApi.isMultiRegionSupported()) {
/* Do not cache if multi region is supported */
return;
}
createVRouterSummaryJob();
createVRouterGeneratorsJob();
}
function registerTojobListenerEvent()
{
var pkgDir;
var pkgListLen = pkgList.length;
for (var i = 0; i < pkgListLen; i++) {
if (pkgList[i]['jobProcess.xml']) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
var jobListLen = pkgList[i]['jobProcess.xml'].length;
for (var j = 0; j < jobListLen; j++) {
logutils.logger.debug("Registering jobListeners: " +
pkgDir + pkgList[i]['jobProcess.xml'][j]);
require(pkgDir +
pkgList[i]['jobProcess.xml'][j]).addjobListenerEvent();
require(pkgDir + pkgList[i]['jobProcess.xml'][j]).jobsProcess();
}
}
}
}
/* Function: doFeatureTaskInit
This function is used to do all initializations of all the feature packages
installed and if is set as enabled in config file
*/
function doFeatureTaskInit ()
{
var featurePkgList = config.featurePkg;
for (key in featurePkgList) {
if ((config.featurePkg[key]) && (config.featurePkg[key]['path']) &&
((null == config.featurePkg[key]['enable']) ||
(true == config.featurePkg[key]['enable'])) &&
(true == fs.existsSync(config.featurePkg[key]['path'] +
'/webroot/common/api/init.api.js'))) {
var initApi = require(config.featurePkg[key]['path'] +
'/webroot/common/api/init.api.js');
if (initApi.featureInit) {
initApi.featureInit();
}
}
}
}
function startServers ()
{
kueJobListen();
registerTojobListenerEvent();
jobsApi.doCheckJobsProcess();
if (true == discServEnable) {
discServ.createRedisClientAndStartSubscribeToDiscoveryService(global.service.MIDDLEWARE);
discServ.startWatchDiscServiceRetryList();
}
redisPub.createRedisPubClient(function() {
connectToMainServer();
createJobsAtInit();
doFeatureTaskInit();
process.send("INIT IS DONE");
});
}
}
function jobServerPurgeAndStart (redisClient, callback)
{
var redisDBs = [global.WEBUI_SESSION_REDIS_DB, global.WEBUI_DFLT_REDIS_DB,
global.QE_DFLT_REDIS_DB, global.SM_DFLT_REDIS_DB];
async.map(redisDBs, commonUtils.flushRedisDB, function() {
/* Already logged */
callback();
});
}
function messageHandler (msg)
{
if ((null != msg) && (null != msg.cmd)) {
switch(msg.cmd) {
case global.MSG_CMD_KILLALL:
/* Kill the worker process and let master again fork it */
clusterUtils.killAllWorkers();
break;
}
}
}
function startJobCluster ()
{
if (cluster.isMaster) {
clusterMasterInit(function(err) {
clusterUtils.forkWorkers();
clusterUtils.addClusterEventListener(messageHandler);
});
} else {
clusterWorkerInit(function(err) {
loadJobServer();
});
}
}
function clusterWorkerInit (callback)
{
var purgeRedisClient = redisUtils.createRedisClient();
async.parallel([
function(CB) {
jobServerPurgeAndStart(purgeRedisClient, function() {
CB(null, null);
});
}
],
function(error, results) {
callback();
});
}
function clusterMasterInit(callback)
{
callback();
}
startJobCluster();