2012-02-27 20:07:49 +01:00
|
|
|
|
# Cluster
|
|
|
|
|
|
|
|
|
|
Stability: 1 - Experimental
|
2011-10-12 23:19:32 +02:00
|
|
|
|
|
|
|
|
|
A single instance of Node runs in a single thread. To take advantage of
|
|
|
|
|
multi-core systems the user will sometimes want to launch a cluster of Node
|
|
|
|
|
processes to handle the load.
|
|
|
|
|
|
2011-11-07 20:37:05 +01:00
|
|
|
|
The cluster module allows you to easily create a network of processes that
|
|
|
|
|
all share server ports.
|
2011-10-12 23:19:32 +02:00
|
|
|
|
|
2011-10-27 01:21:08 +02:00
|
|
|
|
var cluster = require('cluster');
|
|
|
|
|
var http = require('http');
|
2011-11-04 23:11:19 +01:00
|
|
|
|
var numCPUs = require('os').cpus().length;
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
2011-11-04 23:11:19 +01:00
|
|
|
|
// Fork workers.
|
|
|
|
|
for (var i = 0; i < numCPUs; i++) {
|
|
|
|
|
cluster.fork();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('exit', function(worker, code, signal) {
|
2011-11-04 23:11:19 +01:00
|
|
|
|
console.log('worker ' + worker.pid + ' died');
|
|
|
|
|
});
|
2011-10-27 01:21:08 +02:00
|
|
|
|
} else {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
// Workers can share any TCP connection
|
|
|
|
|
// In this case its a HTTP server
|
|
|
|
|
http.createServer(function(req, res) {
|
2011-10-27 01:21:08 +02:00
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end("hello world\n");
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Running node will now share port 8000 between the workers:
|
|
|
|
|
|
|
|
|
|
% node server.js
|
2011-10-12 23:19:32 +02:00
|
|
|
|
Worker 2438 online
|
|
|
|
|
Worker 2437 online
|
|
|
|
|
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
This feature was introduced recently, and may change in future versions.
|
|
|
|
|
Please try it out and provide feedback.
|
|
|
|
|
|
2012-06-12 19:02:52 +02:00
|
|
|
|
## How It Works
|
|
|
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
|
|
|
|
|
The worker processes are spawned using the `child_process.fork` method,
|
|
|
|
|
so that they can communicate with the parent via IPC and pass server
|
|
|
|
|
handles back and forth.
|
|
|
|
|
|
|
|
|
|
When you call `server.listen(...)` in a worker, it serializes the
|
|
|
|
|
arguments and passes the request to the master process. If the master
|
|
|
|
|
process already has a listening server matching the worker's
|
|
|
|
|
requirements, then it passes the handle to the worker. If it does not
|
|
|
|
|
already have a listening server matching that requirement, then it will
|
|
|
|
|
create one, and pass the handle to the child.
|
|
|
|
|
|
|
|
|
|
This causes potentially surprising behavior in three edge cases:
|
|
|
|
|
|
|
|
|
|
1. `server.listen({fd: 7})` Because the message is passed to the worker,
|
|
|
|
|
file descriptor 7 **in the parent** will be listened on, and the
|
|
|
|
|
handle passed to the worker, rather than listening to the worker's
|
|
|
|
|
idea of what the number 7 file descriptor references.
|
|
|
|
|
2. `server.listen(handle)` Listening on handles explicitly will cause
|
|
|
|
|
the worker to use the supplied handle, rather than talk to the master
|
|
|
|
|
process. If the worker already has the handle, then it's presumed
|
|
|
|
|
that you know what you are doing.
|
|
|
|
|
3. `server.listen(0)` Normally, this will case servers to listen on a
|
|
|
|
|
random port. However, in a cluster, each worker will receive the
|
|
|
|
|
same "random" port each time they do `listen(0)`. In essence, the
|
|
|
|
|
port is random the first time, but predictable thereafter. If you
|
|
|
|
|
want to listen on a unique port, generate a port number based on the
|
|
|
|
|
cluster worker ID.
|
|
|
|
|
|
|
|
|
|
When multiple processes are all `accept()`ing on the same underlying
|
|
|
|
|
resource, the operating system load-balances across them very
|
|
|
|
|
efficiently. There is no routing logic in Node.js, or in your program,
|
|
|
|
|
and no shared state between the workers. Therefore, it is important to
|
|
|
|
|
design your program such that it does not rely too heavily on in-memory
|
|
|
|
|
data objects for things like sessions and login.
|
|
|
|
|
|
|
|
|
|
Because workers are all separate processes, they can be killed or
|
|
|
|
|
re-spawned depending on your program's needs, without affecting other
|
|
|
|
|
workers. As long as there are some workers still alive, the server will
|
|
|
|
|
continue to accept connections. Node does not automatically manage the
|
|
|
|
|
number of workers for you, however. It is your responsibility to manage
|
|
|
|
|
the worker pool for your application's needs.
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## cluster.settings
|
|
|
|
|
|
|
|
|
|
* {Object}
|
|
|
|
|
* `exec` {String} file path to worker file. (Default=`__filename`)
|
|
|
|
|
* `args` {Array} string arguments passed to worker.
|
|
|
|
|
(Default=`process.argv.slice(2)`)
|
|
|
|
|
* `silent` {Boolean} whether or not to send output to parent's stdio.
|
|
|
|
|
(Default=`false`)
|
|
|
|
|
|
|
|
|
|
All settings set by the `.setupMaster` is stored in this settings object.
|
|
|
|
|
This object is not supposed to be change or set manually, by you.
|
|
|
|
|
|
|
|
|
|
## cluster.isMaster
|
|
|
|
|
|
|
|
|
|
* {Boolean}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
True if the process is a master. This is determined
|
2011-12-20 10:42:48 +01:00
|
|
|
|
by the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is
|
2012-02-27 20:07:49 +01:00
|
|
|
|
undefined, then `isMaster` is `true`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## cluster.isWorker
|
|
|
|
|
|
|
|
|
|
* {Boolean}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
This boolean flag is true if the process is a worker forked from a master.
|
2012-02-27 20:07:49 +01:00
|
|
|
|
If the `process.env.NODE_UNIQUE_ID` is set to a value, then
|
2011-12-20 10:42:48 +01:00
|
|
|
|
`isWorker` is `true`.
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## Event: 'fork'
|
|
|
|
|
|
|
|
|
|
* `worker` {Worker object}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
When a new worker is forked the cluster module will emit a 'fork' event.
|
|
|
|
|
This can be used to log worker activity, and create you own timeout.
|
|
|
|
|
|
|
|
|
|
var timeouts = [];
|
2012-05-06 00:07:58 +02:00
|
|
|
|
function errorMsg() {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
console.error("Something must be wrong with the connection ...");
|
2012-05-06 00:07:58 +02:00
|
|
|
|
}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('fork', function(worker) {
|
2012-05-30 20:32:50 +02:00
|
|
|
|
timeouts[worker.id] = setTimeout(errorMsg, 2000);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
});
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('listening', function(worker, address) {
|
2012-05-30 20:32:50 +02:00
|
|
|
|
clearTimeout(timeouts[worker.id]);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
});
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('exit', function(worker, code, signal) {
|
2012-05-30 20:32:50 +02:00
|
|
|
|
clearTimeout(timeouts[worker.id]);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
errorMsg();
|
|
|
|
|
});
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## Event: 'online'
|
|
|
|
|
|
|
|
|
|
* `worker` {Worker object}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
After forking a new worker, the worker should respond with a online message.
|
|
|
|
|
When the master receives a online message it will emit such event.
|
|
|
|
|
The difference between 'fork' and 'online' is that fork is emitted when the
|
2012-02-27 20:07:49 +01:00
|
|
|
|
master tries to fork a worker, and 'online' is emitted when the worker is
|
|
|
|
|
being executed.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('online', function(worker) {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
console.log("Yay, the worker responded after it was forked");
|
|
|
|
|
});
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## Event: 'listening'
|
|
|
|
|
|
|
|
|
|
* `worker` {Worker object}
|
2012-04-28 14:24:17 +02:00
|
|
|
|
* `address` {Object}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
When calling `listen()` from a worker, a 'listening' event is automatically assigned
|
|
|
|
|
to the server instance. When the server is listening a message is send to the master
|
|
|
|
|
where the 'listening' event is emitted.
|
|
|
|
|
|
2012-04-28 14:24:17 +02:00
|
|
|
|
The event handler is executed with two arguments, the `worker` contains the worker
|
|
|
|
|
object and the `address` object contains the following connection properties:
|
|
|
|
|
`address`, `port` and `addressType`. This is very useful if the worker is listening
|
|
|
|
|
on more than one address.
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('listening', function(worker, address) {
|
2012-04-28 14:24:17 +02:00
|
|
|
|
console.log("A worker is now connected to " + address.address + ":" + address.port);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
});
|
|
|
|
|
|
2012-03-10 16:30:06 +01:00
|
|
|
|
## Event: 'disconnect'
|
|
|
|
|
|
|
|
|
|
* `worker` {Worker object}
|
|
|
|
|
|
|
|
|
|
When a workers IPC channel has disconnected this event is emitted. This will happen
|
2012-03-19 21:34:12 +01:00
|
|
|
|
when the worker dies, usually after calling `.destroy()`.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2012-03-19 21:34:12 +01:00
|
|
|
|
When calling `.disconnect()`, there may be a delay between the
|
2012-03-30 21:24:46 +02:00
|
|
|
|
`disconnect` and `exit` events. This event can be used to detect if
|
2012-03-19 21:34:12 +01:00
|
|
|
|
the process is stuck in a cleanup or if there are long-living
|
|
|
|
|
connections.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
|
|
|
|
cluster.on('disconnect', function(worker) {
|
2012-05-30 20:32:50 +02:00
|
|
|
|
console.log('The worker #' + worker.id + ' has disconnected');
|
2012-03-10 16:30:06 +01:00
|
|
|
|
});
|
|
|
|
|
|
2012-03-30 21:24:46 +02:00
|
|
|
|
## Event: 'exit'
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
|
|
|
|
* `worker` {Worker object}
|
2012-05-30 20:32:50 +02:00
|
|
|
|
* `code` {Number} the exit code, if it exited normally.
|
2012-05-06 00:07:58 +02:00
|
|
|
|
* `signal` {String} the name of the signal (eg. `'SIGHUP'`) that caused
|
|
|
|
|
the process to be killed.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-03-30 21:24:46 +02:00
|
|
|
|
When any of the workers die the cluster module will emit the 'exit' event.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
This can be used to restart the worker by calling `fork()` again.
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('exit', function(worker, code, signal) {
|
2012-05-02 18:38:31 +02:00
|
|
|
|
var exitCode = worker.process.exitCode;
|
|
|
|
|
console.log('worker ' + worker.pid + ' died ('+exitCode+'). restarting...');
|
2011-12-20 10:42:48 +01:00
|
|
|
|
cluster.fork();
|
|
|
|
|
});
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## Event: 'setup'
|
|
|
|
|
|
|
|
|
|
* `worker` {Worker object}
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
When the `.setupMaster()` function has been executed this event emits.
|
|
|
|
|
If `.setupMaster()` was not executed before `fork()` this function will
|
|
|
|
|
call `.setupMaster()` with no arguments.
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## cluster.setupMaster([settings])
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
* `settings` {Object}
|
|
|
|
|
* `exec` {String} file path to worker file. (Default=`__filename`)
|
|
|
|
|
* `args` {Array} string arguments passed to worker.
|
|
|
|
|
(Default=`process.argv.slice(2)`)
|
|
|
|
|
* `silent` {Boolean} whether or not to send output to parent's stdio.
|
|
|
|
|
(Default=`false`)
|
|
|
|
|
|
|
|
|
|
The `setupMaster` is used to change the default 'fork' behavior. It takes
|
|
|
|
|
one option object argument.
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
var cluster = require("cluster");
|
|
|
|
|
cluster.setupMaster({
|
|
|
|
|
exec : "worker.js",
|
|
|
|
|
args : ["--use", "https"],
|
|
|
|
|
silent : true
|
|
|
|
|
});
|
|
|
|
|
cluster.autoFork();
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## cluster.fork([env])
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
* `env` {Object} Key/value pairs to add to child process environment.
|
|
|
|
|
* return {Worker object}
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
Spawn a new worker process. This can only be called from the master process.
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## cluster.settings
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
* {Object}
|
|
|
|
|
* `exec` {String} file path to worker file. Default: `__filename`
|
|
|
|
|
* `args` {Array} string arguments passed to worker.
|
|
|
|
|
(Default=`process.argv.slice(2)`)
|
|
|
|
|
* `silent` {Boolean} whether or not to send output to parent's stdio.
|
|
|
|
|
(Default=`false`)
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
All settings set by the `.setupMaster` is stored in this settings object.
|
|
|
|
|
This object is not supposed to be change or set manually.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-03-10 16:30:06 +01:00
|
|
|
|
## cluster.disconnect([callback])
|
|
|
|
|
|
|
|
|
|
* `callback` {Function} called when all workers are disconnected and handlers are closed
|
|
|
|
|
|
2012-03-19 21:34:12 +01:00
|
|
|
|
When calling this method, all workers will commit a graceful suicide. When they are
|
2012-03-10 16:30:06 +01:00
|
|
|
|
disconnected all internal handlers will be closed, allowing the master process to
|
|
|
|
|
die graceful if no other event is waiting.
|
|
|
|
|
|
2012-03-19 21:34:12 +01:00
|
|
|
|
The method takes an optional callback argument which will be called when finished.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## cluster.workers
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
* {Object}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
In the cluster all living worker objects are stored in this object by there
|
2012-05-30 20:32:50 +02:00
|
|
|
|
`id` as the key. This makes it easy to loop through all living workers.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-01-05 08:57:54 +01:00
|
|
|
|
// Go through all workers
|
2011-12-20 10:42:48 +01:00
|
|
|
|
function eachWorker(callback) {
|
2012-05-30 20:32:50 +02:00
|
|
|
|
for (var id in cluster.workers) {
|
|
|
|
|
callback(cluster.workers[id]);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2012-05-06 00:07:58 +02:00
|
|
|
|
eachWorker(function(worker) {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
worker.send('big announcement to all workers');
|
|
|
|
|
});
|
|
|
|
|
|
2012-01-05 08:57:54 +01:00
|
|
|
|
Should you wish to reference a worker over a communication channel, using
|
2012-05-30 20:32:50 +02:00
|
|
|
|
the worker's unique id is the easiest way to find the worker.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-05-30 20:32:50 +02:00
|
|
|
|
socket.on('data', function(id) {
|
|
|
|
|
var worker = cluster.workers[id];
|
2011-12-20 10:42:48 +01:00
|
|
|
|
});
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
## Class: Worker
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
A Worker object contains all public information and method about a worker.
|
2012-01-05 08:57:54 +01:00
|
|
|
|
In the master it can be obtained using `cluster.workers`. In a worker
|
|
|
|
|
it can be obtained using `cluster.worker`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-05-30 20:32:50 +02:00
|
|
|
|
### worker.id
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
|
|
|
|
* {String}
|
|
|
|
|
|
|
|
|
|
Each new worker is given its own unique id, this id is stored in the
|
2012-05-30 20:32:50 +02:00
|
|
|
|
`id`.
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
|
|
|
|
While a worker is alive, this is the key that indexes it in
|
|
|
|
|
cluster.workers
|
|
|
|
|
|
|
|
|
|
### worker.process
|
|
|
|
|
|
|
|
|
|
* {ChildProcess object}
|
|
|
|
|
|
|
|
|
|
All workers are created using `child_process.fork()`, the returned object
|
|
|
|
|
from this function is stored in process.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
See: [Child Process module](child_process.html)
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
### worker.suicide
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
* {Boolean}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-03-10 16:30:06 +01:00
|
|
|
|
This property is a boolean. It is set when a worker dies after calling `.destroy()`
|
|
|
|
|
or immediately after calling the `.disconnect()` method. Until then it is `undefined`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
### worker.send(message, [sendHandle])
|
|
|
|
|
|
|
|
|
|
* `message` {Object}
|
|
|
|
|
* `sendHandle` {Handle object}
|
|
|
|
|
|
|
|
|
|
This function is equal to the send methods provided by
|
|
|
|
|
`child_process.fork()`. In the master you should use this function to
|
|
|
|
|
send a message to a specific worker. However in a worker you can also use
|
|
|
|
|
`process.send(message)`, since this is the same function.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
This example will echo back all messages from the master:
|
|
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
|
var worker = cluster.fork();
|
|
|
|
|
worker.send('hi there');
|
|
|
|
|
|
|
|
|
|
} else if (cluster.isWorker) {
|
2012-05-06 00:07:58 +02:00
|
|
|
|
process.on('message', function(msg) {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
process.send(msg);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
### worker.destroy()
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
This function will kill the worker, and inform the master to not spawn a
|
2012-03-30 22:54:13 +02:00
|
|
|
|
new worker. The boolean `suicide` lets you distinguish between voluntary
|
|
|
|
|
and accidental exit.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.on('exit', function(worker, code, signal) {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
if (worker.suicide === true) {
|
2012-01-17 05:36:01 +01:00
|
|
|
|
console.log('Oh, it was just suicide\' – no need to worry').
|
2011-12-20 10:42:48 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// destroy worker
|
|
|
|
|
worker.destroy();
|
|
|
|
|
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2012-04-20 16:42:57 +02:00
|
|
|
|
### worker.disconnect()
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
|
|
|
|
When calling this function the worker will no longer accept new connections, but
|
|
|
|
|
they will be handled by any other listening worker. Existing connection will be
|
|
|
|
|
allowed to exit as usual. When no more connections exist, the IPC channel to the worker
|
|
|
|
|
will close allowing it to die graceful. When the IPC channel is closed the `disconnect`
|
2012-03-30 21:24:46 +02:00
|
|
|
|
event will emit, this is then followed by the `exit` event, there is emitted when
|
2012-03-10 16:30:06 +01:00
|
|
|
|
the worker finally die.
|
|
|
|
|
|
|
|
|
|
Because there might be long living connections, it is useful to implement a timeout.
|
|
|
|
|
This example ask the worker to disconnect and after 2 seconds it will destroy the
|
|
|
|
|
server. An alternative wound be to execute `worker.destroy()` after 2 seconds, but
|
|
|
|
|
that would normally not allow the worker to do any cleanup if needed.
|
|
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
|
var worker = cluser.fork();
|
|
|
|
|
var timeout;
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
worker.on('listening', function(address) {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
worker.disconnect();
|
2012-05-06 00:07:58 +02:00
|
|
|
|
timeout = setTimeout(function() {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
worker.send('force kill');
|
|
|
|
|
}, 2000);
|
|
|
|
|
});
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
worker.on('disconnect', function() {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else if (cluster.isWorker) {
|
|
|
|
|
var net = require('net');
|
2012-05-06 00:07:58 +02:00
|
|
|
|
var server = net.createServer(function(socket) {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
// connection never end
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
server.on('close', function() {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
// cleanup
|
|
|
|
|
});
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
process.on('message', function(msg) {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
if (msg === 'force kill') {
|
|
|
|
|
server.destroy();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
### Event: 'message'
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
* `message` {Object}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
This event is the same as the one provided by `child_process.fork()`.
|
|
|
|
|
In the master you should use this event, however in a worker you can also use
|
|
|
|
|
`process.on('message')`
|
|
|
|
|
|
|
|
|
|
As an example, here is a cluster that keeps count of the number of requests
|
|
|
|
|
in the master process using the message system:
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
|
|
|
|
var cluster = require('cluster');
|
|
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
|
|
2011-12-20 10:42:48 +01:00
|
|
|
|
// Keep track of http requests
|
|
|
|
|
var numReqs = 0;
|
2011-11-04 23:33:49 +01:00
|
|
|
|
setInterval(function() {
|
|
|
|
|
console.log("numReqs =", numReqs);
|
|
|
|
|
}, 1000);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
// Count requestes
|
2012-05-06 00:07:58 +02:00
|
|
|
|
function messageHandler(msg) {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
if (msg.cmd && msg.cmd == 'notifyRequest') {
|
|
|
|
|
numReqs += 1;
|
|
|
|
|
}
|
2012-05-06 00:07:58 +02:00
|
|
|
|
}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
// Start workers and listen for messages containing notifyRequest
|
|
|
|
|
cluster.autoFork();
|
2012-05-30 20:32:50 +02:00
|
|
|
|
Object.keys(cluster.workers).forEach(function(id) {
|
|
|
|
|
cluster.workers[id].on('message', messageHandler);
|
2011-12-20 10:42:48 +01:00
|
|
|
|
});
|
|
|
|
|
|
2011-11-04 23:33:49 +01:00
|
|
|
|
} else {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2011-11-04 23:33:49 +01:00
|
|
|
|
// Worker processes have a http server.
|
|
|
|
|
http.Server(function(req, res) {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end("hello world\n");
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
// notify master about the request
|
2011-11-04 23:33:49 +01:00
|
|
|
|
process.send({ cmd: 'notifyRequest' });
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
### Event: 'online'
|
|
|
|
|
|
2011-12-20 10:42:48 +01:00
|
|
|
|
Same as the `cluster.on('online')` event, but emits only when the state change
|
|
|
|
|
on the specified worker.
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.fork().on('online', function() {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
// Worker is online
|
|
|
|
|
};
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2012-02-27 20:07:49 +01:00
|
|
|
|
### Event: 'listening'
|
|
|
|
|
|
2012-04-28 14:24:17 +02:00
|
|
|
|
* `address` {Object}
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2011-12-20 10:42:48 +01:00
|
|
|
|
Same as the `cluster.on('listening')` event, but emits only when the state change
|
|
|
|
|
on the specified worker.
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.fork().on('listening', function(address) {
|
2011-12-20 10:42:48 +01:00
|
|
|
|
// Worker is listening
|
|
|
|
|
};
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2012-03-24 17:25:23 +01:00
|
|
|
|
### Event: 'disconnect'
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
|
|
|
|
Same as the `cluster.on('disconnect')` event, but emits only when the state change
|
|
|
|
|
on the specified worker.
|
|
|
|
|
|
2012-05-06 00:07:58 +02:00
|
|
|
|
cluster.fork().on('disconnect', function() {
|
2012-03-10 16:30:06 +01:00
|
|
|
|
// Worker has disconnected
|
|
|
|
|
};
|
|
|
|
|
|
2012-03-30 21:24:46 +02:00
|
|
|
|
### Event: 'exit'
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2012-05-30 20:32:50 +02:00
|
|
|
|
* `code` {Number} the exit code, if it exited normally.
|
2012-05-02 18:38:31 +02:00
|
|
|
|
* `signal` {String} the name of the signal (eg. `'SIGHUP'`) that caused
|
|
|
|
|
the process to be killed.
|
|
|
|
|
|
|
|
|
|
Emitted by the individual worker instance, when the underlying child process
|
2012-05-30 20:32:50 +02:00
|
|
|
|
is terminated. See [child_process event: 'exit'](child_process.html#child_process_event_exit).
|
2012-05-02 18:38:31 +02:00
|
|
|
|
|
|
|
|
|
var worker = cluster.fork();
|
2012-05-06 00:07:58 +02:00
|
|
|
|
worker.on('exit', function(code, signal) {
|
2012-05-02 18:38:31 +02:00
|
|
|
|
if( signal ) {
|
|
|
|
|
console.log("worker was killed by signal: "+signal);
|
|
|
|
|
} else if( code !== 0 ) {
|
|
|
|
|
console.log("worker exited with error code: "+code);
|
|
|
|
|
} else {
|
|
|
|
|
console.log("worker success!");
|
|
|
|
|
}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
};
|