From 80fb71b45fa7dab94f4c6054b6d365a9319150f7 Mon Sep 17 00:00:00 2001 From: Scott Gress Date: Wed, 30 Nov 2016 13:59:11 -0600 Subject: [PATCH] Disconnect Redis session client when Sails is lowering. --- lib/hooks/session/ensure-redis-connection.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/hooks/session/ensure-redis-connection.js b/lib/hooks/session/ensure-redis-connection.js index 6ce2cf81e7..51d7d0940f 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(); });