cluster-fuck
A simple, elegant cluster forker with TCP REPL server via prepl.
Usage
var ClusterFuck = myCluster; myCluster = exec: "mySpecialWorker.js" args: '--my' '--arguments' silent: false workers: 8; myClusterstart;
When workers are ready, they must send a ready
signal and their memory usage:
var server = http; server; server;
^C quits gracefully; however, you may connect to the REPL to further control the cluster using nc:
$ nc -U prepl.sock
> help
COMMAND DESCRIPTION
help Display this help message
exit Disconnect from REPL
status Display the status of the Cluster
memory Display detailed memory information.
start Start the cluster
restart Restart all workers gracefully
stop Stop all workers gracefully
kill Kill all workers immediately
shutdown Stop all workers gracefully, then shutdown the cluster.
>
or another node app:
var net = ;var client = net { console;}); client; replClient var commands = 'help\n' 'kill\n' 'status\n' 'start\n' 'status\n'; for var i = 0; i < commandslength; i++ { ; }commandsi;
which would output something similar to:
connected!
> COMMAND DESCRIPTION
help Display this help message
exit Disconnect from REPL
status Display the status of the Cluster
memory Display detailed memory information.
start Start the cluster
restart Restart all workers gracefully
stop Stop all workers gracefully
kill Kill all workers immediately
shutdown Stop all workers gracefully, then shutdown the cluster.
>
>
State: stopped
Ready Workers: 0
Memory: 15.421875 MB
>
>
State: ready
Ready Workers: 7
Memory: 300.91015625 MB
>
Configuration
Default configuration:
Options
args
: see cluster.setupMaster([settings])exec
: see cluster.setupMaster([settings])silent
: see cluster.setupMaster([settings])prepl
: see prepl configurationshutdownTimeout
: period in ms to wait for workers to shutdown. Default5000
workers
: the number of workers to fork. Default:1
API
ClusterFuck
Exposed by require('cluster-fuck')
.
ClusterFuck()
Creates a new ClusterFuck
. Works with and without new
:
var ClusterFuck = ; // orvar ClusterFuck = ;var myCluster = ;
ClusterFuck.start()
Starts the cluster:
myClusterstart;
ClusterFuck.shutdown([done:Function])
- done
function
: function to be called when the cluster has shutdown
Used to stop all workers and shutdown the cluster:
myCluster
ClusterFuck.memoryUsage([unit: String])
- unit
function
: if specified, this function returns the total memory formatted inunit
s.
Returns the total memory usage for each process
var memory = myCluster;//{ cluster: { rss: 308719616, heapUsed: 17281008, heapTotal: 34354176 },// master: { rss: 15187968, heapTotal: 10456064, heapUsed: 4650712 },// workers: // { '1': { rss: 43851776, heapTotal: 34354176, heapUsed: 19252248 },// '2': { rss: 41627648, heapTotal: 34354176, heapUsed: 16673376 },// '3': { rss: 41598976, heapTotal: 34354176, heapUsed: 17105496 },// '4': { rss: 41418752, heapTotal: 34354176, heapUsed: 16969800 },// '5': { rss: 41639936, heapTotal: 34354176, heapUsed: 16935840 },// '6': { rss: 41676800, heapTotal: 34354176, heapUsed: 17133768 },// '7': { rss: 41717760, heapTotal: 34354176, heapUsed: 17281008 } },// workerTotal: 293531648 } // orvar memory = myCluster;// 294.11328125 MB
note: unit
must be a valid unit in techjeffharris-utils.
Events
In addtion to all events inherited from cluster, ClusterFuck exposes the following events:
Event: 'starting'
Emitted before the cluster starts all workers:
myCluster;
Event: 'ready'
Emitted when all workers are ready:
myCluster;
Event: 'restarting'
Emitted before the cluster restarts all workers:
myCluster;
Event 'restarted'
Emitted when all workers are ready after a restart:
myCluster;
Event: 'shuttingDown'
Emitted when the cluster starts shutting down.
myCluster;
Event: 'shutdown'
Emitted when the cluster has shut down.
myCluster;
Event: 'stopping'
Emitted before the cluster stops all workers:
myCluster;
Event: 'stopped'
Emitted when all workers have stopped:
myCluster;
Event 'killing'
Emitted before the cluster kills all workers:
myCluster;
Event: 'killed'
Emitted when all workers have been killed:
myCluster;