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

View File

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

View File

@ -115,6 +115,31 @@ module.exports = function() {
new Identification((identHandler) => { new Identification((identHandler) => {
manager.init(identHandler, sockets); 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);
}); });
}; };