cluster-man
Extendable and easy-to-use node cluster management.
Basic Usage
Via Environment Configuration
By Default cluster-man configure itself via process.env
by using the following
variables:
process.env.CLUSTER_WORKERS
(Integer) - Number of workers to fork from the
master process when the cluster is started.process.env.CLUSTER_DEBUG
(String) - Prefix for cluster event logging via
debug
Here's an example of how to use cluster man with as little configuration as
possible:
require('loadenv')();
var ClusterManager = require('cluster-man');
var manager = new ClusterManager(function () {
});
manager.start();
Via Custom Options
Developers can also instantiate a ClusterManager
using options to configure
how the manager operates, like so:
var ClusterManager = require('cluster-man');
var manager = new ClusterManager({
worker: function () {
},
master: function () {
},
numWorkers: 16,
killOnError: false,
beforeExit: function(err, done) {
done();
}
});
manager.start();
API Documentation
For the full API documentation, please visit http://runnable.github.io/cluster-man/
Extending ClusterManager
While we think that the basic behaviors encapsulated by cluster-man represent a
reasonable approach to handling clustering, it stands to reason that there will
be times when a developer needs to handle clustering in a specific way for their
application.
To aid such specialized behaviors the ClusterManager
class was designed to be
extendable via prototypal inheritance. Furthermore, instances expose the node
cluster
directly so additional eventing can easily be added.
Example: Adding additional cluster event listeners
var app = require('./lib/app.js');
var ClusterManager = require('cluster-man');
var manager = new ClusterManager(function () {
app.start();
});
manager.cluster.on('exit', function (worker, code, signal) {
var delta = manager.options.numWorkers - manager.workers.length;
for (var i = 0; i < delta; i++) {
this.createWorker();
}
});
manager.start();
Example: Worker Start/Stop Monitoring
Here's an example of how to extend ClusterManager
to log worker start and stop
information with monitor-dog
:
var ClusterManager = require('cluster-man');
var monitor = require('monitor-dog');
var inherits = require('util').inherits;
var app = require('./lib/app.js');
function AppManager() {
ClusterManager.apply(this, arguments);
}
inherits(AppManager, ClusterManager);
AppManager.prototype._startWorker = function () {
app.start();
};
AppManager.prototype.createWorker = function() {
var worker = ClusterManager.prototype.createWorker.apply(this, arguments);
monitor.increment('workers');
return worker;
};
AppManager.prototype.exit = function (worker, code, signal) {
ClusterManager.prototype.exit.call(this, worker, code, signal);
monitor.increment('workers', -1);
};
var manager = new AppManager();
manager.start();
License
MIT