Skip to content

Instantly share code, notes, and snippets.

@shurane
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shurane/13cca4b4fff5b7b54dbd to your computer and use it in GitHub Desktop.
Save shurane/13cca4b4fff5b7b54dbd to your computer and use it in GitHub Desktop.
PM2-test-graceful-exit

Test the handlers around process.on('SIGTERM') for pm2 restart|stop|kill.

Run the following commands in separate shells:

  1. pm2 start index.json
  2. while true; do (curl localhost:5001 -m 7 2>/dev/null &) ; sleep 10; done

And trigger conditions with pm2 restart|stop|kill

var express = require('express');
var moment = require('moment');
var faker = require('faker');
var _ = require('lodash');
var app = express();
app.all('*', function (req, res) {
var word = faker.hacker.noun();
var t1 = moment();
console.log(process.pid, word, 'received request', t1.format('hh:mm:ss'));
setTimeout(function(){
var t2 = moment();
console.log(process.pid, word, 'sent response', t2.format('hh:mm:ss'));
res.send(JSON.stringify({
t1: t1.format('hh:mm:ss'),
t2: t2.format('hh:mm:ss'),
word: word,
duration: moment.duration(t2.diff(t1)).asSeconds(),
pid: process.pid
}) + '\n');
}, 6000);
});
var server = app.listen(5001, function () {
var host = server.address().address;
var port = server.address().port;
});
process.on('message', function(event) {
console.log('message:shutdown()');
var clog = _.partial(console.log, process.pid, 'message', event);
if (msg == 'shutdown') {
clog(moment().format('hh:mm:ss'));
// You will have 4000ms to close all connections before
// the reload mechanism will try to do its job
setTimeout(function() {
clog('Finished closing connections', moment().format('hh:mm:ss'));
// This timeout means that all connections have been closed
// Now we can exit to let the reload mechanism do its job
process.exit(0);
}, 5000);
}
});
function cleanup(event) {
console.log('cleanup()', process.pid, event);
var clog = _.partial(console.log, process.pid, event);
clog('cleanup()', moment().format('hh:mm:ss'));
server.close(function() {
clog( 'cleanup() -- server.close()', moment().format('hh:mm:ss'));
setTimeout(function() {
clog('cleanup() -- server.close()', 'after setTimeout', moment().format('hh:mm:ss'));
process.exit(0);
}, 2000);
});
}
process.once('SIGTERM', _.partial(cleanup, 'SIGTERM'));
process.once('SIGINT', _.partial(cleanup, 'SIGINT'));
[{
"script":"index.js",
"name":"index",
"instances":1,
"error_file":"errfile.log",
"out_file":"outfile.log",
"pid_file":"pidfile.pid"
}]
var http = require('http');
var moment = require('moment');
console.log('pid:', process.pid);
process.stdin.resume();
console.log('reading');
//http.createServer(function(req, res) {
//res.writeHead(200);
//res.end("hello world\n");
//}).listen(8020);
process.on('SIGTERM', function() {
console.log('Sigterm called', moment().format('hh:mm:ss'));
setTimeout(function() {
console.log('Sigterm setTimeout called', moment().format('hh:mm:ss'));
process.exit(1);
}, 20000);
});
process.on('uncaughtException', function(err) {
console.log('LOL, what happened?', moment().format('hh:mm:ss'));
process.exit(0);
//setTimeout(function() { process.exit(0) })
});
var pm2 = require('pm2');
var chalk = require('chalk');
console.log(chalk.red(pm2.reload.toString()));
console.log(chalk.blue(pm2.gracefulReload.toString()));
console.log(chalk.green(pm2.stop.toString()));
console.log(chalk.cyan(pm2.delete.toString()));
// Start reading from stdin so we don't exit.
var moment = require('moment');
process.stdin.resume();
console.log('reading');
process.once('SIGTERM', function () {
console.log('SIGTERM initial', moment().format('hh:mm:ss'));
setTimeout(function() {
console.log('SIGTERM exiting', moment().format('hh:mm:ss'));
process.exit(0);
}, 6000);
});
process.once('SIGINT', function () {
console.log('SIGINT initial', moment().format('hh:mm:ss'));
setTimeout(function() {
console.log('SIGINT exiting', moment().format('hh:mm:ss'));
process.exit(0);
}, 6000);
});
[{
"script":"test.js",
"name":"test",
"instances":1,
"error_file":"errfile-test.log",
"out_file":"outfile-test.log",
"pid_file":"pidfile-test.pid"
}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment