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. The cluster module allows you to easily create child processes that all share server ports.

This feature was introduced recently, and may change in future versions. Please try it out and provide feedback.

Also note that, on Windows, it is not yet possible to set up a named pipe server in a worker.

Static variables

@:value(cast Cluster)staticinlineread onlyinstance:Cluster = cast Cluster

A reference to the Cluster object returned by node.js module.

It can be imported into module namespace by using: "import js.node.Cluster.instance in cluster"

Variables

read onlyisMaster:Bool

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.

read onlyisWorker:Bool

True if the process is not a master (it is the negation of isMaster).

schedulingPolicy:ClusterSchedulingPolicy

The scheduling policy, either SCHED_RR for round-robin or SCHED_NONE to leave it to the operating system.

This is a global setting and effectively frozen once you spawn the first worker or call setupMaster, whatever comes first.

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.

schedulingPolicy can also be set through the NODE_CLUSTER_SCHED_POLICY environment variable. Valid values are "rr" and "none".

read onlysettings:ClusterSettings

After calling setupMaster (or fork) this settings object will contain the settings, including the default values.

It is effectively frozen after being set, because setupMaster can only be called once.

This object is not supposed to be changed or set manually, by you.

read onlyworker:Worker

A reference to the current worker object.

Not available in the master process.

read onlyworkers:DynamicAccess<Worker>

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.

A worker is removed from workers just before the 'disconnect' or 'exit' event is emitted.

Should you wish to reference a worker over a communication channel, using the worker's unique id is the easiest way to find the worker.

Methods

disconnect(?callback:() ‑> Void):Void

Calls disconnect on each worker in workers.

When they are disconnected all internal handles will be closed, allowing the master process to die gracefully if no other event is waiting.

The method takes an optional callback argument which will be called when finished.

This can only be called from the master process.

fork(?env:DynamicAccess<String>):Worker

Spawn a new worker process.

This can only be called from the master process.

setupMaster(?settings:{silent:Null<Bool>, exec:Null<String>, args:Null<Array<String>>}):Void

setupMaster is used to change the default fork behavior.

Once called, the settings will be present in settings.

Note that:

Only the first call to `setupMaster` has any effect, subsequent calls are ignored

That because of the above, the only attribute of a worker that may be customized per-worker
is the `env` passed to `fork`

`fork` calls `setupMaster` internally to establish the defaults, so to have any effect,
`setupMaster` must be called before any calls to `fork`

Inherited Variables

Inherited Methods

Defined by EventEmitter

addListener<T>(eventName:Event<T>, listener:T):TSelf

emit<T>(eventName:Event<T>, args:Rest<Dynamic>):Bool

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

See also:

eventNames():Array<EitherType<String, Symbol>>

Returns an array listing the events for which the emitter has registered listeners. The values in the array will be strings or Symbols.

See also:

getMaxListeners():Int

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.

See also:

listenerCount<T>(eventName:Event<T>):Int

Returns the number of listeners listening to the event named eventName.

See also:

listeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName.

See also:

off<T>(eventName:Event<T>, listener:T):TSelf

on<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

once<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

See also:

prependListener<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

prependOnceListener<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

See also:

rawListeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

See also:

removeAllListeners<T>(?eventName:Event<T>):TSelf

Removes all listeners, or those of the specified eventName.

See also:

removeListener<T>(eventName:Event<T>, listener:T):TSelf

Removes the specified listener from the listener array for the event named eventName.

See also:

setMaxListeners(n:Int):Void

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. Obviously, not all events should be limited to just 10 listeners. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

See also: