@@ -4,11 +4,11 @@ module.exports = function (sails) {
4
4
* Module dependencies.
5
5
*/
6
6
7
- var util = require ( 'sails-util' ) ,
7
+ var util = require ( 'sails-util' ) ,
8
8
SocketServer = require ( 'socket.io' ) ,
9
9
RedisStore = require ( 'socket.io/lib/stores/redis' ) ,
10
- Redis = require ( 'socket.io/node_modules/redis' ) ,
11
- Socket = {
10
+ Redis = require ( 'socket.io/node_modules/redis' ) ,
11
+ Socket = {
12
12
authorization : require ( './authorization' ) ( sails ) ,
13
13
connection : require ( './connection' ) ( sails )
14
14
} ;
@@ -66,9 +66,9 @@ module.exports = function (sails) {
66
66
var host = socketConfig . host || '127.0.0.1' ;
67
67
var port = socketConfig . port || 6379 ;
68
68
69
- var pub = createRedisConnection ( port , host ) ;
70
- var sub = createRedisConnection ( port , host ) ;
71
- var client = createRedisConnection ( port , host ) ;
69
+ var pub = createRedisConnection ( port , host , 'pub' ) ;
70
+ var sub = createRedisConnection ( port , host , 'sub' ) ;
71
+ var client = createRedisConnection ( port , host , 'client' ) ;
72
72
73
73
var storeConfig = {
74
74
redisPub : pub ,
@@ -136,7 +136,7 @@ module.exports = function (sails) {
136
136
* `pass` is declared in the socketConfig file.
137
137
*/
138
138
139
- function createRedisConnection ( port , host ) {
139
+ function createRedisConnection ( port , host , id ) {
140
140
141
141
var socketConfig = sails . config . sockets ;
142
142
@@ -155,6 +155,24 @@ module.exports = function (sails) {
155
155
client . select ( socketConfig . db ) ;
156
156
}
157
157
158
+ // If Redis connection ends, catch the error and retry
159
+ // until it comes back
160
+
161
+ client . on ( 'ready' , function ( ) {
162
+ sails . log . debug ( 'RedisClient::Events[ready]: [OK] Redis "' + id + '" is up. Connections: ' , client . connections ) ;
163
+ } ) ;
164
+
165
+ client . on ( 'end' , function ( ) {
166
+ sails . log . debug ( 'RedisClient::Events[end]: "' + id + '" , Connected:' , client . connected ) ;
167
+ } ) ;
168
+
169
+ client . on ( 'error' , function ( err ) {
170
+ sails . log . error ( 'RedisClient::Events[error]: "' + id + '" , ' + err ) ;
171
+ if ( / E C O N N R E F U S E D / g. test ( err ) ) {
172
+ sails . log . error ( 'Waiting for "' + id + '" redis client to come back online. Connections:' , client . connections ) ;
173
+ }
174
+ } ) ;
175
+
158
176
return client ;
159
177
}
160
178
0 commit comments