Cluster

Introduced in 4.0.0

Stability: 2 - Stable

const Cluster = require('kado/lib/Cluster')

The Cluster library is an extension of the Node.js cluster module that comes bundled. This library adds features to the existing module such as:

Class: Cluster

event 'recycle'

Emitted when a worker has been recycled

event 'orphan'

Emitted when a worker no longer has contact to the master

event 'exit'

Emitted when a worker exits

event 'worker'

Emitted when a new worker comes online.

event 'respawn'

Emitted when a worker has respawned

event 'path'

Emitted when a new worker path has been set

event 'status'

Emitted when the status of the cluster changes and will be one of the following values.

event 'restart'

Emitted when the cluster has been restarted by the restart() method.

event 'messageError'

Emitted when an error occurs sending a worker a message

event 'error'

Emitted when an error occurs

static Cluster.handleSigint()

This adds proper SIGINT handling on win32 systems.

Internal Use

static Cluster.child(server, title, start, stop)

This method is used in cluster workers to setup communication with the master process.

static Cluster.getInstance(options)

Cluster.constructor(options)

Available Options:

Cluster.isMaster(options)

Available Options

NOTE: This will always return false when disableMaster is set to true which enables single process mode.

Example Usage for Development:

const Cluster = require('kado/lib/Cluster')
const cluster = Cluster.getInstance()
if (cluster.isMaster()) {
 // watch files, set process title, start other children
} else {
  // worker operations, listening routing, cli processing etc
}

The above code provides a single process system in development and a fully concurrent ready for load system in production. The scaling in this example is fully automatic.

Cluster.isWorker()

Cluster.getSchedulingPolicy()

Cluster.setSchedulingPolicy(policy)

Cluster.getSettings()

Cluster.registerExitHandler()

Internal Use

Cluster.prepareWorker(worker)

This method sets up to remove listeners form workers to prevent a memory leak over time.

Internal Use

Cluster.checkRecycle(worker)

Internal Use

Cluster.recycleWorker(worker)

This will stop the existing worker passed in and start a replacement that is returned.

Internal Use

Cluster.handleWorkerMessage(worker, message)

Internal Use

Cluster.setupWorker(worker)

Internal Use

Cluster.respawnWorker(worker, code, signal)

Internal Use

Cluster.setupCluster()

Used to setup the master process with the required options.

Internal Use

Cluster.getMasterOptions()

Internal Use

Cluster.prepareCluster()

Called internally by Cluster.start() and will setup the master and start the heartbeat.

Internal Use

Cluster.handleWatchEvent(eventType, filename, path)

This is actually an event handler for the fs.watch() and fs.watchFile() methods called by the Cluster.addWatcher() method.

The public method is Cluster.watch(file1, file2...)

Internal Use

Cluster.addWatcher(path, options)

This adds a watcher for the given file with the options and adds them to the internal watch list.

Internal Use

Cluster.watch(path1, path2…)

This method will watch the given paths for changes or renames and when that occurs the cluster processes will be restarted to make the new changes take effect.

NOTE: This functionality is DISABLED when the NODE_ENV=production environment variable is set.

NOTE: If an Array is passed as the first argument, all other arguments will be ignored.

Cluster.unwatch(path1, path2…)

This is the opposite of the Cluster.watch() method.

Cluster.each(fn)

Cluster.kill(signal = 'SIGTERM')

Cluster.destruct()

This also tears down the master and the clears the master instance. Used for reusing the master process for other purposes.

Cluster.exit(code)

This method will immediately kill all workers with SIGKILL and then end the master process with code.

Cluster.send(message)

Cluster.setEnv(env)

Cluster.getEnv()

Cluster.setPath(path)

Cluster.start()

Cluster.stop()

Cluster.restart()