node-cluster-app

Basic nodejs application using cluster

npm install node-cluster-app
6 downloads in the last month

Build Status NPM version

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 for timeout event
  • restart - automatically restart died workers
  • evlog - log native [cluster][] events to console

Methods

ClusterApp is [EventEmitter][], so we have all it methods as emit, on etc.

  • init - assign worker
    • worker - path to worker's js file
  • start - forks neccessary number of workers
  • stop - kill all workers
  • restart - Sequentially run of stop and start
  • setEvlog - Set mode of logging all inherited from [cluster][] events by util.log
    • value - boolean value(default: false)

Events

ClusterApp reemitted all native [cluster][] events:

  • fork - new worker forked
  • online - worker become online
  • listening - worker become listen
  • disconnect - worker disconnect
  • exit - worker exited
  • setup - cluster setupMaster was executed

Also it has a number own events:

  • timeout - Emited on worker not emit listen event longer than timeout value
    • worker - [Worker object][]
    • timeout - Value of applied timeout in ms
  • start - Emited on app started
  • stop - Emited on app stopped
  • noworkers - Emitted on last worker exit
  • log - worker initiate log action
    • msg - 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:

npm loves you