Skip to content

Commit

Permalink
Disconnect Redis session client when Sails is lowering.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Nov 30, 2016
1 parent 5c3814d commit 80fb71b
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/hooks/session/ensure-redis-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,31 @@ module.exports = function ensureRedisConnection(app, cb) {

// Bind a "ready" listener so that we know when the client has connected.
client.once('ready', function onConnectionReady(){
// Remove the pre-connection handlers.
client.removeListener('end', onPreConnectionEnd);
client.removeListener('error', onPreConnectionError);
// Add a new "end" handler.
client.on('end', function(){
if (_.isFunction(app.config.session.onDisconnect)) {
app.config.session.onDisconnect();
}
app.log.error('Redis session server went off-line...');
});
// Add a new "error" handler.
client.on('error', function(err){app.log.verbose('Redis session server reported error: ', err.stack);});
// Add a new "ready" handler that will be triggered on a reconnect.
client.on('ready', function(err){
if (_.isFunction(app.config.session.onReconnect)) {
app.config.session.onReconnect();
}
app.log.error('Redis session server came back on-line...');
});

// When Sails is lowering, disconnect the Redis client so that the process doesn't hang.
app.on('lower', function() {
client.end(true);
});

return cb();
});

Expand Down

0 comments on commit 80fb71b

Please sign in to comment.