-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosdf.js
303 lines (251 loc) · 9.67 KB
/
osdf.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
#!/usr/bin/node
var _ = require('lodash');
var cluster = require('cluster');
var log4js = require('log4js');
var os = require('os');
var path = require('path');
var config_path = null;
var working_path = null;
var log_file_path = null;
var logger = null;
// Flag that is consulted for whether we will attempt to spawn
// replacement workers if any should die. This will be set to
// true when we shutdown via the 'exit' handler.
var letWorkersDie = false;
// Flags to indicate whether a non-standard config file location or
// non-standard working directories were specified.
var custom_config = false;
var custom_working = false;
var custom_log_file = false;
var osdf_utils = require('osdf_utils');
configure();
function engine_start() {
// Get the configuration.
var config = require('config');
config.load(config_path);
if (cluster.isMaster) {
start_master(config);
} else {
var forked_worker = require('./worker');
forked_worker.start_worker(config, working_path);
}
}
function configure() {
var commander = require('commander');
commander.option('-c, --config <path>',
'Specify a configuration file. Default is ' +
'<OSDF_HOME>/conf/config.ini.')
.option('-w, --working <path>',
'Specify a path to the working directory where ' +
'namespace data is stored.')
.option('-l, --log <path>',
'Specify the path to the log file.')
.parse(process.argv);
config_path = commander.config;
working_path = commander.working;
log_file_path = commander.log;
if (config_path === null || typeof config_path === 'undefined') {
config_path = osdf_utils.get_config();
} else {
osdf_utils.set_config(config_path);
custom_config = true;
}
if (log_file_path === null || typeof log_file_path === 'undefined') {
log_file_path = path.join(osdf_utils.get_osdf_root(), '/logs/osdf.log');
} else {
// Set the path to the log file and...
osdf_utils.set_log_file(log_file_path);
custom_log_file = true;
}
// ...get the logger object
logger = osdf_utils.get_logger();
if (working_path === null || typeof working_path === 'undefined') {
// Nothing specified, get the default
working_path = osdf_utils.get_working_dir();
engine_start();
} else {
custom_working = true;
osdf_utils.set_working_dir(working_path, function() {
engine_start();
});
}
}
function determine_worker_count(config) {
// HOw many workers should we start? Look a thte configruation
// file, and if set to auto, or some non-sensical number, then
// just use the system's CPU count.
var cpu_count = os.cpus().length;
var workers = config.value('global', 'workers');
if (_.isUndefined(workers)) {
workers = cpu_count;
} else if (_.isString(workers)) {
if (workers === 'auto') {
workers = cpu_count;
} else {
workers = parseInt(workers, 10);
}
}
if (workers <= 0) {
logger.warn('Detected worker count of zero. Using CPU count.');
workers = cpu_count;
}
return workers;
}
function start_master(config) {
console.log('OSDF_ROOT: ' + osdf_utils.get_osdf_root());
var workers_ready = 0;
var worker_idx;
var workers_array = [];
var ready_data = {};
var workers = determine_worker_count(config);
if (workers === 1) {
console.log('Running on a single CPU.');
} else {
console.log('Running on ' + workers + ' CPUs.');
}
// Fork a worker for each CPU
for (worker_idx = 0; worker_idx < workers; worker_idx++) {
var worker = cluster.fork();
workers_array.push(worker);
worker.on('message', function(msg) { // jshint ignore:line
if (msg.hasOwnProperty('cmd') && msg['cmd'] === 'user_count') {
ready_data['user_count'] = msg['users'];
}
var type;
if (msg.hasOwnProperty('cmd') && msg['cmd'] === 'schema_change') {
// Send messages to all the workers so that they can adjust
// their lists of primary schemas.
type = msg['type'];
logger.info('Master got a schema change event of type: ' +
type + '. ' + 'Relay this to the workers.');
_.each(workers_array, function(clustered_worker) {
clustered_worker.send(msg);
});
}
if (msg.hasOwnProperty('cmd') &&
msg['cmd'] === 'aux_schema_change') {
// Send messages to all the workers so that they can adjust
// their lists of auxiliary schemas.
type = msg['type'];
logger.info('Master got an auxiliary schema change event of ' +
'type: ' + type + '. Relay this to the workers.');
_.each(workers_array, function(clustered_worker) {
clustered_worker.send(msg);
});
}
if (msg.hasOwnProperty('cmd') && msg['cmd'] === 'abort') {
var reason = msg['reason'];
console.error('Aborting execution. Reason: ' + reason);
letWorkersDie = true;
process.exit(1);
}
if (msg.hasOwnProperty('cmd') && msg['cmd'] === 'init_completed') {
workers_ready++;
if (workers_ready === workers) {
// Show some details about the server after it's up and
// running.
var bind_address = config.value('global', 'bind_address');
var port = config.value('global', 'port');
var cors_enabled = config.value('global', 'cors_enabled');
var https_enabled = config.value('global', 'https_enabled');
ready_data['address'] = bind_address;
ready_data['port'] = port;
ready_data['cors_enabled'] = cors_enabled;
ready_data['https_enabled'] = https_enabled;
ready_data['worker_count'] = workers_array.length;
show_ready(ready_data);
}
}
});
}
// This is for node.js .6.x, in wich the event is called 'death'.
cluster.on('death', function(worker) {
if (! letWorkersDie) {
console.error('Worker ' + process.pid +
' died. Starting a replacement...');
cluster.fork();
}
});
// For node 0.8.x, the 'death' event was renamed to 'exit' on the cluster
// object. See here:
// https://github.com/joyent/node/wiki/API-changes-between-v0.6-and-v0.8
cluster.on('exit', function(worker) {
if (! letWorkersDie) {
console.error('Worker ' + process.pid +
' died. Starting a replacement...');
cluster.fork();
}
});
process.on('SIGTERM', function() {
console.error('Caught SIGTERM. Destroying workers.');
shutdown(workers_array);
});
process.on('exit', function() {
console.error('Exiting. Destroying workers.');
shutdown(workers_array);
});
}
function shutdown(workers) {
// Modify the flag so that the 'death' handler does not attempt
// to replace the workers we are about to destroy off.
letWorkersDie = true;
destroy_workers(workers);
log4js.shutdown();
}
function destroy_workers(workers) {
// Iterate through the workers, and destroy each of them.
_.each(workers, function(worker) {
console.error('Destroying worker ' + process.pid);
if (worker.kill) {
// This is for node.js 0.6.x
worker.kill();
} else {
worker.destroy();
}
});
}
// Display server details when we have started up.
function show_ready(ready_data) {
var user_count = ready_data['user_count'];
var address = ready_data['address'];
var port = ready_data['port'];
var worker_count = ready_data['worker_count'];
var cors_enabled = ready_data['cors_enabled'];
var https_enabled = ready_data['https_enabled'];
if (custom_config) {
console.log('Configured settings file: ' + config_path);
}
if (custom_working) {
console.log('Configured working area: ' + working_path);
}
if (custom_log_file) {
console.log('Configured log file: ' + log_file_path);
}
// Configuration for CORS
var cors = false;
if (cors_enabled !== 'undefined' && cors_enabled !== null &&
(cors_enabled === 'true' || cors_enabled === 'yes')) {
cors = true;
}
// Configuration for encrypted operation
var https = false;
if (https_enabled !== 'undefined' && https_enabled !== null &&
(https_enabled === 'true' || https_enabled === 'yes')) {
https = true;
}
console.log('HTTPS enabled: ' + https);
console.log('CORS enabled: ' + cors);
console.log('Total number of registered OSDF users: ' + user_count);
console.log('Running on node.js version: ' + process.version);
console.log('Workers being used: ' + worker_count);
console.log('Listening on server:port : ' + address + ':' + port);
console.log('===============================================');
console.log('Welcome to');
console.log(
new Buffer('ICBfX19fICBfX19fX19fICBfX19fCiAvIF9fIFwvIF9fLyBfIFwvIF9f' +
'LwovIC9fLyAvXCBcLyAvLyAvIF8vClxfX19fL19fXy9fX19fL18vCgo=',
'base64').toString('utf8')
);
console.log('Open Science Data Framework\n');
console.log('===============================================');
}