Server Cluster
Server cluster.
Installation
$ npm install @kgryte/server-cluster
Usage
var serverCluster = require( '@kgryte/server-cluster' );
serverCluster( [ options,] logger )
Returns a function
to create a server cluster.
var bunyan = require( 'bunyan' );
// Create a logger...
var logger = bunyan.createLogger({
'name': 'logger',
'streams': [
{
'name': 'main',
'level': 'info',
'stream': process.stdout
}
]
});
// Create a function for creating a server cluster...
var create = serverCluster( logger );
The function
accepts the following options
:
-
size: cluster size. Default:
num_cpus
. -
timeout: maximum allowed time for a new worker to be ready to receive HTTP requests before being considered unhealthy. Default:
5000
[ms].
To specify cluster options,
var opts = {
'size': 8,
'timeout': 2500 // [ms]
};
var create = serverCluster( opts, logger );
create( done )
Creates a server cluster.
function done() {
console.log( 'Success!' );
process.exit( 0 );
}
create( done );
Notes
- This module does not, by itself, create
server
instances. Instead, the module expects to be used in conjunction with code which handles server creation. See the examples below. - For server creation modules, see
Examples
var cluster = require( 'cluster' ),
bunyan = require( 'bunyan' ),
express = require( 'express' ),
httpServer = require( '@kgryte/http-server' ),
serverCluster = require( '@kgryte/server-cluster' );
// LOGGER //
// Create a logger...
var logger = bunyan.createLogger({
'name': 'logger',
'streams': [
{
'name': 'main',
'level': 'info',
'stream': process.stdout
}
]
});
// CLUSTER //
var createCluster;
/**
* FUNCTION: onCluster()
* Callback invoked once all workers are running and a cluster is ready to receive HTTP requests.
*
* @returns {Void}
*/
function onCluster() {
console.log( 'Cluster is successfully running!' );
}
// If the current process is a 'master' process, create a server cluster...
if ( cluster.isMaster ) {
// Create a function for creating a server cluster...
createCluster = serverCluster( logger );
// Create the cluster...
return createCluster( onCluster );
}
// SERVER/WORKER //
// Specify server options...
var opts = {
'port': 7331
};
// Create an express app:
var app = express();
// Create a function for creating an HTTP server...
var createServer = httpServer( opts, logger, app );
/**
* FUNCTION: done( error, server )
* Callback invoked once a server is ready to receive HTTP requests.
*
* @param {Error|Null} error - error object
* @param {Server} server - server instance
* @returns {Void}
*/
function done( error, server ) {
if ( error ) {
throw error;
}
console.log( server.address() );
// Close the server and exit:
setTimeout( onTimeout, 2500 );
}
/**
* FUNCTION: onTimeout()
* Callback invoked after a specified interval. Kills the current worker process.
*
* @returns {Void}
*/
function onTimeout() {
logger.info( 'Killing worker %s. Process id: %d.', cluster.worker.id, cluster.worker.process.pid );
cluster.worker.kill();
}
// Create a server:
createServer( done );
To run the example code from the top-level application directory,
$ node ./examples/index.js
Tests
Unit
Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:
$ make test
All new feature development should have corresponding unit tests to validate correct functionality.
Test Coverage
This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:
$ make test-cov
Istanbul creates a ./reports/coverage
directory. To access an HTML version of the report,
$ make view-cov
License
Copyright
Copyright © 2015. Athan Reines.