npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

like-process

v1.1.2

Published

Handle resources and events for gracefully exit

Downloads

76

Readme

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.