GithubHelp home page GithubHelp logo

chenjianniu / terminate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dwyl/terminate

0.0 0.0 0.0 422 KB

:recycle: Terminate a Node.js Process (and all Child Processes) based on the Process ID

License: GNU General Public License v2.0

JavaScript 100.00%

terminate's Introduction

terminate

A minimalist yet reliable (tested) way to Terminate a Node.js Process (and all Child Processes) based on the Process ID

terminate-the-node-process-final

Build Status codecov.io npm version Node.js Version Dependency Status JavaScript Style Guide: Good Parts

Usage

Install from NPM

## Using NPM
npm install terminate --save

In your script

const terminate = require('terminate');

terminate(process.pid, function (err) {
  if (err) { // you will get an error if you did not supply a valid process.pid
    console.log('Oopsy:', err); // handle errors in your preferred way.
  }
  else {
    console.log('done'); // terminating the Processes succeeded.
    // NOTE: The above won't be run in this example as the process itself will be killed before.
  }
});

Usage

All the available parameters and their descriptions are viewable in the TypeScript definition file: index.d.ts.

Promise API

If you are using Node.js 8+, you can load the Promise version importing the module terminate/promise.js like this:

const terminate = require('terminate/promise');

(async () => {
  try {
    await terminate(process.pid);
    console.log('done');
  } catch (err) {
    console.log('Oopsy:', err);
  }
})();

tl;dr

Why?

In our Faster project we run the server using Child Process(es). When we want to re-start the server,
we needed to ensure the process (and any Child Processes) were Terminated before attempting to re-start.

Prevents Zombie Processes

Imagine you are running your Node.js app on a Multi-Core Processor and don't want to "waste" the CPU by only using one of those cores
(remember: one of Node's selling points is that its single-process running on a single-core), you can "farm out" tasks to the other cores using Child Process(es)

If you then want to restart your app you naively (we did this!) terminate the "main" process and expect all the "child" processes to be ended too.

Sadly this results in Zombie Processes which you would have to either manually kill or restart the machine/VM to clear.
Instead you need to find all the Process IDs for the child processes that your app created and terminate those before you can re-start your app.

Using terminate you no longer have this problem.


Dependencies Dependency Status devDependency Status Retire Status

While researching how to get a list of child processes given a Process ID we found ps-tree: https://github.com/indexzero/ps-tree
it was un-maintained and had no tests so we submitted an issue offering to update the module with tests.
Charlie replied welcoming an update so we submitted a Pull Request with 100% test coverage which was Merged by Charlie
ps-tree in turn uses event-stream (by Dominc Tarr) for streaming the result of its process lookups.
event-stream does not publish its' code coverage, but is tested (and more importantly used by millions of people)
has had many itterations and has been stable for a while:

NPM

While we don't like the fact that event-stream has dependencies which have limited automated tests,
we are willing to place reliance on the module given how "real-world tested" it is.

If at any point we find that relying event-stream or its underlying dependencies is the source of a bug or sleepless nights,
we can either make time to contribute the tests or write our own version of ps-tree without dependencies
(which should "only" take a day but won't be as elegant...)

Research

Background Reading

Useful StackOverflow Questions/Answers

Other potential modules we could have used:

Name

Terminate seemed like the best (of the available) name,
if you have a better suggestion, please share!



## This Project Reminded me of Two *xkcd* Comics:

xkcd terminate

xkcd time robot

terminate's People

Contributors

adrianblynch avatar asakusuma avatar benjaminlees avatar cono52 avatar dependabot[bot] avatar iteles avatar jarofghosts avatar jedwards1211 avatar jpunt avatar micalevisk avatar mitchellporter avatar morigs avatar nelsonic 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.