new ClusterManager(opts)
Utility class for creating new server clusters.
Parameters:
Name | Type | Default | Description |
---|---|---|---|
opts |
object | function | Options for the cluster or a worker function to execute on worker processes. | |
opt.worker |
cluster-man~Callback | Function to execute on the worker processes. | |
opt.master |
cluster-man~Callback | Function to execute on the master process. | |
opt.numWorkers |
Number | Number of workers to spawn. Defaults to the value in `process.env.CLUSTER_WORKERS` if present, and if not then the number of CPUs as reported by `os.cpus().length`. | |
opt.debugScope |
String | Root scope for debug logging. Defaults to the value in `process.env.CLUSTER_DEBUG` if present, and if not then defaults to 'cluster-man'. | |
opt.killOnError |
Boolean | true | Whether or not to kill the master process on and unhandled error. |
opt.beforeExit |
cluster-man~BeforeExit | Callback to execute before the master process exits in response to an error. |
Throws:
Error If a opt.worker was not specified or was not a function.
Examples
var ClusterManager = require('cluster-man');
var server = require('./lib/server');
// Basic usage (if you only need to handle workers)
new ClusterManager(server.start).start();
var ClusterManager = require('cluster-man');
var server = require('./lib/server');
// Create a new cluster manager using options, for handling master process
// and worker processes with a specific number of workers.
var serverCluster = new ClusterManager({
worker: server.start,
master: masterStart,
numWorkers: 4
});
function masterStart(clusterManager) {
// Any additional things you'd like to do after the cluster
// has started...
}
// Start the cluster
serverCluster.start();
Methods
_addLogger(name, label)
Adds a logger debug method to the manager.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | Name of the logger method. |
label |
string | Output label for debug. |
_exitMaster(erropt)
Runs before exit callback and exits the master process.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
err |
Error |
<optional> |
Error that caused the master process to exit. |
_startMaster()
Starts a cluster master. Specifically this will bind worker events to
specific handlers on this manager instance, fork all worker process, setup a
domain to catch unhandled errors on the master process and execute the master
process callback (as specified in the constructor).
_startWorker()
Starts a cluster worker. Simply executes the provided worker callback.
createWorker() → {cluster~Worker}
Creates a new worker. Specifically it forks a new worker, sets a domain error
handler for the worker, and returns it.
Returns:
Newly created worker.
- Type
- cluster~Worker
disconnect(worker)
Handles worker `disconnect` events. This indicates that the worker has
disconnected from communication but is not nessessarily dead.
Parameters:
Name | Type | Description |
---|---|---|
worker |
cluster~Worker | Worker that disconnected. |
exit(worker, code, signal)
Handles worker `exit` events.
Parameters:
Name | Type | Description |
---|---|---|
worker |
cluster~Worker | Worker that exited. |
code |
Number | Exit code for the worker process. |
signal |
String | Signal name that caused the process to be killed. |
fork(Worker)
Handles worker `fork` events. This event is emitted when a worker is forked
off the master cluster.
Parameters:
Name | Type | Description |
---|---|---|
Worker |
cluster~Worker | that was forked. |
listening(Worker, address)
Handles worker `listening` events. Indicates to the master that a particular
worker is listening.
Parameters:
Name | Type | Description |
---|---|---|
Worker |
cluster~Worker | that is now listening. |
address |
Address on which the worker is listening. |
masterError(err)
Called when master process domain encounters an unhandled error. By default
this method will log the error stack, indicate that the error is fatal, and
kill the process with a status code `1`.
Parameters:
Name | Type | Description |
---|---|---|
err |
Error | Unhandled error on the master process. |
online(worker)
Handles worker `online` events. This indicates to the cluster that a worker
process has successfully spawned a process and is running.
Parameters:
Name | Type | Description |
---|---|---|
worker |
cluster~Worker | Worker that came online. |
start()
Starts either a cluster master or a worker depending on the process type at
the time of invocation.