node-cluster-app v0.0.3
node-cluster-app
#Basic nodejs application using cluster
Install
npm install node-cluster-app
##Usage:
var ClusterApp = require('node-cluster-app')
var app = new ClusterApp({
workers: 2,
timeout: 2000,
restart: true,
evlog: false
})
app.init('worker.js')
app.start()Also view sample app in the examples directory
##Constructor options
workers- workers number (default: cpu number)timeout- timeout fortimeouteventrestart- automatically restart died workersevlog- log native cluster events to console
##Methods
ClusterApp is EventEmitter, so we have all it methods as emit, on etc.
init- assign workerworker- path to worker's js file
start- forks neccessary number of workersstop- kill all workersrestart- Sequentially run ofstopandstartsetEvlog- Set mode of logging all inherited from cluster events by util.logvalue- boolean value(default: false)
##Events ClusterApp reemitted all native cluster events:
fork- new worker forkedonline- worker become onlinelistening- worker become listendisconnect- worker disconnectexit- worker exitedsetup- clustersetupMasterwas executed
Also it has a number own events:
timeout- Emited on worker not emitlistenevent longer than timeout valueworker- Worker objecttimeout- Value of applied timeout in ms
start- Emited on app startedstop- Emited on app stoppednoworkers- Emitted on last workerexitlog- worker initiatelogactionmsg- message by worker provided
Worker API
Worker is a simple nodejs executable.
It can to talk with a ClusterApp throw standard process.send(msg) mechanism
Commands transferred by msg.actions param
For now supports next commands:
stop- Initiate stopping of the ClusterApprestart- Initiate restarting of the ClusterApplog- Transfer some data to ClusterApplogevent -msg- Provided data For details you can review sample httpworker in/testsdirectory of this repocluster:http://nodejs.org/docs/latest/api/cluster.html
EventEmitter:http://nodejs.org/api/events.html#events_class_events_eventemitter
Worker object:http://nodejs.org/docs/latest/api/cluster.html#cluster_class_worker
httpworker:https://github.com/vpArth/node-cluster-app/blob/master/tests/httpworker.js
examples:https://github.com/vpArth/node-cluster-app/tree/master/examples

