|
@ -108,12 +108,18 @@ responsibility to manage the worker pool for your application's needs. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Class: Worker |
|
|
## Class: Worker |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
A Worker object contains all public information and method about a worker. |
|
|
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 |
|
|
In the master it can be obtained using `cluster.workers`. In a worker |
|
|
it can be obtained using `cluster.worker`. |
|
|
it can be obtained using `cluster.worker`. |
|
|
|
|
|
|
|
|
### Event: 'disconnect' |
|
|
### Event: 'disconnect' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.7 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
Similar to the `cluster.on('disconnect')` event, but specific to this worker. |
|
|
Similar to the `cluster.on('disconnect')` event, but specific to this worker. |
|
|
|
|
|
|
|
@ -124,12 +130,18 @@ cluster.fork().on('disconnect', () => { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Event: 'error' |
|
|
### Event: 'error' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.3 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
This event is the same as the one provided by [`child_process.fork()`][]. |
|
|
This event is the same as the one provided by [`child_process.fork()`][]. |
|
|
|
|
|
|
|
|
In a worker you can also use `process.on('error')`. |
|
|
In a worker you can also use `process.on('error')`. |
|
|
|
|
|
|
|
|
### Event: 'exit' |
|
|
### Event: 'exit' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.11.2 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `code` {Number} the exit code, if it exited normally. |
|
|
* `code` {Number} the exit code, if it exited normally. |
|
|
* `signal` {String} the name of the signal (eg. `'SIGHUP'`) that caused |
|
|
* `signal` {String} the name of the signal (eg. `'SIGHUP'`) that caused |
|
@ -151,6 +163,9 @@ worker.on('exit', (code, signal) => { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Event: 'listening' |
|
|
### Event: 'listening' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `address` {Object} |
|
|
* `address` {Object} |
|
|
|
|
|
|
|
@ -165,6 +180,9 @@ cluster.fork().on('listening', (address) => { |
|
|
It is not emitted in the worker. |
|
|
It is not emitted in the worker. |
|
|
|
|
|
|
|
|
### Event: 'message' |
|
|
### Event: 'message' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `message` {Object} |
|
|
* `message` {Object} |
|
|
* `handle` {undefined|Object} |
|
|
* `handle` {undefined|Object} |
|
@ -220,6 +238,9 @@ if (cluster.isMaster) { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### Event: 'online' |
|
|
### Event: 'online' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
Similar to the `cluster.on('online')` event, but specific to this worker. |
|
|
Similar to the `cluster.on('online')` event, but specific to this worker. |
|
|
|
|
|
|
|
@ -232,6 +253,9 @@ cluster.fork().on('online', () => { |
|
|
It is not emitted in the worker. |
|
|
It is not emitted in the worker. |
|
|
|
|
|
|
|
|
### worker.disconnect() |
|
|
### worker.disconnect() |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.7 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
In a worker, this function will close all servers, wait for the `'close'` event on |
|
|
In a worker, this function will close all servers, wait for the `'close'` event on |
|
|
those servers, and then disconnect the IPC channel. |
|
|
those servers, and then disconnect the IPC channel. |
|
@ -293,6 +317,9 @@ if (cluster.isMaster) { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### worker.exitedAfterDisconnect |
|
|
### worker.exitedAfterDisconnect |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v6.0.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Boolean} |
|
|
* {Boolean} |
|
|
|
|
|
|
|
@ -314,6 +341,9 @@ worker.kill(); |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### worker.id |
|
|
### worker.id |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.8.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Number} |
|
|
* {Number} |
|
|
|
|
|
|
|
@ -324,17 +354,26 @@ While a worker is alive, this is the key that indexes it in |
|
|
cluster.workers |
|
|
cluster.workers |
|
|
|
|
|
|
|
|
### worker.isConnected() |
|
|
### worker.isConnected() |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.11.14 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
This function returns `true` if the worker is connected to its master via its IPC |
|
|
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's been |
|
|
channel, `false` otherwise. A worker is connected to its master after it's been |
|
|
created. It is disconnected after the `'disconnect'` event is emitted. |
|
|
created. It is disconnected after the `'disconnect'` event is emitted. |
|
|
|
|
|
|
|
|
### worker.isDead() |
|
|
### worker.isDead() |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.11.14 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
This function returns `true` if the worker's process has terminated (either |
|
|
This function returns `true` if the worker's process has terminated (either |
|
|
because of exiting or being signaled). Otherwise, it returns `false`. |
|
|
because of exiting or being signaled). Otherwise, it returns `false`. |
|
|
|
|
|
|
|
|
### worker.kill([signal='SIGTERM']) |
|
|
### worker.kill([signal='SIGTERM']) |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.9.12 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `signal` {String} Name of the kill signal to send to the worker |
|
|
* `signal` {String} Name of the kill signal to send to the worker |
|
|
process. |
|
|
process. |
|
@ -351,6 +390,9 @@ Note that in a worker, `process.kill()` exists, but it is not this function, |
|
|
it is [`kill`][]. |
|
|
it is [`kill`][]. |
|
|
|
|
|
|
|
|
### worker.process |
|
|
### worker.process |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {ChildProcess} |
|
|
* {ChildProcess} |
|
|
|
|
|
|
|
@ -365,6 +407,9 @@ on `process` and `.exitedAfterDisconnect` is not `true`. This protects against |
|
|
accidental disconnection. |
|
|
accidental disconnection. |
|
|
|
|
|
|
|
|
### worker.send(message[, sendHandle][, callback]) |
|
|
### worker.send(message[, sendHandle][, callback]) |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `message` {Object} |
|
|
* `message` {Object} |
|
|
* `sendHandle` {Handle} |
|
|
* `sendHandle` {Handle} |
|
@ -394,6 +439,10 @@ if (cluster.isMaster) { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
### worker.suicide |
|
|
### worker.suicide |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
deprecated: v6.0.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
> Stability: 0 - Deprecated: Use [`worker.exitedAfterDisconnect`][] instead. |
|
|
> Stability: 0 - Deprecated: Use [`worker.exitedAfterDisconnect`][] instead. |
|
|
|
|
|
|
|
@ -420,6 +469,9 @@ This API only exists for backwards compatibility and will be removed in the |
|
|
future. |
|
|
future. |
|
|
|
|
|
|
|
|
## Event: 'disconnect' |
|
|
## Event: 'disconnect' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.9 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `worker` {cluster.Worker} |
|
|
* `worker` {cluster.Worker} |
|
|
|
|
|
|
|
@ -438,6 +490,9 @@ cluster.on('disconnect', (worker) => { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## Event: 'exit' |
|
|
## Event: 'exit' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.9 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `worker` {cluster.Worker} |
|
|
* `worker` {cluster.Worker} |
|
|
* `code` {Number} the exit code, if it exited normally. |
|
|
* `code` {Number} the exit code, if it exited normally. |
|
@ -459,6 +514,9 @@ cluster.on('exit', (worker, code, signal) => { |
|
|
See [child_process event: 'exit'][]. |
|
|
See [child_process event: 'exit'][]. |
|
|
|
|
|
|
|
|
## Event: 'fork' |
|
|
## Event: 'fork' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `worker` {cluster.Worker} |
|
|
* `worker` {cluster.Worker} |
|
|
|
|
|
|
|
@ -484,6 +542,9 @@ cluster.on('exit', (worker, code, signal) => { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## Event: 'listening' |
|
|
## Event: 'listening' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `worker` {cluster.Worker} |
|
|
* `worker` {cluster.Worker} |
|
|
* `address` {Object} |
|
|
* `address` {Object} |
|
@ -538,6 +599,9 @@ cluster.on('message', function(worker, message, handle) { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## Event: 'online' |
|
|
## Event: 'online' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `worker` {cluster.Worker} |
|
|
* `worker` {cluster.Worker} |
|
|
|
|
|
|
|
@ -553,6 +617,9 @@ cluster.on('online', (worker) => { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## Event: 'setup' |
|
|
## Event: 'setup' |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.1 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `settings` {Object} |
|
|
* `settings` {Object} |
|
|
|
|
|
|
|
@ -565,6 +632,9 @@ The `settings` object is the `cluster.settings` object at the time |
|
|
If accuracy is important, use `cluster.settings`. |
|
|
If accuracy is important, use `cluster.settings`. |
|
|
|
|
|
|
|
|
## cluster.disconnect([callback]) |
|
|
## cluster.disconnect([callback]) |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.7 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `callback` {Function} called when all workers are disconnected and handles are |
|
|
* `callback` {Function} called when all workers are disconnected and handles are |
|
|
closed |
|
|
closed |
|
@ -579,6 +649,9 @@ The method takes an optional callback argument which will be called when finishe |
|
|
This can only be called from the master process. |
|
|
This can only be called from the master process. |
|
|
|
|
|
|
|
|
## cluster.fork([env]) |
|
|
## cluster.fork([env]) |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.6.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `env` {Object} Key/value pairs to add to worker process environment. |
|
|
* `env` {Object} Key/value pairs to add to worker process environment. |
|
|
* return {cluster.Worker} |
|
|
* return {cluster.Worker} |
|
@ -588,6 +661,9 @@ Spawn a new worker process. |
|
|
This can only be called from the master process. |
|
|
This can only be called from the master process. |
|
|
|
|
|
|
|
|
## cluster.isMaster |
|
|
## cluster.isMaster |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.8.1 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Boolean} |
|
|
* {Boolean} |
|
|
|
|
|
|
|
@ -596,12 +672,18 @@ by the `process.env.NODE_UNIQUE_ID`. If `process.env.NODE_UNIQUE_ID` is |
|
|
undefined, then `isMaster` is `true`. |
|
|
undefined, then `isMaster` is `true`. |
|
|
|
|
|
|
|
|
## cluster.isWorker |
|
|
## cluster.isWorker |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.6.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Boolean} |
|
|
* {Boolean} |
|
|
|
|
|
|
|
|
True if the process is not a master (it is the negation of `cluster.isMaster`). |
|
|
True if the process is not a master (it is the negation of `cluster.isMaster`). |
|
|
|
|
|
|
|
|
## cluster.schedulingPolicy |
|
|
## cluster.schedulingPolicy |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.11.2 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
The scheduling policy, either `cluster.SCHED_RR` for round-robin or |
|
|
The scheduling policy, either `cluster.SCHED_RR` for round-robin or |
|
|
`cluster.SCHED_NONE` to leave it to the operating system. This is a |
|
|
`cluster.SCHED_NONE` to leave it to the operating system. This is a |
|
@ -617,6 +699,9 @@ distribute IOCP handles without incurring a large performance hit. |
|
|
values are `"rr"` and `"none"`. |
|
|
values are `"rr"` and `"none"`. |
|
|
|
|
|
|
|
|
## cluster.settings |
|
|
## cluster.settings |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.1 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Object} |
|
|
* {Object} |
|
|
* `execArgv` {Array} list of string arguments passed to the Node.js |
|
|
* `execArgv` {Array} list of string arguments passed to the Node.js |
|
@ -638,6 +723,9 @@ the settings, including the default values. |
|
|
This object is not supposed to be changed or set manually, by you. |
|
|
This object is not supposed to be changed or set manually, by you. |
|
|
|
|
|
|
|
|
## cluster.setupMaster([settings]) |
|
|
## cluster.setupMaster([settings]) |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.1 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* `settings` {Object} |
|
|
* `settings` {Object} |
|
|
* `exec` {String} file path to worker file. (Default=`process.argv[1]`) |
|
|
* `exec` {String} file path to worker file. (Default=`process.argv[1]`) |
|
@ -680,6 +768,9 @@ cluster.fork(); // http worker |
|
|
This can only be called from the master process. |
|
|
This can only be called from the master process. |
|
|
|
|
|
|
|
|
## cluster.worker |
|
|
## cluster.worker |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Object} |
|
|
* {Object} |
|
|
|
|
|
|
|
@ -698,6 +789,9 @@ if (cluster.isMaster) { |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
## cluster.workers |
|
|
## cluster.workers |
|
|
|
|
|
<!-- YAML |
|
|
|
|
|
added: v0.7.0 |
|
|
|
|
|
--> |
|
|
|
|
|
|
|
|
* {Object} |
|
|
* {Object} |
|
|
|
|
|
|
|
|