-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathworker.js
232 lines (188 loc) · 4.54 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
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
/*!
* Cluster - Worker
* Copyright(c) 2011 LearnBoost <[email protected]>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var EventEmitter = require('events').EventEmitter
, spawn = require('child_process').spawn
, binding = process.binding('net')
, utils = require('./utils')
, net = require('net');
/**
* Node binary.
*/
var node = process.execPath;
/**
* Initialize a new `Worker` with the given `master`.
*
* Signals:
*
* - `SIGINT` immediately exit
* - `SIGTERM` immediately exit
* - `SIGQUIT` graceful exit
*
* @param {Master} master
* @api private
*/
var Worker = module.exports = function Worker(master) {
this.master = master;
this.server = master.server;
};
/**
* Inherit from `EventEmitter.prototype`.
*/
Worker.prototype.__proto__ = EventEmitter.prototype;
/**
* Worker is a receiver.
*/
require('./mixins/receiver')(Worker.prototype);
/**
* Start worker.
*
* @api private
*/
Worker.prototype.start = function(){
var self = this
, call = this.master.call;
// proxy to provide worker id
this.master.call = function(){
var args = utils.toArray(arguments);
// Allow calling master methods that
// don't take worker as first argument
if (false !== args[0]) args.unshift(self.id);
return call.apply(this, args);
};
// stdin
this.stdin = new net.Socket(0, 'unix');
this.stdin.setEncoding('ascii');
this.stdin.on('data', this.frame.bind(this));
this.stdin.resume();
// demote usr/group
if (this.server && this.server.listenFD) {
this.server.on('listening', function(){
var group = self.options.group;
if (group) process.setgid(group);
var user = self.options.user;
if (user) process.setuid(user);
});
// stdin
this.stdin.on('fd', this.server.listenFD.bind(this.server));
}
// signal handlers
process.on('SIGINT', this.destroy.bind(this));
process.on('SIGTERM', this.destroy.bind(this));
process.on('SIGQUIT', this.close.bind(this));
// conditionally handle uncaughtException
process.nextTick(function(){
if (!process.listeners('uncaughtException').length) {
process.on('uncaughtException', function(err){
// stderr for logs
console.error(err.stack || err.message);
// report exception
self.master.call('workerException', err);
// exit
process.nextTick(function(){
self.destroy();
});
});
}
});
};
/**
* Received connect event, set the worker `id`
* and `options`.
*
* @param {String} id
* @param {Object} options
* @api private
*/
Worker.prototype.connect = function(id, options){
this.options = options;
// worker id
this.id = id;
// timeout
this.timeout = options.timeout;
// title
process.title = options['worker title'].replace('{n}', id);
// notify master of connection
this.master.call('connect');
};
/**
* Immediate shutdown.
*
* @api private
*/
Worker.prototype.destroy = function(){
this.emit('close');
process.nextTick(process.exit);
};
/**
* Perform graceful shutdown.
*
* @api private
*/
Worker.prototype.close = function(){
var self = this
, server = this.server;
if (server && server.connections) {
// stop accepting
server.watcher.stop();
// check pending connections
setInterval(function(){
self.master.call('workerWaiting', server.connections);
server.connections || self.destroy();
}, 2000);
// timeout
if (this.timeout) {
setTimeout(function(){
self.master.call('workerTimeout', self.timeout);
self.destroy();
}, this.timeout);
}
} else {
this.destroy();
}
};
/**
* Spawn the worker with the given `id`.
*
* @param {Number} id
* @return {Worker} for chaining
* @api private
*/
Worker.prototype.spawn = function(id){
var fds = binding.socketpair()
, customFds = [fds[0]].concat(this.master.customFds)
, env = {};
// merge env
for (var key in process.env) {
env[key] = process.env[key];
}
this.id = env.CLUSTER_WORKER = id;
// spawn worker process
this.proc = spawn(
node
, this.master.cmd
, { customFds: customFds, env: env });
// unix domain socket for ICP + fd passing
this.sock = new net.Socket(fds[1], 'unix');
// saving file descriptors for later use
this.fds = fds;
return this;
};
/**
* Invoke worker's `method` (called from Master).
*
* @param {String} method
* @param {...} args
* @api private
*/
Worker.prototype.call = function(method){
this.sock.write(utils.frame({
args: utils.toArray(arguments, 1)
, method: method
}), 'ascii');
};