Skip to content

Commit

Permalink
Add listeners for end, error and ready on Redis sessions
Browse files Browse the repository at this point in the history
Also add support for `onDisconnect()` and `onReconnect()` in session config
  • Loading branch information
sgress454 committed Nov 29, 2016
1 parent 44c6d9b commit 1e55c63
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/hooks/session/ensure-redis-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Module dependencies.
*/
var flaverr = require('flaverr');

var _ = require('@sailshq/lodash');

/**
* ensureRedisConnection()
Expand Down Expand Up @@ -72,6 +72,19 @@ module.exports = function ensureRedisConnection(app, cb) {
client.once('ready', function onConnectionReady(){
client.removeListener('end', onPreConnectionEnd);
client.removeListener('error', onPreConnectionError);
client.on('end', function(){
if (_.isFunction(app.config.session.onDisconnect)) {
app.config.session.onDisconnect();
}
app.log.error('Redis session server went off-line...');
});
client.on('error', function(err){app.log.verbose('Redis session server reported error: ', err.stack);});
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...');
});
return cb();
});

Expand Down

0 comments on commit 1e55c63

Please sign in to comment.