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.

Variables

exitedAfterDisconnect:Null<Bool>

Set by calling kill or disconnect. Until then, it is undefined.

Lets you distinguish between voluntary and accidental exit, the master may choose not to respawn a worker based on this value.

read onlyid:String

Each new worker is given its own unique id, this id is stored in the id.

While a worker is alive, this is the key that indexes it in Cluster.workers

process:ChildProcess

All workers are created using ChildProcess.fork, the returned object from this function is stored as process. In a worker, the global process is stored.

Note that workers will call process.exit(0) if the 'disconnect' event occurs on process and suicide is not true. This protects against accidental disconnection.

suicide:Null<Bool>

Set by calling kill or disconnect, until then it is undefined.

Lets you distinguish between voluntary and accidental exit, the master may choose not to respawn a worker based on this value.

(an alias to exitedAfterDisconnect in newer node.js versions)

Methods

disconnect():Void

In a worker, this function will close all servers, wait for the 'close' event on those servers, and then disconnect the IPC channel.

In the master, an internal message is sent to the worker causing it to call disconnect on itself.

Causes suicide to be set.

isConnected():Bool

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 created. It is disconnected after the 'disconnect' event is emitted.

isDead():Bool

This function returns true if the worker's process has terminated (either because of exiting or being signaled). Otherwise, it returns false.

kill(?signal:String):Void

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.

Causes suicide to be set.

send(message:Dynamic, ?callback:Error ‑> Void):Bool

send(message:Dynamic, sendHandle:Dynamic, ?callback:Error ‑> Void):Bool

This function is equal to the send methods provided by ChildProcess.fork. In the master you should use this function to send a message to a specific worker.

In a worker you can also use process.send, it is the same function.

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: