-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·166 lines (131 loc) · 4.19 KB
/
server.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
/*
CLUSTER SERVER SOURCE
*/
"use strict";
var config = require("config"),
cluster = require("cluster"),
_ = require("underscore"),
AMQPStats = require("amqp-stats"),
Logger = require("basic-logger"),
log = new Logger( config.logger ),
Syslog = require( "node-syslog" ),
util = require("util"),
Worker = require("./worker"),
exiting = false,
workers = {};
Logger.setLevel(config.logger.level, true);
// Syslog
Syslog.init(config.logger.prefix, Syslog.LOG_PID | Syslog.LOG_ODELAY, Syslog.LOG_LOCAL0);
var SyslogLevels = {
error: Syslog.LOG_ERROR,
warning: Syslog.LOG_WARNING,
info: Syslog.LOG_INFO,
debug: Syslog.LOG_DEBUG,
trace: Syslog.LOG_TRACE
};
var SyslogLevelOrder = [ "error", "warning", "info", "debug", "trace" ];
var oldLogFunction = log.log;
log.log = function(msg, levelName) {
var args = Array.prototype.splice.call(arguments,2);
if ( SyslogLevelOrder.indexOf(levelName) <= SyslogLevelOrder.indexOf(config.logger.level) ) {
Syslog.log(SyslogLevels[levelName],util.inspect([msg].concat(args),false,2,false));
}
oldLogFunction.call(log,msg,levelName,args);
};
if (cluster.isMaster) {
log.info("Cluster master started with pid", process.pid );
var workerInit = function _workerInit(workerName) {
var clusterWorker = cluster.fork({
worker: workerName
}).on("message",function(msg){
if (msg && msg.uncaughtException) {
workerInit( msg.name );
}
});
workers[clusterWorker.id] = workerName;
};
cluster.on("exit",function(worker,code) {
log.info("Worker",worker.process.pid,"exited.", _.size(cluster.workers),code);
if (exiting) {
if (_.size(cluster.workers) === 0) {
log.info("All worker exited. Cluster terminating.");
process.exit();
}
}
else if (workers[worker.id] && code !== 3) {
log.warn("Restarting worker",workers[worker.id]);
workerInit(workers[worker.id]);
}
delete workers[worker.id];
});
process.on("SIGTERM",function() {
log.info("Cluster exit signal");
exiting = true;
if (_.size(cluster.workers).length === 0) {
return process.exit();
}
for(var id in cluster.workers) {
cluster.workers[id].send("exitWorker");
}
});
Object.keys(config.workers).forEach(function(worker){
for(var i = 0; i < (config.workers[worker].min || 1); i++) {
workerInit( worker );
}
});
var stats = new AMQPStats({
username: config.rabbitmq.login,
password: config.rabbitmq.password,
hostname: config.rabbitmq.host + ":" + config.rabbitmq.managementPort
});
var loadBalancer = function _loadBalancer() {
stats.queues(function(err, res, data){
if (err) { throw err; }
log.debug("Statcheck");
var queues = {};
data.forEach(function(queue){
queues[queue.name] = queue;
});
Object.keys(config.workers).forEach(function(workerName){
if (config.workers[workerName].threshold) {
var changeNum = 0;
Object.keys(config.workers[workerName].threshold).forEach(function(thresholdQueueName){
if ( config.workers[workerName].threshold[thresholdQueueName].count <= queues[ config.tasks[thresholdQueueName].queue ].messages_ready ) {
if ( _.countBy(workers)[workerName] < config.workers[workerName].max ) {
changeNum = 1;
}
}
else if (queues[ config.tasks[thresholdQueueName].queue ].messages_ready === 0) {
if ( _.countBy(workers)[workerName] > config.workers[workerName].min ) {
if (!changeNum) {
changeNum = -1;
}
}
}
});
if (changeNum > 0) {
workerInit( workerName );
}
else if (changeNum < 0) {
for(var id in cluster.workers) {
if ( workers[id] === workerName ) {
delete workers[id];
cluster.workers[id].send("exitWorker");
break;
}
}
}
}
});
setTimeout(loadBalancer, config.loadBalancer.timeout);
});
};
setTimeout(loadBalancer, config.loadBalancer.timeout);
}
else {
log.info( "Starting worker with pid", cluster.worker.process.pid );
cluster.worker.on("SIGTERM",function(){
console.log("SIGTERM");
});
new Worker(cluster.worker.process.env.worker);
}