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:
// Load your environment; // Grab a copy of the cluster manager classvar ClusterManager = ; // Instantiate a new manager using environment variable configurationvar manager = { // This is the closure called after worker processes are forked}; // Finally, start your cluster!managerstart;
Via Custom Options
Developers can also instantiate a ClusterManager
using options to configure
how the manager operates, like so:
var ClusterManager = ;var manager = // Worker processes execute this on process start: { // ... } // Master process executes this when you call `manager.start()`: { // ... } // Explicitly tell it the number of workers to fork: numWorkers: 16 // Tell it not to kill the master process on an un-handled error // (sometimes useful, not recommended) killOnError: false // Perform some action before the master process exits due to an error { // Do what you need to before the process is killed... // Then call the `done` function ; }; // Start the cluster!managerstart;
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 = ;var ClusterManager = ; // Create a new cluster manager for your applicationvar manager = { appstart;}; // Spawn new workers when others die...managercluster; // Start the clustermanagerstart;
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 = ;var monitor = ;var inherits = inherits;var app = ; { ClusterManager;}; // Override `_startWorker` since this manager only works for this particular appAppManagerprototype { appstart;}; // Increment a `workers` key in datadog when a worker is createdAppManagerprototype { var worker = ClusterManagerprototypecreateWorker; monitor; return worker;}; // Decrement the `workers` key when a worker diesAppManagerprototype { ClusterManagerprototypeexit; monitor;}; // Start the custom clustervar manager = ;managerstart;
License
MIT