pm2-state-helper

Abstraction for pm2 state events.

Usage no npm install needed!

<script type="module">
  import pm2StateHelper from 'https://cdn.skypack.dev/pm2-state-helper';
</script>

README

PM2-State-Helper - Exit With Grace

Intro

Simple module which allows a process running under PM2 to start and stop gracefully.

const state = require('pm2-state-helper');
const server = require('http').createServer();

// Handle stopping
state.onStopping(callback => {
    // Handle your stuff here...

    // Do stuff...
    // Do stuff...

    server.close(callback); // Finally let us know its over
});

// Notify start
server.listen('8080', () => {
    state.started();
});

Notes

onStopping also accepts a function that returns a Promise instead of one accepting the callback.