Merge pull request #1477 from thelounge/xpaw/graceful-quit

Gracefully quit on Ctrl+C
This commit is contained in:
Pavel Djundik 2017-08-31 22:12:08 +03:00 committed by GitHub
commit 271948b0fb
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);
}); });
}; };