# cluster-man

> Extendable and easy-to-use node cluster management.

Latest version **1.1.1** (published 2015-06-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install cluster-man
pnpm add cluster-man
yarn add cluster-man
bun add cluster-man
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2015-06-03 |
| First published | 2015-04-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Ryan Sandor Richards |
| Maintainers | rsandor |
| Keywords | cluster, management, node |

## Links

- npm: https://www.npmjs.com/package/cluster-man
- Repository: https://github.com/Runnable/cluster-man
- Issues: https://github.com/Runnable/cluster-man/issues
- npm.io page: https://npm.io/package/cluster-man

## Dependencies (4)

- [101](https://npm.io/package/101.md) ^0.16.1
- [debug](https://npm.io/package/debug.md) ^2.1.3
- [loadenv](https://npm.io/package/loadenv.md) ^1.0.3
- [map-utils](https://npm.io/package/map-utils.md) ^0.4.0

## Alternatives

- [@expo/fingerprint](https://npm.io/package/@expo/fingerprint.md) — 6.2M weekly downloads
- [@azure/monitor-opentelemetry-exporter](https://npm.io/package/@azure/monitor-opentelemetry-exporter.md) — 850.0K weekly downloads
- [@azure/monitor-opentelemetry](https://npm.io/package/@azure/monitor-opentelemetry.md) — 624.0K weekly downloads
- [@posthog/ai](https://npm.io/package/@posthog/ai.md) — 423.3K weekly downloads
- [fakefilter](https://npm.io/package/fakefilter.md) — 63.9K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2015-06-03
- 1.1.0 — 2015-04-17
- 1.0.0 — 2015-04-10
- 0.0.1 — 2015-04-08

## README

# cluster-man

![Build Status](https://travis-ci.org/Runnable/cluster-man.svg?branch=master)
![Dependency Status](https://david-dm.org/Runnable/cluster-man.svg)
![devDependency Status](https://david-dm.org/Runnable/cluster-man/dev-status.svg)

[![NPM](https://nodei.co/npm/cluster-man.png?compact=true)](https://nodei.co/npm/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](https://www.npmjs.com/package/debug)

Here's an example of how to use cluster man with as little configuration as
possible:

```js
// Load your environment
require('loadenv')();

// Grab a copy of the cluster manager class
var ClusterManager = require('cluster-man');

// Instantiate a new manager using environment variable configuration
var manager = new ClusterManager(function () {
  // This is the closure called after worker processes are forked
});

// Finally, start your cluster!
manager.start();
```

**Via Custom Options**

Developers can also instantiate a `ClusterManager` using options to configure
how the manager operates, like so:

```js
var ClusterManager = require('cluster-man');
var manager = new ClusterManager({
  // Worker processes execute this on process start:
  worker: function () {
    // ...
  },

  // Master process executes this when you call `manager.start()`:
  master: function () {
    // ...
  },

  // 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
  beforeExit: function(err, done) {
    // Do what you need to before the process is killed...

    // Then call the `done` function
    done();
  }
});

// Start the cluster!
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**
```js
var app = require('./lib/app.js');
var ClusterManager = require('cluster-man');

// Create a new cluster manager for your application
var manager = new ClusterManager(function () {
  app.start();
});

// Spawn new workers when others die...
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();
  }
});

// Start the cluster
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`:

```js
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);

// Override `_startWorker` since this manager only works for this particular app
AppManager.prototype._startWorker = function () {
  app.start();
};

// Increment a `workers` key in datadog when a worker is created
AppManager.prototype.createWorker = function() {
  var worker = ClusterManager.prototype.createWorker.apply(this, arguments);
  monitor.increment('workers');
  return worker;
};

// Decrement the `workers` key when a worker dies
AppManager.prototype.exit = function (worker, code, signal) {
  ClusterManager.prototype.exit.call(this, worker, code, signal);
  monitor.increment('workers', -1);
};

// Start the custom cluster
var manager = new AppManager();
manager.start();
```

## License
MIT

---
_Source: https://npm.io/package/cluster-man · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
