Gracefully quit on Ctrl+C

Fixes #268
This commit is contained in:
Pavel Djundik 2017-08-30 20:26:45 +03:00
parent 5821247b3d
commit 0d57df81af
3 changed files with 31 additions and 3 deletions

View file

@ -490,7 +490,7 @@ Client.prototype.names = function(data) {
});
};
Client.prototype.quit = function() {
Client.prototype.quit = function(signOut) {
const sockets = this.sockets.sockets;
const room = sockets.adapter.rooms[this.id];
@ -499,7 +499,10 @@ Client.prototype.quit = function() {
const socket = sockets.connected[user];
if (socket) {
socket.emit("sign-out");
if (signOut) {
socket.emit("sign-out");
}
socket.disconnect();
}
}

View file

@ -62,7 +62,7 @@ ClientManager.prototype.autoloadUsers = function() {
_.difference(loaded, updatedUsers).forEach((name) => {
const client = _.find(this.clients, {name: name});
if (client) {
client.quit();
client.quit(true);
this.clients = _.without(this.clients, client);
log.info(`User ${colors.bold(name)} disconnected and removed`);
}

View file

@ -115,6 +115,31 @@ module.exports = function() {
new Identification((identHandler) => {
manager.init(identHandler, sockets);
});
// Handle ctrl+c and kill gracefully
let suicideTimeout = null;
const exitGracefully = function() {
if (suicideTimeout !== null) {
return;
}
// Forcefully exit after 3 seconds
suicideTimeout = setTimeout(() => process.exit(1), 3000);
log.info("Exiting...");
// Close all client and IRC connections
manager.clients.forEach((client) => client.quit());
// Close http server
server.close(() => {
clearTimeout(suicideTimeout);
process.exit(0);
});
};
process.on("SIGINT", exitGracefully);
process.on("SIGTERM", exitGracefully);
});
};