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

Cleanup on console closed & log error.stack #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,30 +191,39 @@ function adapter(uri, opts){

// Set up exit handlers so we can clean up this process's redis data before exiting

process.stdin.resume(); //so the program will not close instantly
function exitHandler(options, err){
function exitHandler(error){
var i;
var multi = data.multi();
var execDone = false;

var roomIds = Object.keys(self.rooms);
var socketIds = Object.keys(self.sids);

for(i=0; i<roomIds.length; ++i){
debug('Remove from room key "%s" clients: ', prefix + '#' + roomIds[i], Object.keys(self.rooms[roomIds[i]]));
multi.srem(prefix + '#' + roomIds[i], Object.keys(self.rooms[roomIds[i]]));
}
for(i=0; i<socketIds.length; ++i){
debug('Remove from client key "%s" rooms: ', prefix + '#' + socketIds[i], Object.keys(self.sids[socketIds[i]]));
multi.srem(prefix + '#' + socketIds[i], Object.keys(self.sids[socketIds[i]]));
}
multi.exec(function(err, replies){
debug(replies);
error && console.error(error.stack);
process.exit();
});
}

// //do something when app is closing
// process.on('exit', exitHandler.bind(null,{cleanup:true}));
//do something when app is closing

// SIGTERM and SIGINT have default handlers on non-Windows platforms that resets the terminal mode before exiting with code 128 + signal number.
// If one of these signals has a listener installed, its default behaviour will be removed (node will no longer exit).
process.on('SIGTERM', exitHandler);
process.on('SIGINT', exitHandler);
//generated on Windows when the console window is closed and on other platforms under various similar conditions
process.on('SIGHUP', exitHandler);
process.on('SIGQUIT', exitHandler);
//Custom error handler
process.on('uncaughtException', exitHandler);


Expand Down