Created
April 22, 2016 21:49
-
-
Save evantahler/a74f6a007f256e0c5901e3744aebc5fe to your computer and use it in GitHub Desktop.
gracefulShutdown.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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