Giter VIP home page Giter VIP logo

simple-express-cluster's Introduction

Simple Express Cluster

A simple and light weight library to implement the cluster for your node apps.

Why use simple-express-cluster?

  • Simple and easy to use.
  • Works for both express and native node servers.
  • Auto restart workers during uncaught exception until reach the maximum limit of restart count.
  • Displaying running cluster status. (i.e) Healthcheck, stats.
  • Zero dependancy.
  • Typescript supported.

Installation

To install the stable version:

Using npm as your package manager.

  npm install --save simple-express-cluster

Using yarn as your package manager.

  yarn add simple-express-cluster

Usage

Basic Usage

Example 1

const express = require('express');
const SimpleExpressCluster = require('simple-express-cluster').SimpleExpressCluster;
const simpleExpCluster = new SimpleExpressCluster();
const app = express()
const port = 3000
app.get('/', function (req, res) {
    res.send('Hello World...!').end();
});
simpleExpCluster.run((worker) => {
    console.log(worker.process.pid)
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })
});

Example 2

const express = require('express');
const SimpleExpressCluster = require('simple-express-cluster').SimpleExpressCluster;
const simpleExpCluster = new SimpleExpressCluster({ cpus:2,  auto_restart: true, auto_restart_limit: 3 });
simpleExpCluster.on('output', (message) => console.log(message));
const app = express()
const port = 3000
app.get('/', function (req, res) {
    res.send('Hello World...!').end();
});
simpleExpCluster.setExpress(app);
simpleExpCluster.run((worker) => {
    console.log(worker.process.pid)
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })
});

API

Config

we can configure the default values by passing the config object to simple express cluster contructor

  • cpus - Number of workers.
  • auto_restart - Restart the worker on the process exit
  • auto_restart_limit - Restart limit to avoid the round robin
{
    cpus: 4,                // defaults to os.cpus().length
    auto_restart: false,    // defaults to false
    auto_restart_limit: 3   // defaults to 3
}

run

The run method will initialize cluster based on configuration. The current worker will be received in the argument callback itself.

simpleExpCluster.run((worker) => {
    app.listen(port, () => {
        console.log(`Example app listening at http://localhost:${port}`)
    })
});

Note: Ensure that you have invoked the server listen method in the callback method

setExpress

Set the current express instance in simple express cluster. so that, the healthcheck and stats api will be attached in the given express instance.

simpleExpCluster.setExpress(app);

Note: The setExpress method is only applicable for express servers.

/cluster/healthcheck

Gives the cluster status

curl localhost:3000/cluster/healthcheck
> Simple express cluster is runnging...!

/cluster/stats

Gives the cluster stats. (i.e) Number of worker, worker process ids..etc.

curl localhost:3000/cluster/stats
> {"cluster_size":2,"master_process_id":32183,"cluster_status":"running","workers":[{"worker_id":2,"pid":32191,"status":"online"},{"worker_id":1,"pid":32190,"status":"exit","reason":"The worker died with exit code 0, and signal null"},{"worker_id":3,"pid":32209,"status":"online"}]}

Event

A customer event to notify the verbose logs

simpleExpCluster.on('output', (message) => console.log(message));

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

simple-express-cluster's People

Contributors

gokulakannant avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.