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.
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.