2012-02-27 20:07:49 +01:00
|
|
|
|
# Cluster
|
|
|
|
|
|
2017-01-23 04:16:21 +01:00
|
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
|
> Stability: 2 - Stable
|
2011-10-12 23:19:32 +02:00
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
|
A single instance of Node.js runs in a single thread. To take advantage of
|
2017-10-14 17:13:26 +02:00
|
|
|
|
multi-core systems, the user will sometimes want to launch a cluster of Node.js
|
2011-10-12 23:19:32 +02:00
|
|
|
|
processes to handle the load.
|
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
The cluster module allows easy creation of child processes that all share
|
|
|
|
|
server ports.
|
2011-10-12 23:19:32 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const cluster = require('cluster');
|
|
|
|
|
const http = require('http');
|
|
|
|
|
const numCPUs = require('os').cpus().length;
|
|
|
|
|
|
|
|
|
|
if (cluster.isMaster) {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
console.log(`Master ${process.pid} is running`);
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// Fork workers.
|
2016-12-14 22:46:08 +01:00
|
|
|
|
for (let i = 0; i < numCPUs; i++) {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
cluster.fork();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
|
|
|
|
console.log(`worker ${worker.process.pid} died`);
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// Workers can share any TCP connection
|
|
|
|
|
// In this case it is an HTTP server
|
|
|
|
|
http.createServer((req, res) => {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end('hello world\n');
|
|
|
|
|
}).listen(8000);
|
2016-12-14 22:46:08 +01:00
|
|
|
|
|
|
|
|
|
console.log(`Worker ${process.pid} started`);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
}
|
|
|
|
|
```
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2015-08-13 18:14:34 +02:00
|
|
|
|
Running Node.js will now share port 8000 between the workers:
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2016-07-09 07:13:09 +02:00
|
|
|
|
```txt
|
2016-12-14 22:46:08 +01:00
|
|
|
|
$ node server.js
|
|
|
|
|
Master 3596 is running
|
|
|
|
|
Worker 4324 started
|
|
|
|
|
Worker 4520 started
|
|
|
|
|
Worker 6056 started
|
|
|
|
|
Worker 5644 started
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2016-09-16 03:24:50 +02:00
|
|
|
|
Please note that on Windows, it is not yet possible to set up a named pipe
|
2012-06-20 00:53:01 +02:00
|
|
|
|
server in a worker.
|
|
|
|
|
|
2012-06-12 19:02:52 +02:00
|
|
|
|
## How It Works
|
|
|
|
|
|
|
|
|
|
<!--type=misc-->
|
|
|
|
|
|
2016-02-04 03:33:39 +01:00
|
|
|
|
The worker processes are spawned using the [`child_process.fork()`][] method,
|
2012-06-12 19:02:52 +02:00
|
|
|
|
so that they can communicate with the parent via IPC and pass server
|
|
|
|
|
handles back and forth.
|
|
|
|
|
|
2013-04-23 15:11:17 +02:00
|
|
|
|
The cluster module supports two methods of distributing incoming
|
|
|
|
|
connections.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
2013-04-23 15:11:17 +02:00
|
|
|
|
The first one (and the default one on all platforms except Windows),
|
|
|
|
|
is the round-robin approach, where the master process listens on a
|
|
|
|
|
port, accepts new connections and distributes them across the workers
|
|
|
|
|
in a round-robin fashion, with some built-in smarts to avoid
|
|
|
|
|
overloading a worker process.
|
|
|
|
|
|
|
|
|
|
The second approach is where the master process creates the listen
|
|
|
|
|
socket and sends it to interested workers. The workers then accept
|
|
|
|
|
incoming connections directly.
|
|
|
|
|
|
|
|
|
|
The second approach should, in theory, give the best performance.
|
|
|
|
|
In practice however, distribution tends to be very unbalanced due
|
|
|
|
|
to operating system scheduler vagaries. Loads have been observed
|
|
|
|
|
where over 70% of all connections ended up in just two processes,
|
|
|
|
|
out of a total of eight.
|
|
|
|
|
|
|
|
|
|
Because `server.listen()` hands off most of the work to the master
|
|
|
|
|
process, there are three cases where the behavior between a normal
|
2015-08-13 18:14:34 +02:00
|
|
|
|
Node.js process and a cluster worker differs:
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
2012-06-19 20:07:59 +02:00
|
|
|
|
1. `server.listen({fd: 7})` Because the message is passed to the master,
|
2012-06-12 19:02:52 +02:00
|
|
|
|
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
|
2017-04-26 19:16:12 +02:00
|
|
|
|
process.
|
2012-09-02 09:36:21 +02:00
|
|
|
|
3. `server.listen(0)` Normally, this will cause servers to listen on a
|
2012-06-12 19:02:52 +02:00
|
|
|
|
random port. However, in a cluster, each worker will receive the
|
|
|
|
|
same "random" port each time they do `listen(0)`. In essence, the
|
2017-04-26 19:16:12 +02:00
|
|
|
|
port is random the first time, but predictable thereafter. To listen
|
|
|
|
|
on a unique port, generate a port number based on the cluster worker ID.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
2018-02-06 06:55:16 +01:00
|
|
|
|
Node.js does not provide routing logic. It is, therefore important to design an
|
|
|
|
|
application such that it does not rely too heavily on in-memory data objects for
|
|
|
|
|
things like sessions and login.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
|
|
|
|
Because workers are all separate processes, they can be killed or
|
2017-04-26 19:16:12 +02:00
|
|
|
|
re-spawned depending on a program's needs, without affecting other
|
2012-06-12 19:02:52 +02:00
|
|
|
|
workers. As long as there are some workers still alive, the server will
|
2015-08-28 23:26:07 +02:00
|
|
|
|
continue to accept connections. If no workers are alive, existing connections
|
2017-04-26 19:16:12 +02:00
|
|
|
|
will be dropped and new connections will be refused. Node.js does not
|
|
|
|
|
automatically manage the number of workers, however. It is the application's
|
|
|
|
|
responsibility to manage the worker pool based on its own needs.
|
2012-06-12 19:02:52 +02:00
|
|
|
|
|
2017-11-15 14:53:57 +01:00
|
|
|
|
Although a primary use case for the `cluster` module is networking, it can
|
|
|
|
|
also be used for other use cases requiring worker processes.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## Class: Worker
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
A Worker object contains all public information and method about a worker.
|
|
|
|
|
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
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### Event: 'disconnect'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.7
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Similar to the `cluster.on('disconnect')` event, but specific to this worker.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
cluster.fork().on('disconnect', () => {
|
|
|
|
|
// Worker has disconnected
|
|
|
|
|
});
|
|
|
|
|
```
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### Event: 'error'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.3
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
|
This event is the same as the one provided by [`child_process.fork()`][].
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
Within a worker, `process.on('error')` may also be used.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### Event: 'exit'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.2
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-06-18 21:53:54 +02:00
|
|
|
|
* `code` {number} The exit code, if it exited normally.
|
|
|
|
|
* `signal` {string} The name of the signal (e.g. `'SIGHUP'`) that caused
|
2015-11-04 16:51:00 +01:00
|
|
|
|
the process to be killed.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Similar to the `cluster.on('exit')` event, but specific to this worker.
|
2012-04-28 14:24:17 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const worker = cluster.fork();
|
|
|
|
|
worker.on('exit', (code, signal) => {
|
2016-05-02 10:36:11 +02:00
|
|
|
|
if (signal) {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
console.log(`worker was killed by signal: ${signal}`);
|
2016-05-02 10:36:11 +02:00
|
|
|
|
} else if (code !== 0) {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
console.log(`worker exited with error code: ${code}`);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('worker success!');
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
```
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### Event: 'listening'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* `address` {Object}
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Similar to the `cluster.on('listening')` event, but specific to this worker.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
cluster.fork().on('listening', (address) => {
|
|
|
|
|
// Worker is listening
|
|
|
|
|
});
|
|
|
|
|
```
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
It is not emitted in the worker.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### Event: 'message'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* `message` {Object}
|
2016-06-15 16:08:53 +02:00
|
|
|
|
* `handle` {undefined|Object}
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
Similar to the `cluster.on('message')` event, but specific to this worker.
|
|
|
|
|
|
2017-07-18 21:02:47 +02:00
|
|
|
|
Within a worker, `process.on('message')` may also be used.
|
2015-02-15 03:50:56 +01:00
|
|
|
|
|
2016-06-15 16:08:53 +02:00
|
|
|
|
See [`process` event: `'message'`][].
|
2015-02-15 03:50:56 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
As an example, here is a cluster that keeps count of the number of requests
|
|
|
|
|
in the master process using the message system:
|
2015-02-15 03:50:56 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const cluster = require('cluster');
|
|
|
|
|
const http = require('http');
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
if (cluster.isMaster) {
|
2014-07-10 18:13:27 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// Keep track of http requests
|
2016-12-14 22:46:08 +01:00
|
|
|
|
let numReqs = 0;
|
2016-01-17 18:39:07 +01:00
|
|
|
|
setInterval(() => {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
console.log(`numReqs = ${numReqs}`);
|
2016-01-17 18:39:07 +01:00
|
|
|
|
}, 1000);
|
2014-07-10 18:13:27 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// Count requests
|
|
|
|
|
function messageHandler(msg) {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
if (msg.cmd && msg.cmd === 'notifyRequest') {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
numReqs += 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-10 18:13:27 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// Start workers and listen for messages containing notifyRequest
|
|
|
|
|
const numCPUs = require('os').cpus().length;
|
2016-12-14 22:46:08 +01:00
|
|
|
|
for (let i = 0; i < numCPUs; i++) {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
cluster.fork();
|
|
|
|
|
}
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2016-12-14 22:46:08 +01:00
|
|
|
|
for (const id in cluster.workers) {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
cluster.workers[id].on('message', messageHandler);
|
2016-12-14 22:46:08 +01:00
|
|
|
|
}
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
} else {
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// Worker processes have a http server.
|
|
|
|
|
http.Server((req, res) => {
|
|
|
|
|
res.writeHead(200);
|
|
|
|
|
res.end('hello world\n');
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// notify master about the request
|
|
|
|
|
process.send({ cmd: 'notifyRequest' });
|
|
|
|
|
}).listen(8000);
|
|
|
|
|
}
|
|
|
|
|
```
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### Event: 'online'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Similar to the `cluster.on('online')` event, but specific to this worker.
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
cluster.fork().on('online', () => {
|
|
|
|
|
// Worker is online
|
|
|
|
|
});
|
|
|
|
|
```
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
It is not emitted in the worker.
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### worker.disconnect()
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.7
|
2017-02-21 23:38:43 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v7.3.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/10019
|
|
|
|
|
description: This method now returns a reference to `worker`.
|
2016-07-09 22:14:17 +02:00
|
|
|
|
-->
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2018-01-29 23:15:53 +01:00
|
|
|
|
* Returns: {cluster.Worker} A reference to `worker`.
|
2016-12-01 18:30:34 +01:00
|
|
|
|
|
2018-02-12 08:31:55 +01:00
|
|
|
|
In a worker, this function will close all servers, wait for the `'close'` event
|
|
|
|
|
on those servers, and then disconnect the IPC channel.
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
In the master, an internal message is sent to the worker causing it to call
|
|
|
|
|
`.disconnect()` on itself.
|
2012-01-05 20:09:43 +01:00
|
|
|
|
|
2015-11-10 20:14:34 +01:00
|
|
|
|
Causes `.exitedAfterDisconnect` to be set.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Note that after a server is closed, it will no longer accept new connections,
|
|
|
|
|
but connections may be accepted by any other listening worker. Existing
|
|
|
|
|
connections will be allowed to close as usual. When no more connections exist,
|
2018-02-12 08:31:55 +01:00
|
|
|
|
see [`server.close()`][], the IPC channel to the worker will close allowing it
|
|
|
|
|
to die gracefully.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
The above applies *only* to server connections, client connections are not
|
|
|
|
|
automatically closed by workers, and disconnect does not wait for them to close
|
|
|
|
|
before exiting.
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Note that in a worker, `process.disconnect` exists, but it is not this function,
|
2015-11-28 00:30:32 +01:00
|
|
|
|
it is [`disconnect`][].
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Because long living server connections may block workers from disconnecting, it
|
|
|
|
|
may be useful to send a message, so application specific actions may be taken to
|
|
|
|
|
close them. It also may be useful to implement a timeout, killing a worker if
|
2015-11-28 00:30:32 +01:00
|
|
|
|
the `'disconnect'` event has not been emitted after some time.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
if (cluster.isMaster) {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
const worker = cluster.fork();
|
|
|
|
|
let timeout;
|
2016-01-17 18:39:07 +01:00
|
|
|
|
|
|
|
|
|
worker.on('listening', (address) => {
|
|
|
|
|
worker.send('shutdown');
|
|
|
|
|
worker.disconnect();
|
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
|
worker.kill();
|
|
|
|
|
}, 2000);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
worker.on('disconnect', () => {
|
|
|
|
|
clearTimeout(timeout);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
} else if (cluster.isWorker) {
|
|
|
|
|
const net = require('net');
|
2016-12-14 22:46:08 +01:00
|
|
|
|
const server = net.createServer((socket) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// connections never end
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
server.listen(8000);
|
|
|
|
|
|
|
|
|
|
process.on('message', (msg) => {
|
2016-05-02 10:36:11 +02:00
|
|
|
|
if (msg === 'shutdown') {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
// initiate graceful close of any connections to server
|
2012-08-17 22:57:23 +02:00
|
|
|
|
}
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
```
|
2012-08-17 22:57:23 +02:00
|
|
|
|
|
2015-11-10 20:14:34 +01:00
|
|
|
|
### worker.exitedAfterDisconnect
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v6.0.0
|
|
|
|
|
-->
|
2015-11-10 20:14:34 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* {boolean}
|
2015-11-10 20:14:34 +01:00
|
|
|
|
|
|
|
|
|
Set by calling `.kill()` or `.disconnect()`. Until then, it is `undefined`.
|
|
|
|
|
|
2017-06-28 20:55:48 +02:00
|
|
|
|
The boolean [`worker.exitedAfterDisconnect`][] allows distinguishing between
|
2017-04-26 19:16:12 +02:00
|
|
|
|
voluntary and accidental exit, the master may choose not to respawn a worker
|
|
|
|
|
based on this value.
|
2015-11-10 20:14:34 +01:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
|
|
|
|
if (worker.exitedAfterDisconnect === true) {
|
2016-04-29 05:39:53 +02:00
|
|
|
|
console.log('Oh, it was just voluntary – no need to worry');
|
2015-11-10 20:14:34 +01:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// kill worker
|
|
|
|
|
worker.kill();
|
|
|
|
|
```
|
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### worker.id
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.8.0
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* {number}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Each new worker is given its own unique id, this id is stored in the
|
|
|
|
|
`id`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
While a worker is alive, this is the key that indexes it in
|
|
|
|
|
cluster.workers
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### worker.isConnected()
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.14
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
This function returns `true` if the worker is connected to its master via its
|
|
|
|
|
IPC channel, `false` otherwise. A worker is connected to its master after it
|
|
|
|
|
has been created. It is disconnected after the `'disconnect'` event is emitted.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### worker.isDead()
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.14
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
This function returns `true` if the worker's process has terminated (either
|
|
|
|
|
because of exiting or being signaled). Otherwise, it returns `false`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
### worker.kill([signal='SIGTERM'])
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.9.12
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `signal` {string} Name of the kill signal to send to the worker
|
2015-11-04 16:51:00 +01:00
|
|
|
|
process.
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
This function will kill the worker. In the master, it does this by disconnecting
|
|
|
|
|
the `worker.process`, and once disconnected, killing with `signal`. In the
|
|
|
|
|
worker, it does it by disconnecting the channel, and then exiting with code `0`.
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-10 20:14:34 +01:00
|
|
|
|
Causes `.exitedAfterDisconnect` to be set.
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
This method is aliased as `worker.destroy()` for backwards compatibility.
|
|
|
|
|
|
|
|
|
|
Note that in a worker, `process.kill()` exists, but it is not this function,
|
2015-11-28 00:30:32 +01:00
|
|
|
|
it is [`kill`][].
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
|
|
|
|
### worker.process
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* {ChildProcess}
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
|
All workers are created using [`child_process.fork()`][], the returned object
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
from this function is stored as `.process`. In a worker, the global `process`
|
|
|
|
|
is stored.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
|
See: [Child Process module][]
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
|
|
|
|
Note that workers will call `process.exit(0)` if the `'disconnect'` event occurs
|
2015-11-10 20:14:34 +01:00
|
|
|
|
on `process` and `.exitedAfterDisconnect` is not `true`. This protects against
|
|
|
|
|
accidental disconnection.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-08-31 00:49:34 +02:00
|
|
|
|
### worker.send(message[, sendHandle][, callback])
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
2017-02-21 23:38:43 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v4.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/2620
|
|
|
|
|
description: The `callback` parameter is supported now.
|
2016-07-09 22:14:17 +02:00
|
|
|
|
-->
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
|
|
|
|
* `message` {Object}
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `sendHandle` {Handle}
|
2015-08-31 00:49:34 +02:00
|
|
|
|
* `callback` {Function}
|
2017-06-18 21:53:54 +02:00
|
|
|
|
* Returns: {boolean}
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-02-13 22:41:00 +01:00
|
|
|
|
Send a message to a worker or master, optionally with a handle.
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-02-13 22:41:00 +01:00
|
|
|
|
In the master this sends a message to a specific worker. It is identical to
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`ChildProcess.send()`][].
|
2015-02-13 22:41:00 +01:00
|
|
|
|
|
|
|
|
|
In a worker this sends a message to the master. It is identical to
|
|
|
|
|
`process.send()`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
|
|
|
|
This example will echo back all messages from the master:
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
if (cluster.isMaster) {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
const worker = cluster.fork();
|
2016-01-17 18:39:07 +01:00
|
|
|
|
worker.send('hi there');
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
} else if (cluster.isWorker) {
|
|
|
|
|
process.on('message', (msg) => {
|
|
|
|
|
process.send(msg);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
```
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## Event: 'disconnect'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.9
|
|
|
|
|
-->
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `worker` {cluster.Worker}
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
|
|
|
|
Emitted after the worker IPC channel has disconnected. This can occur when a
|
|
|
|
|
worker exits gracefully, is killed, or is disconnected manually (such as with
|
|
|
|
|
worker.disconnect()).
|
|
|
|
|
|
2018-02-12 08:31:55 +01:00
|
|
|
|
There may be a delay between the `'disconnect'` and `'exit'` events. These
|
|
|
|
|
events can be used to detect if the process is stuck in a cleanup or if there
|
|
|
|
|
are long-living connections.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
cluster.on('disconnect', (worker) => {
|
|
|
|
|
console.log(`The worker #${worker.id} has disconnected`);
|
|
|
|
|
});
|
|
|
|
|
```
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
|
|
|
|
## Event: 'exit'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.9
|
|
|
|
|
-->
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `worker` {cluster.Worker}
|
2017-06-18 21:53:54 +02:00
|
|
|
|
* `code` {number} The exit code, if it exited normally.
|
|
|
|
|
* `signal` {string} The name of the signal (e.g. `'SIGHUP'`) that caused
|
2015-11-04 16:51:00 +01:00
|
|
|
|
the process to be killed.
|
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
|
When any of the workers die the cluster module will emit the `'exit'` event.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
|
|
|
|
This can be used to restart the worker by calling `.fork()` again.
|
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2017-04-21 21:55:51 +02:00
|
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
|
|
|
|
console.log('worker %d died (%s). restarting...',
|
|
|
|
|
worker.process.pid, signal || code);
|
|
|
|
|
cluster.fork();
|
|
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
|
See [child_process event: 'exit'][].
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
|
|
|
|
## Event: 'fork'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `worker` {cluster.Worker}
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2015-11-28 00:30:32 +01:00
|
|
|
|
When a new worker is forked the cluster module will emit a `'fork'` event.
|
2017-04-26 19:16:12 +02:00
|
|
|
|
This can be used to log worker activity, and create a custom timeout.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
2016-12-14 22:46:08 +01:00
|
|
|
|
const timeouts = [];
|
2016-01-17 18:39:07 +01:00
|
|
|
|
function errorMsg() {
|
|
|
|
|
console.error('Something must be wrong with the connection ...');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cluster.on('fork', (worker) => {
|
|
|
|
|
timeouts[worker.id] = setTimeout(errorMsg, 2000);
|
|
|
|
|
});
|
|
|
|
|
cluster.on('listening', (worker, address) => {
|
|
|
|
|
clearTimeout(timeouts[worker.id]);
|
|
|
|
|
});
|
|
|
|
|
cluster.on('exit', (worker, code, signal) => {
|
|
|
|
|
clearTimeout(timeouts[worker.id]);
|
|
|
|
|
errorMsg();
|
|
|
|
|
});
|
|
|
|
|
```
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
|
|
|
|
## Event: 'listening'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `worker` {cluster.Worker}
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* `address` {Object}
|
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
After calling `listen()` from a worker, when the `'listening'` event is emitted
|
|
|
|
|
on the server a `'listening'` event will also be emitted on `cluster` in the
|
|
|
|
|
master.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2017-04-26 19:16:12 +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.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
cluster.on('listening', (worker, address) => {
|
|
|
|
|
console.log(
|
|
|
|
|
`A worker is now connected to ${address.address}:${address.port}`);
|
|
|
|
|
});
|
|
|
|
|
```
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
|
|
|
|
The `addressType` is one of:
|
|
|
|
|
|
|
|
|
|
* `4` (TCPv4)
|
|
|
|
|
* `6` (TCPv6)
|
|
|
|
|
* `-1` (unix domain socket)
|
|
|
|
|
* `"udp4"` or `"udp6"` (UDP v4 or v6)
|
|
|
|
|
|
|
|
|
|
## Event: 'message'
|
2017-02-21 23:38:43 +01:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v2.5.0
|
|
|
|
|
changes:
|
|
|
|
|
- version: v6.0.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/5361
|
|
|
|
|
description: The `worker` parameter is passed now; see below for details.
|
|
|
|
|
-->
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `worker` {cluster.Worker}
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* `message` {Object}
|
2016-02-22 12:30:33 +01:00
|
|
|
|
* `handle` {undefined|Object}
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2016-08-08 19:15:26 +02:00
|
|
|
|
Emitted when the cluster master receives a message from any worker.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
|
2015-11-14 04:21:49 +01:00
|
|
|
|
See [child_process event: 'message'][].
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2016-02-22 12:30:33 +01:00
|
|
|
|
Before Node.js v6.0, this event emitted only the message and the handle,
|
|
|
|
|
but not the worker object, contrary to what the documentation stated.
|
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
If support for older versions is required but a worker object is not
|
|
|
|
|
required, it is possible to work around the discrepancy by checking the
|
|
|
|
|
number of arguments:
|
2016-02-22 12:30:33 +01:00
|
|
|
|
|
|
|
|
|
```js
|
2016-12-14 22:46:08 +01:00
|
|
|
|
cluster.on('message', (worker, message, handle) => {
|
2016-02-22 12:30:33 +01:00
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
|
handle = message;
|
|
|
|
|
message = worker;
|
|
|
|
|
worker = undefined;
|
|
|
|
|
}
|
|
|
|
|
// ...
|
|
|
|
|
});
|
|
|
|
|
```
|
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## Event: 'online'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2016-01-19 17:03:15 +01:00
|
|
|
|
* `worker` {cluster.Worker}
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
After forking a new worker, the worker should respond with an online message.
|
|
|
|
|
When the master receives an online message it will emit this event.
|
2015-11-28 00:30:32 +01:00
|
|
|
|
The difference between `'fork'` and `'online'` is that fork is emitted when the
|
2015-11-04 16:51:00 +01:00
|
|
|
|
master forks a worker, and 'online' is emitted when the worker is running.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
cluster.on('online', (worker) => {
|
|
|
|
|
console.log('Yay, the worker responded after it was forked');
|
|
|
|
|
});
|
|
|
|
|
```
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## Event: 'setup'
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.1
|
|
|
|
|
-->
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* `settings` {Object}
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Emitted every time `.setupMaster()` is called.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
The `settings` object is the `cluster.settings` object at the time
|
|
|
|
|
`.setupMaster()` was called and is advisory only, since multiple calls to
|
|
|
|
|
`.setupMaster()` can be made in a single tick.
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
If accuracy is important, use `cluster.settings`.
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.disconnect([callback])
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.7
|
|
|
|
|
-->
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2017-06-18 21:53:54 +02:00
|
|
|
|
* `callback` {Function} Called when all workers are disconnected and handles are
|
|
|
|
|
closed.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Calls `.disconnect()` on each worker in `cluster.workers`.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
When they are disconnected all internal handles will be closed, allowing the
|
|
|
|
|
master process to die gracefully if no other event is waiting.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2018-02-12 08:31:55 +01:00
|
|
|
|
The method takes an optional callback argument which will be called when
|
|
|
|
|
finished.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
This can only be called from the master process.
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.fork([env])
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.6.0
|
|
|
|
|
-->
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* `env` {Object} Key/value pairs to add to worker process environment.
|
2017-06-18 21:53:54 +02:00
|
|
|
|
* Returns: {cluster.Worker}
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Spawn a new worker process.
|
2014-08-16 02:20:32 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
This can only be called from the master process.
|
2014-08-16 02:20:32 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.isMaster
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.8.1
|
|
|
|
|
-->
|
2014-08-16 02:20:32 +02:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* {boolean}
|
2014-08-16 02:20:32 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
True if the process is a master. This is determined
|
|
|
|
|
by the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is
|
|
|
|
|
undefined, then `isMaster` is `true`.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.isWorker
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.6.0
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-03-05 18:03:39 +01:00
|
|
|
|
* {boolean}
|
2015-02-15 03:50:56 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
True if the process is not a master (it is the negation of `cluster.isMaster`).
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.schedulingPolicy
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.11.2
|
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
The scheduling policy, either `cluster.SCHED_RR` for round-robin or
|
|
|
|
|
`cluster.SCHED_NONE` to leave it to the operating system. This is a
|
2017-04-26 19:16:12 +02:00
|
|
|
|
global setting and effectively frozen once either the first worker is spawned,
|
|
|
|
|
or `cluster.setupMaster()` is called, whichever comes first.
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
`SCHED_RR` is the default on all operating systems except Windows.
|
|
|
|
|
Windows will change to `SCHED_RR` once libuv is able to effectively
|
|
|
|
|
distribute IOCP handles without incurring a large performance hit.
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
`cluster.schedulingPolicy` can also be set through the
|
|
|
|
|
`NODE_CLUSTER_SCHED_POLICY` environment variable. Valid
|
|
|
|
|
values are `"rr"` and `"none"`.
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.settings
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.1
|
2017-02-21 23:38:43 +01:00
|
|
|
|
changes:
|
2018-03-24 11:45:19 +01:00
|
|
|
|
- version: v9.5.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/18399
|
|
|
|
|
description: The `cwd` option is supported now.
|
2018-03-24 18:03:05 +01:00
|
|
|
|
- version: v9.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/17412
|
|
|
|
|
description: The `windowsHide` option is supported now.
|
2017-07-19 17:40:29 +02:00
|
|
|
|
- version: 8.2.0
|
2017-07-18 20:12:19 +02:00
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/14140
|
|
|
|
|
description: The `inspectPort` option is supported now.
|
2017-02-21 23:38:43 +01:00
|
|
|
|
- version: v6.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7838
|
|
|
|
|
description: The `stdio` option is supported now.
|
2016-07-09 22:14:17 +02:00
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* {Object}
|
2017-07-10 21:19:57 +02:00
|
|
|
|
* `execArgv` {Array} List of string arguments passed to the Node.js
|
2017-10-27 21:51:28 +02:00
|
|
|
|
executable. **Default:** `process.execArgv`
|
|
|
|
|
* `exec` {string} File path to worker file. **Default:** `process.argv[1]`
|
2017-07-10 21:19:57 +02:00
|
|
|
|
* `args` {Array} String arguments passed to worker.
|
2017-06-18 21:53:54 +02:00
|
|
|
|
**Default:** `process.argv.slice(2)`
|
2018-01-26 20:16:20 +01:00
|
|
|
|
* `cwd` {string} Current working directory of the worker process. **Default:**
|
|
|
|
|
`undefined` (inherits from parent process)
|
2017-07-10 21:19:57 +02:00
|
|
|
|
* `silent` {boolean} Whether or not to send output to parent's stdio.
|
2017-06-18 21:53:54 +02:00
|
|
|
|
**Default:** `false`
|
2016-07-22 18:05:27 +02:00
|
|
|
|
* `stdio` {Array} Configures the stdio of forked processes. Because the
|
|
|
|
|
cluster module relies on IPC to function, this configuration must contain an
|
|
|
|
|
`'ipc'` entry. When this option is provided, it overrides `silent`.
|
2017-02-04 16:15:33 +01:00
|
|
|
|
* `uid` {number} Sets the user identity of the process. (See setuid(2).)
|
|
|
|
|
* `gid` {number} Sets the group identity of the process. (See setgid(2).)
|
2018-01-12 01:07:18 +01:00
|
|
|
|
* `inspectPort` {number|Function} Sets inspector port of worker.
|
2017-07-18 20:12:19 +02:00
|
|
|
|
This can be a number, or a function that takes no arguments and returns a
|
|
|
|
|
number. By default each worker gets its own port, incremented from the
|
|
|
|
|
master's `process.debugPort`.
|
2017-12-01 17:08:01 +01:00
|
|
|
|
* `windowsHide` {boolean} Hide the forked processes console window that would
|
|
|
|
|
normally be created on Windows systems. **Default:** `false`
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
After calling `.setupMaster()` (or `.fork()`) this settings object will contain
|
|
|
|
|
the settings, including the default values.
|
2012-06-25 18:53:35 +02:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
This object is not intended to be changed or set manually.
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.setupMaster([settings])
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.1
|
2017-02-21 23:38:43 +01:00
|
|
|
|
changes:
|
|
|
|
|
- version: v6.4.0
|
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/7838
|
|
|
|
|
description: The `stdio` option is supported now.
|
2016-07-09 22:14:17 +02:00
|
|
|
|
-->
|
2011-12-20 10:42:48 +01:00
|
|
|
|
|
2017-07-10 21:19:57 +02:00
|
|
|
|
* `settings` {Object} see [`cluster.settings`][]
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
`setupMaster` is used to change the default 'fork' behavior. Once called,
|
|
|
|
|
the settings will be present in `cluster.settings`.
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Note that:
|
2011-11-04 23:33:49 +01:00
|
|
|
|
|
2017-07-10 21:19:57 +02:00
|
|
|
|
* Any settings changes only affect future calls to `.fork()` and have no
|
|
|
|
|
effect on workers that are already running.
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* The *only* attribute of a worker that cannot be set via `.setupMaster()` is
|
2017-07-10 21:19:57 +02:00
|
|
|
|
the `env` passed to `.fork()`.
|
|
|
|
|
* The defaults above apply to the first call only, the defaults for later
|
|
|
|
|
calls is the current value at the time of `cluster.setupMaster()` is called.
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
Example:
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const cluster = require('cluster');
|
|
|
|
|
cluster.setupMaster({
|
|
|
|
|
exec: 'worker.js',
|
|
|
|
|
args: ['--use', 'https'],
|
|
|
|
|
silent: true
|
|
|
|
|
});
|
|
|
|
|
cluster.fork(); // https worker
|
|
|
|
|
cluster.setupMaster({
|
2016-02-18 18:56:05 +01:00
|
|
|
|
exec: 'worker.js',
|
2016-01-17 18:39:07 +01:00
|
|
|
|
args: ['--use', 'http']
|
|
|
|
|
});
|
|
|
|
|
cluster.fork(); // http worker
|
|
|
|
|
```
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
This can only be called from the master process.
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.worker
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* {Object}
|
2011-10-27 01:21:08 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
A reference to the current worker object. Not available in the master process.
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
const cluster = require('cluster');
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
if (cluster.isMaster) {
|
|
|
|
|
console.log('I am master');
|
|
|
|
|
cluster.fork();
|
|
|
|
|
cluster.fork();
|
|
|
|
|
} else if (cluster.isWorker) {
|
|
|
|
|
console.log(`I am worker #${cluster.worker.id}`);
|
|
|
|
|
}
|
|
|
|
|
```
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
## cluster.workers
|
2016-07-09 22:14:17 +02:00
|
|
|
|
<!-- YAML
|
|
|
|
|
added: v0.7.0
|
|
|
|
|
-->
|
2012-03-10 16:30:06 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
* {Object}
|
2012-02-27 20:07:49 +01:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
A hash that stores the active worker objects, keyed by `id` field. Makes it
|
|
|
|
|
easy to loop through all the workers. It is only available in the master
|
|
|
|
|
process.
|
2012-05-02 18:38:31 +02:00
|
|
|
|
|
2015-11-04 16:51:00 +01:00
|
|
|
|
A worker is removed from cluster.workers after the worker has disconnected _and_
|
|
|
|
|
exited. The order between these two events cannot be determined in advance.
|
|
|
|
|
However, it is guaranteed that the removal from the cluster.workers list happens
|
|
|
|
|
before last `'disconnect'` or `'exit'` event is emitted.
|
2012-05-02 18:38:31 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
// Go through all workers
|
|
|
|
|
function eachWorker(callback) {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
for (const id in cluster.workers) {
|
2016-01-17 18:39:07 +01:00
|
|
|
|
callback(cluster.workers[id]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
eachWorker((worker) => {
|
|
|
|
|
worker.send('big announcement to all workers');
|
|
|
|
|
});
|
|
|
|
|
```
|
doc: cluster documentation cleanup and corrections
- fixed some incomprehensible wording ("event assigned to..."?)
- removed undocumented and unnecessary process properties from example
- corrected the docs on the default for the exec setting
- described when workers are removed from cluster.workers
- described addressType, which was documented as existing, but not what
values it might have
- spell out more clearly the limitations of setupMaster
- describe disconnect in sufficient detail that why a child does or does
not exit can be understood
- clarify which cluster functions and events are available on process or
just on the worker, as well as which are not available in children,
- don't describe events as the same, when they have receive different
arguments
- fix misleading disconnect example: since disconnect already calls
close on all servers, doing it again in the example is a no-op, not
the "force close" it was claimed to be
- document the error event, not catching it will kill your node
- describe suicide better, it is important, and a bit unintuitive
(process.exit() is not suicide?)
- use worker consistently throughout, instead of child.
2013-07-26 03:17:38 +02:00
|
|
|
|
|
2017-04-26 19:16:12 +02:00
|
|
|
|
Using the worker's unique id is the easiest way to locate the worker.
|
2015-06-15 04:06:22 +02:00
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
|
```js
|
|
|
|
|
socket.on('data', (id) => {
|
2016-12-14 22:46:08 +01:00
|
|
|
|
const worker = cluster.workers[id];
|
2016-01-17 18:39:07 +01:00
|
|
|
|
});
|
|
|
|
|
```
|
2015-11-14 04:21:49 +01:00
|
|
|
|
|
2017-09-09 13:09:28 +02:00
|
|
|
|
[`ChildProcess.send()`]: child_process.html#child_process_subprocess_send_message_sendhandle_options_callback
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`child_process.fork()`]: child_process.html#child_process_child_process_fork_modulepath_args_options
|
2017-09-09 13:09:28 +02:00
|
|
|
|
[`disconnect`]: child_process.html#child_process_subprocess_disconnect
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`kill`]: process.html#process_process_kill_pid_signal
|
2017-05-08 18:30:13 +02:00
|
|
|
|
[`process` event: `'message'`]: process.html#process_event_message
|
2015-11-28 00:30:32 +01:00
|
|
|
|
[`server.close()`]: net.html#net_event_close
|
2015-11-10 20:14:34 +01:00
|
|
|
|
[`worker.exitedAfterDisconnect`]: #cluster_worker_exitedafterdisconnect
|
2015-11-14 04:21:49 +01:00
|
|
|
|
[Child Process module]: child_process.html#child_process_child_process_fork_modulepath_args_options
|
|
|
|
|
[child_process event: 'exit']: child_process.html#child_process_event_exit
|
2015-11-20 20:28:11 +01:00
|
|
|
|
[child_process event: 'message']: child_process.html#child_process_event_message
|
2017-09-09 13:09:28 +02:00
|
|
|
|
[`cluster.settings`]: #cluster_cluster_settings
|