Skip to content

Instantly share code, notes, and snippets.

@evantahler
Created April 22, 2016 21:49
Show Gist options
  • Save evantahler/a74f6a007f256e0c5901e3744aebc5fe to your computer and use it in GitHub Desktop.
Save evantahler/a74f6a007f256e0c5901e3744aebc5fe to your computer and use it in GitHub Desktop.
gracefulShutdown.js
api.socketServer.gracefulShutdown = function(api, next, alreadyShutdown){
if(alreadyShutdown == null){alreadyShutdown = false;}
if(alreadyShutdown == false){
api.socketServer.server.close();
alreadyShutdown = true;
}
for(var i in api.socketServer.connections){
var connection = api.socketServer.connections[i];
if (connection.responsesWaitingCount == 0){
api.socketServer.connections[i].end("Server going down NOW\r\nBye!\r\n");
}
}
if(api.socketServer.connections.length != 0){
api.log("[socket] waiting on shutdown, there are still " + api.socketServer.connections.length + " connected clients waiting on a response");
setTimeout(function(){
api.socketServer.gracefulShutdown(api, next, alreadyShutdown);
}, 3000);
}else{
next();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment