Giter Club home page Giter Club logo

like-process's Introduction

like-process

Handle resources and events for gracefully exit

const like = require('like-process');

setTimeout(() => this_var_not_exists, 2000);

let intervalId = setInterval(() => {
  console.log('count');
}, 500);

like.on('cleanup', () => {
  console.log('cleanup');
  clearInterval(intervalId);
});

like.handle('uncaughtException', (evt, err) => {
  console.error(err);
});

Install

npm i like-process

Features

Handled signals by default:

  • SIGTERM exit for docker, k8s, etc.
  • SIGINT exit for pm2 cluster and fork.
  • SIGHUP reload for native cluster.

Reload or SIGHUP in single process will just exit.

Handle resources (interval, timeout, etc) with:

  • 'terminate' and 'cleanup' event.
  • like.terminated and like.cleanup states.

cleanup event and state are only for single process or worker but not master.

Fallback start-first redundancy with:

  • Native cluster module.
  • PM2 fork + native cluster.
  • Container + native cluster.

Description

It was made to combine with like-server.
Using pm2 will send the ready signal when all servers are listening.
Using native cluster there is also an internal ready signal.
Handling uncaughtException or unhandledRejection the default exitCode is 1.

Properties

like.terminated: Boolean
like.cleanup: Boolean
like.fallback: Boolean
like.exitCode: {
  exception: Number
  rejection: Number
}
like.isCluster: Boolean
like.isMaster: Boolean
like.isWorker: Boolean

Methods

like.fork(env?: Object): Object
like.exit(code?: Number, worker?: Object): undefined
like.reload(code?: Number, worker?: Object): undefined
like.handle(events: Array|Object|String, callback?: Function): undefined

Examples

In the folder examples there is a lot of cases.
Also check the examples/config_and_flexibility.js.

Almost all the examples has an uncaught exception, like.reload(), etc
in that way the process will reload or exit for demonstration purposes.

Most examples uses terminate and cleanup events, examples using states:
examples/noncluster_loop.js and examples/noncluster_error.js

Examples for PM2 cluster and fork: examples/pm2_cluster_fork.js

PM2 commands

Cluster: pm2 start app.js -i 2 --wait-ready
Fork: pm2 start app.js --kill-timeout 300000

How it works?

There are too much ways to use it due cluster and more situations.
I normally handle the events separately and I use like-server obviously.

With the next code you already have all:

const like = require('like-process');
require('like-server');
const express = require('express');

const app = express();
app.get('/', (req, res) => {
  res.send('hello');
});
let server = app.listen(3000);
like.handle(server); // can attach more servers, for example, http, ws, etc

like.handle(['uncaughtException', 'unhandledRejection'], (evt, err) => {
  console.error(err);
});

You can handle more events like disconnect (cluster), beforeExit and exit.

If an event occurs then:

  • like.terminated state is setted and 'terminate' event is emitted
  • On cluster: will worker.disconnect() which also close servers
  • On non-cluster: all handled servers will server.close()
  • When all servers are closed:
  • like.cleanup state is setted and 'cleanup' event is emitted
  • Here we have the event loop empty so it really gracefully exit

Why I use like-server?

  • Servers and sockets are also treated as resources because they have:
  • At server.close() 'terminate' event and terminated state
  • On that way we can clear the event loop instantly

Tests

There are no tests yet

License

Code released under the MIT License.

like-process's People

Contributors

lukks avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  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.