-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathworker.js
More file actions
91 lines (75 loc) · 1.53 KB
/
worker.js
File metadata and controls
91 lines (75 loc) · 1.53 KB
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
var options = JSON.parse(process.argv[2])
, requires = options.requires;
/**
* Run requires before loading the server file.
* This ensures that any requires module that
* changes the behavior of the process or
* require() function (such as coffeescript)
* are present.
*/
for (var i = 0, l = requires.length; i < l; i++) {
require(requires[i]);
}
/**
* Start server.
*/
var server = require(options.file)
, assumeReady = options.assumeReady
, addr = null;
/**
* Set our process title.
*/
if (options.title) {
process.title = options.title + ' worker';
}
/**
* Tell master, that we are and that we are ready
*/
function ready() {
if (addr === null) return assumeReady = true;
process.send({
type: 'addr',
addr: addr
});
}
/**
* Listen.
*/
server.listen(function () {
addr = this.address();
if (assumeReady) {
ready();
}
});
/**
* RPC channel with master.
*/
process.on('message', function (msg) {
switch (msg.type) {
case 'die':
setTimeout(function () {
process.kill(process.pid);
}, msg.time);
break;
case 'ready':
ready();
break;
}
});
/**
* Ping master, on failure commit suicide
* as we're lost in limbo.
*
* TODO: ideally we just use process.ppid (which doesn't exist)
* and nul-signal the parent.
*/
if (options.pingInterval) {
setInterval(function(){
try {
process.send({ type: 'ping' });
} catch (err) {
console.error('master killed, committing suicide');
process.exit(1);
}
}, options.pingInterval);
}