Skip to content

Commit

Permalink
Add support for onDisconnect and onReconnect in Redis clients
Browse files Browse the repository at this point in the history
  • Loading branch information
sgress454 committed Nov 28, 2016
1 parent 9b396ac commit 3a25971
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lib/ensure-redis-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ module.exports = function ensureRedisClientsLoadedSuccessfully (app, adapterConf
return;
}

// If a `sails.hooks.sockets.onDisconnect()` method is supplied, run it.
if (_.isFunction(adapterConfig.onDisconnect)) {
adapterConfig.onDisconnect();
}

// Log a message about the client being disconnected.
app.log.error('Redis socket "pub" server went off-line...');
});
Expand All @@ -93,7 +98,13 @@ module.exports = function ensureRedisClientsLoadedSuccessfully (app, adapterConf
adapterConfig.pubClient.on('error', function(err){app.log.verbose('Redis session "pub" server reported error: ', err.stack);});

// Log the "reconnect" message.
adapterConfig.pubClient.on('ready', function(err){app.log.error('Redis session "pub" server came back on-line...');});
adapterConfig.pubClient.on('ready', function(err){
// If a `sails.hooks.sockets.onReconnect()` method is supplied, run it.
if (_.isFunction(adapterConfig.onReconnect)) {
adapterConfig.onReconnect();
}
app.log.error('Redis session "pub" server came back on-line...');
});

// Log a message in silly mode indicating that the client connected.
app.log.silly('ad hoc redis client ready (pub) (%d)', app.config.port);
Expand All @@ -117,6 +128,11 @@ module.exports = function ensureRedisClientsLoadedSuccessfully (app, adapterConf
return;
}

// If a `sails.hooks.sockets.onDisconnect()` method is supplied, run it.
if (_.isFunction(adapterConfig.onDisconnect)) {
adapterConfig.onDisconnect();
}

// Log a message about the client being disconnected.
app.log.error('Redis socket "sub" server went off-line...');
});
Expand All @@ -125,7 +141,14 @@ module.exports = function ensureRedisClientsLoadedSuccessfully (app, adapterConf
adapterConfig.subClient.on('error', function(err){app.log.verbose('Redis session "sub" server reported error: ', err.stack);});

// Log the "reconnect" message.
adapterConfig.subClient.on('ready', function(err){app.log.error('Redis session "sub" server came back on-line...');});
adapterConfig.subClient.on('ready', function(err){

// If a `sails.hooks.sockets.onReconnect()` method is supplied, run it.
if (_.isFunction(adapterConfig.onReconnect)) {
adapterConfig.onReconnect();
}
app.log.error('Redis session "sub" server came back on-line...');
});

// Log a message in silly mode indicating that the client connected.
app.log.silly('ad hoc redis client ready (sub) (%d)', app.config.port);
Expand Down

0 comments on commit 3a25971

Please sign in to comment.