@@ -24,36 +24,43 @@ public class RedisSentinel : IRedisSentinel
2424 private int sentinelIndex = - 1 ;
2525 private List < string > sentinels ;
2626 private RedisSentinelWorker worker ;
27- internal IRedisClientsManager redisManager ;
2827 private static int MaxFailures = 5 ;
28+
29+ public IRedisClientsManager RedisManager { get ; set ; }
2930 public Action < IRedisClientsManager > OnFailover { get ; set ; }
30- public Action OnWorkerError { get ; set ; }
31- public Action < string , string > OnSentinelMessageReceived { get ; set ; }
31+ public Action < Exception > OnWorkerError { get ; set ; }
32+ public Action < string , string > OnSentinelMessageReceived { get ; set ; }
33+
34+ public RedisSentinel ( string sentinelHost , string sentinelName )
35+ : this ( new [ ] { sentinelHost } , sentinelName ) { }
3236
3337 public RedisSentinel ( IEnumerable < string > sentinelHosts , string sentinelName )
3438 {
3539 this . sentinels = sentinelHosts != null ? sentinelHosts . ToList ( ) : null ;
36- if ( sentinelHosts == null || sentinels . Count == 0 )
40+ if ( sentinelHosts == null || sentinels . Count == 0 )
3741 throw new ArgumentException ( "sentinels must have at least one entry" ) ;
3842
3943 this . sentinelName = sentinelName ;
4044 this . RedisManagerFactory = new RedisManagerFactory ( ) ;
4145 }
4246
47+ [ Obsolete ( "Use Start()" ) ]
48+ public IRedisClientsManager Setup ( )
49+ {
50+ return Start ( ) ;
51+ }
52+
4353 /// <summary>
44- /// Initialize channel and register client manager
54+ /// Initialize Sentinel Subscription and Configure Redis ClientsManager
4555 /// </summary>
46- /// <param name="container"></param>
47- public IRedisClientsManager Setup ( )
56+ public IRedisClientsManager Start ( )
4857 {
4958 GetValidSentinel ( ) ;
5059
51- if ( this . redisManager == null )
52- {
60+ if ( this . RedisManager == null )
5361 throw new ApplicationException ( "Unable to resolve sentinels!" ) ;
54- }
5562
56- return this . redisManager ;
63+ return this . RedisManager ;
5764 }
5865
5966 public Func < string , string > HostFilter { get ; set ; }
@@ -75,23 +82,23 @@ private RedisSentinelWorker GetValidSentinel()
7582
7683 RedisException lastEx = null ;
7784
78- while ( this . redisManager == null && ShouldRetry ( ) )
85+ while ( this . RedisManager == null && ShouldRetry ( ) )
7986 {
8087 try
8188 {
8289 this . worker = GetNextSentinel ( ) ;
83- this . redisManager = worker . GetClientManager ( ) ;
90+ this . RedisManager = worker . GetClientManager ( ) ;
8491 this . worker . BeginListeningForConfigurationChanges ( ) ;
8592 return this . worker ;
8693 }
8794 catch ( RedisException ex )
8895 {
96+ if ( OnWorkerError != null )
97+ OnWorkerError ( ex ) ;
98+
8999 lastEx = ex ;
90100 if ( this . worker != null )
91- {
92- this . worker . SentinelError -= Worker_SentinelError ;
93101 this . worker . Dispose ( ) ;
94- }
95102
96103 this . failures ++ ;
97104 }
@@ -115,13 +122,13 @@ private RedisSentinelWorker GetNextSentinel()
115122 sentinelIndex ++ ;
116123
117124 if ( sentinelIndex >= sentinels . Count )
118- {
119125 sentinelIndex = 0 ;
120- }
121126
122- var sentinelWorker = new RedisSentinelWorker ( this , sentinels [ sentinelIndex ] , this . sentinelName ) ;
127+ var sentinelWorker = new RedisSentinelWorker ( this , sentinels [ sentinelIndex ] , this . sentinelName )
128+ {
129+ OnSentinelError = OnSentinelError
130+ } ;
123131
124- sentinelWorker . SentinelError += Worker_SentinelError ;
125132 return sentinelWorker ;
126133 }
127134
@@ -130,21 +137,17 @@ private RedisSentinelWorker GetNextSentinel()
130137 /// </summary>
131138 /// <param name="sender"></param>
132139 /// <param name="e"></param>
133- private void Worker_SentinelError ( object sender , EventArgs e )
140+ private void OnSentinelError ( Exception ex )
134141 {
135- var worker = sender as RedisSentinelWorker ;
136-
137- if ( worker != null )
142+ if ( this . worker != null )
138143 {
139- Log . Info ( "Error on existing SentinelWorker, reconnecting..." ) ;
144+ Log . Error ( "Error on existing SentinelWorker, reconnecting..." ) ;
145+
140146 if ( OnWorkerError != null )
141- {
142- OnWorkerError ( ) ;
143- }
147+ OnWorkerError ( ex ) ;
144148
145149 // dispose the worker
146- worker . SentinelError -= Worker_SentinelError ;
147- worker . Dispose ( ) ;
150+ this . worker . Dispose ( ) ;
148151
149152 // get a new worker and start looking for more changes
150153 this . worker = GetNextSentinel ( ) ;
@@ -154,15 +157,13 @@ private void Worker_SentinelError(object sender, EventArgs e)
154157
155158 public SentinelInfo FailoverToSentinelHosts ( )
156159 {
157- var worker = GetValidSentinel ( ) ;
158- return worker . ConfigureRedisFromSentinel ( ) ;
160+ return GetValidSentinel ( ) . ConfigureRedisFromSentinel ( ) ;
159161 }
160162
161163 public void Dispose ( )
162164 {
163165 if ( worker != null )
164166 {
165- worker . SentinelError -= Worker_SentinelError ;
166167 worker . Dispose ( ) ;
167168 worker = null ;
168169 }
@@ -178,7 +179,7 @@ public class SentinelInfo
178179 public SentinelInfo ( List < string > redisMasters , List < string > redisSlaves )
179180 {
180181 RedisMasters = redisMasters != null ? redisMasters . ToArray ( ) : new string [ 0 ] ;
181- RedisSlaves = redisSlaves != null ? RedisSlaves . ToArray ( ) : new string [ 0 ] ;
182+ RedisSlaves = redisSlaves != null ? redisSlaves . ToArray ( ) : new string [ 0 ] ;
182183 }
183184
184185 public override string ToString ( )
0 commit comments