<commit_before># multi-cluster

Create robust node.js applications with automatic forking of multiple processes and multiple apps, signal handling and reforking.

All in a couple of lines and without changing your existing code!

### This is an early BETA version

The thoughts and techniques behind this module, has been derived from live systems running incredibly stabile!
That said, this is not code tested on live systems (yet).

As soon as the module has shown it's worth and stability on a live system, it will be marked as version >= 1.0.0.

Until then: Feel free to play around with it, learn from it.


### To install

	npm install multi-cluster

### Possible pitfall

You can NOT run 2 different applications, that both starts an HTTP (or TCP) server and tries to listen on the same port!!!

This would not fail when trying - just give you some weird results!

The reason for this lies in the node.js cluster handler, which CAN split incoming requests between processes, but can't know, which application should get which requests.

This will of course result in unwanted results, where you will see all requests being handled by one app and suddenly shift to the other app.


# The thoughts behind

Basically the module will enable you to seperate your app code from cluster and signal handling code.
You can start 1 or many apps with a single script.
The module will keep track on which processes (app childs) dies, which to restart and enables you to live reload all apps at once.

### You don't need to change a single line of code

The architecture of this module simplifies development by seperating everything completely!
You can write your app as if it should run by itself, without signal handling and clustering.

**Once you want to put it live, you can just write a start script like this:**

	var MultiCluster = require('multi-cluster');
	var multiCluster = new MultiCluster('myapp.js');

That's it!

--

**If you want to automate 3 more apps, it will look something like this:**


	var MultiCluster = require('multi-cluster');
	var multiCluster1 = new MultiCluster('myapp.js');
	var multiCluster2 = new MultiCluster('my_second_app.js');
	var multiCluster3 = new MultiCluster('my_third_app.js');
	var multiCluster4 = new MultiCluster('my_fourth_app.js');

Can it get any simpler?


# Reload on file change

This is a simple implementation for now.

The following will look for file changes in the current working dir and reload the child processes. It will wait for up to 5 secs, for all connections to close properly. 

	var MultiCluster = require('multi-cluster');
	var multiCluster = new MultiCluster('myapp.js');
	multiCluster.watch(__dirname);


# Resource usage info

If you want the library to gather information about resource usage, you just need to include the multi-cluster lib for each app, you want info from.

I.e. in myapp.js add this line:

	var MultiCluster = require('multi-cluster');
	
Now you can use one of these two (promise):

	MultiCluster.aggregateStats().then(console.log);

Or (callback):

	MultiCluster.aggregateStats(function(stats) { console.log(stats); });
<commit_msg>Fixed a wrong example in the readme<commit_after># multi-cluster

Create robust node.js applications with automatic forking of multiple processes and multiple apps, signal handling and reforking.

All in a couple of lines and without changing your existing code!

### This is an early BETA version

The thoughts and techniques behind this module, has been derived from live systems running incredibly stabile!
That said, this is not code tested on live systems (yet).

As soon as the module has shown it's worth and stability on a live system, it will be marked as version >= 1.0.0.

Until then: Feel free to play around with it, learn from it.


### To install

	npm install multi-cluster

### Possible pitfall

You can NOT run 2 different applications, that both starts an HTTP (or TCP) server and tries to listen on the same port!!!

This would not fail when trying - just give you some weird results!

The reason for this lies in the node.js cluster handler, which CAN split incoming requests between processes, but can't know, which application should get which requests.

This will of course result in unwanted results, where you will see all requests being handled by one app and suddenly shift to the other app.


# The thoughts behind

Basically the module will enable you to seperate your app code from cluster and signal handling code.
You can start 1 or many apps with a single script.
The module will keep track on which processes (app childs) dies, which to restart and enables you to live reload all apps at once.

### You don't need to change a single line of code

The architecture of this module simplifies development by seperating everything completely!
You can write your app as if it should run by itself, without signal handling and clustering.

**Once you want to put it live, you can just write a start script like this:**

	var MultiCluster = require('multi-cluster');
	var multiCluster = new MultiCluster('myapp.js');

That's it!

--

**If you want to automate 3 more apps, it will look something like this:**


	var MultiCluster = require('multi-cluster');
	var multiCluster1 = new MultiCluster('myapp.js');
	var multiCluster2 = new MultiCluster('my_second_app.js');
	var multiCluster3 = new MultiCluster('my_third_app.js');
	var multiCluster4 = new MultiCluster('my_fourth_app.js');

Can it get any simpler?


# Reload on file change

This is a simple implementation for now.

The following will look for file changes in the current working dir and reload the child processes. It will wait for up to 5 secs, for all connections to close properly. 

	var MultiCluster = require('multi-cluster');
	var multiCluster = new MultiCluster('myapp.js');
	multiCluster.watch(__dirname);


# Resource usage info

If you want the library to gather information about resource usage, you just need to include the multi-cluster lib for each app, you want info from.

I.e. in myapp.js add this line:

	var MultiCluster = require('multi-cluster');
	
Now you access stats like this:

	MultiCluster.aggregateStats(function(stats) { console.log(stats); });

{'fraction_non_alphanumeric': 0.061237679754402974, 'fraction_numerical': 0.003554693811601228, 'mean_word_length': 4.434591747146619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1477717', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1543, 'n_words': 900}