diff --git a/lib/hooks/session/ensure-redis-connection.js b/lib/hooks/session/ensure-redis-connection.js index 6ce2cf81e..51d7d0940 100644 --- a/lib/hooks/session/ensure-redis-connection.js +++ b/lib/hooks/session/ensure-redis-connection.js @@ -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(); });