-
Notifications
You must be signed in to change notification settings - Fork 7
/
worker.js
61 lines (49 loc) · 1.16 KB
/
worker.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
var http = require('http');
function output(str){
console.log(str);
}
function main(fn){
fn();
}
var GRACE_EXIT_TIME = 1500;
var server = null;
var exitTimer = null;
var childReqCount = 0;
function aboutExit(){
if(exitTimer) return;
server.close();
exitTimer = setTimeout(function(){
output('worker will exit...');
output('child req total : ' + childReqCount);
process.exit(0);
},GRACE_EXIT_TIME);
}
void main(function(){
process.on('SIGINT' ,aboutExit)
process.on('SIGTERM' ,aboutExit)
server = http.createServer(function(req, res){
var i,r;
for(i=0; i<10000; i++){
r = Math.random();
}
res.writeHead(200 ,{'content-type' : 'text/html'});
res.end('hello,world');
childReqCount++;
});
process.on('message',function(m ,handle){
if(handle){
server.listen(handle, function(err){
if(err){
output('worker listen error');
}else{
process.send({'listenOK' : true});
output('worker listen ok');
}
});
}
if(m.status == 'update'){
process.send({'status' : process.memoryUsage()});
}
});
output('worker is running...');
});