forked from gkozlenko/node-media-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
98 lines (92 loc) · 3.01 KB
/
index.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
'use strict';
const cluster = require('cluster');
const config = require('./config');
const _ = require('lodash');
const path = require('path');
const logger = require('intel').getLogger('app');
let shutdownInterval = null;
let workers = {};
function startWorker(name) {
const worker = cluster.fork({WORKER_NAME: name}).on('online', () => {
logger.info('Start %s worker #%d.', name, worker.id);
workers[name] = workers[name] || [];
workers[name].push(worker.id);
}).on('message', (message) => {
logger.debug('Message from worker #' + worker.id + ':', message);
if (message.action) {
switch (message.action) {
case 'index': {
let indexer = cluster.workers[_.sample(workers.indexer || [])];
if (indexer) {
indexer.send(message);
} else {
logger.debug('Indexer worker not found.');
}
break;
}
}
}
}).on('exit', (status) => {
workers[name] = _.without(workers[name], worker.id);
if (worker.exitedAfterDisconnect === true || status === 0) {
logger.info('Worker %s #%d was killed.', name, worker.id);
} else {
logger.warn('Worker %s #%d was died. Replace it with a new one.', name, worker.id);
startWorker(name);
}
});
}
function shutdownCluster() {
if (cluster.isMaster) {
clearInterval(shutdownInterval);
if (_.size(cluster.workers) > 0) {
logger.info('Shutdown workers:', _.size(cluster.workers));
_.each(cluster.workers, (worker) => {
try {
worker.send({action: 'shutdown'});
} catch (err) {
logger.warn('Cannot send shutdown message to worker:', err);
}
});
shutdownInterval = setInterval(() => {
if (_.size(cluster.workers) === 0) {
process.exit();
}
}, config.shutdownInterval);
} else {
process.exit();
}
}
}
if (cluster.isMaster) {
_.each(config.workers, (conf, name) => {
if (conf.enabled) {
for (let i = 0; i < conf.count; i++) {
startWorker(name);
}
}
});
} else {
const name = process.env.WORKER_NAME;
const WorkerClass = require(path.join(__dirname, 'workers', `${name}.js`));
let worker = null;
if (WorkerClass) {
worker = new WorkerClass(name, config.workers[name]);
worker.start();
worker.on('stop', () => {
process.exit();
});
}
process.on('message', (message) => {
if (message.action === 'shutdown') {
if (worker) {
worker.stop();
} else {
process.exit();
}
}
});
}
// Shutdown
process.on('SIGTERM', shutdownCluster);
process.on('SIGINT', shutdownCluster);