-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrs.js
256 lines (209 loc) · 7.6 KB
/
drs.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
#!/usr/bin/node
const _ = require('lodash');
const cluster = require('cluster');
const log4js = require('log4js');
const os = require('os');
const path = require('path');
const drs_utils = require('drs_utils');
const format = require('string-format');
format.extend(String.prototype);
let config_path = null;
let working_path = null;
let log_file_path = null;
let 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.
let letWorkersDie = false;
// Flags to indicate whether a non-standard config file location or
// non-standard working directories were specified.
let custom_config = false;
let custom_working = false;
let custom_log_file = false;
function engine_start() {
// Get the configuration.
var config = require('config');
config.load(config_path);
if (cluster.isMaster) {
start_master(config);
} else {
try {
var forked_worker = require('./worker');
forked_worker.start_worker(config, working_path);
} catch (e) {
process.send({'cmd': 'abort', 'reason': e.message});
}
}
}
function configure() {
const { Command } = require('commander');
const program = new Command();
program.option('-c, --config <path>',
'Specify a configuration file. Default is ' +
'<DRS_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.');
program.parse(process.argv);
const options = program.opts();
config_path = options.config;
working_path = options.working;
log_file_path = options.log;
if (_.isNil(config_path)) {
config_path = drs_utils.get_config();
} else {
drs_utils.set_config(config_path);
custom_config = true;
}
if (_.isNil(log_file_path)) {
log_file_path = path.join(drs_utils.get_drs_root(), '/logs/drs.log');
} else {
// Set the path to the log file and...
drs_utils.set_log_file(log_file_path);
custom_log_file = true;
}
// ...get the logger object
logger = drs_utils.get_logger();
if (_.isNil(working_path)) {
// Nothing specified, get the default
working_path = drs_utils.get_working_dir();
engine_start();
} else {
custom_working = true;
drs_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('DRS_ROOT: ' + drs_utils.get_drs_root());
let workers_ready = 0;
let worker_idx;
let workers_array = [];
let ready_data = {};
let 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 (_.has(msg, 'cmd') && msg['cmd'] === 'abort') {
var reason = msg['reason'];
console.error('Aborting execution. Reason: ' + reason);
letWorkersDie = true;
process.exit(1);
}
if (_.has(msg, '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');
ready_data['address'] = bind_address;
ready_data['port'] = port;
ready_data['worker_count'] = workers_array.length;
show_ready(ready_data);
}
}
});
}
cluster.on('exit', function() {
if (! letWorkersDie) {
console.error('Worker {} died. Starting a replacement...'.format(process.pid));
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 {}.'.format(process.pid));
worker.destroy();
});
}
// Display server details when we have started up.
function show_ready(ready_data) {
let address = ready_data['address'];
let port = ready_data['port'];
let worker_count = ready_data['worker_count'];
let 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 encrypted operation
var https = false;
if ((! _.isNil(https_enabled)) &&
(https_enabled === 'true' || https_enabled === 'yes')) {
https = true;
}
console.log('Running on node.js version: {}'.format(process.version));
console.log('Workers being used: {}'.format(worker_count));
console.log('HTTPS enabled: {}'.format(https));
console.log('Listening on server:port : {}:{}'.format(address, port));
console.log('===============================================');
console.log('Welcome to');
console.log('HMP Data Repository Service (DRS)\n');
console.log(
Buffer.from(
'ICBfICAgIF8gX18gIF9fIF9fX19fICAgIF9fX19fICBfX19fXyAgIF9fX1' +
'9fICAKIHwgfCAgfCB8ICBcLyAgfCAgX18gXCAgfCAgX18gXHwgIF9fIFwg' +
'LyBfX19ffCAKIHwgfF9ffCB8IFwgIC8gfCB8X18pIHwgfCB8ICB8IHwgfF' +
'9fKSB8IChfX18KIHwgIF9fICB8IHxcL3wgfCAgX19fLyAgfCB8ICB8IHwg' +
'IF8gIC8gXF9fXyBcICAKIHwgfCAgfCB8IHwgIHwgfCB8ICAgICAgfCB8X1' +
'98IHwgfCBcIFwgX19fXykgfAogfF98ICB8X3xffCAgfF98X3wgICAgICB8' +
'X19fX18vfF98ICBcX1xfX19fXy8=', 'base64').toString('utf8')
);
console.log('\n');
console.log('===============================================');
}
configure();