Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully quit on Ctrl+C #1477

Merged
merged 1 commit into from Aug 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/client.js
Expand Up @@ -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];

Expand All @@ -499,7 +499,10 @@ Client.prototype.quit = function() {
const socket = sockets.connected[user];

if (socket) {
socket.emit("sign-out");
if (signOut) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like an unrelated fix.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not, we don't want to sign out users on server close.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh woops, I missed that we were already sending sign-out. Sounds good.

socket.emit("sign-out");
}

socket.disconnect();
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/clientManager.js
Expand Up @@ -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`);
}
Expand Down
25 changes: 25 additions & 0 deletions src/server.js
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe another log.info in there, like "Something went wrong, force-quitting now"? Also, could do 5000 just in case, but whatever.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's necessary to add a message.


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);
});
};

Expand Down