Skip to content

Commit 1ef7f34

Browse files
committed
Applying review comments and fixing some issues.
1 parent f2ffe1d commit 1ef7f34

File tree

3 files changed

+59
-44
lines changed

3 files changed

+59
-44
lines changed

redis/asyncio/sentinel.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,28 +224,31 @@ async def execute_command(self, *args, **kwargs):
224224
once - If set to True, then execute the resulting command on a single
225225
node at random, rather than across the entire sentinel cluster.
226226
"""
227-
once = bool(kwargs.get("once", False))
228-
if "once" in kwargs.keys():
229-
kwargs.pop("once")
227+
once = bool(kwargs.pop("once", False))
230228

231-
# Check if command suppose to return boolean response.
232-
bool_resp = bool(kwargs.get("bool_resp", False))
233-
if "bool_resp" in kwargs.keys():
234-
kwargs.pop("bool_resp")
229+
# Check if command is supposed to return the original
230+
# responces instead of boolean value.
231+
return_responses = bool(kwargs.pop("return_responses", False))
235232

236233
if once:
237-
return await random.choice(self.sentinels).execute_command(*args, **kwargs)
234+
response = await random.choice(self.sentinels).execute_command(
235+
*args, **kwargs
236+
)
237+
if return_responses:
238+
return [response]
239+
else:
240+
return True if response else False
238241

239242
tasks = [
240243
asyncio.Task(sentinel.execute_command(*args, **kwargs))
241244
for sentinel in self.sentinels
242245
]
243246
responses = await asyncio.gather(*tasks)
244247

245-
if bool_resp:
246-
return reduce(lambda x, y: x and y, responses)
248+
if return_responses:
249+
return responses
247250

248-
return responses
251+
return reduce(lambda x, y: x and y, responses)
249252

250253
def __repr__(self):
251254
sentinel_addresses = []

redis/commands/sentinel.py

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,55 @@ def sentinel(self, *args):
1414
def sentinel_get_master_addr_by_name(self, service_name):
1515
"""Returns a (host, port) pair for the given ``service_name``"""
1616
return self.execute_command(
17-
"SENTINEL GET-MASTER-ADDR-BY-NAME", service_name, once=True
17+
"SENTINEL GET-MASTER-ADDR-BY-NAME",
18+
service_name,
19+
once=True,
20+
return_responses=True,
1821
)
1922

2023
def sentinel_master(self, service_name):
2124
"""Returns a dictionary containing the specified masters state."""
22-
return self.execute_command("SENTINEL MASTER", service_name)
25+
return self.execute_command(
26+
"SENTINEL MASTER", service_name, return_responses=True
27+
)
2328

2429
def sentinel_masters(self):
25-
"""Returns a list of dictionaries containing each master's state."""
26-
return self.execute_command("SENTINEL MASTERS", once=True)
30+
"""
31+
Returns a list of dictionaries containing each master's state.
32+
33+
Important: This function is called by the Sentinel implementation and is
34+
called directly on the Redis standalone client for sentinels,
35+
so it doesn'tsupport the "once" and "return_responses" options.
36+
"""
37+
return self.execute_command("SENTINEL MASTERS")
2738

2839
def sentinel_monitor(self, name, ip, port, quorum):
2940
"""Add a new master to Sentinel to be monitored"""
30-
return self.execute_command(
31-
"SENTINEL MONITOR", name, ip, port, quorum, bool_resp=True
32-
)
41+
return self.execute_command("SENTINEL MONITOR", name, ip, port, quorum)
3342

3443
def sentinel_remove(self, name):
3544
"""Remove a master from Sentinel's monitoring"""
36-
return self.execute_command("SENTINEL REMOVE", name, bool_resp=True)
45+
return self.execute_command("SENTINEL REMOVE", name)
3746

3847
def sentinel_sentinels(self, service_name):
3948
"""Returns a list of sentinels for ``service_name``"""
40-
return self.execute_command("SENTINEL SENTINELS", service_name)
49+
return self.execute_command(
50+
"SENTINEL SENTINELS", service_name, return_responses=True
51+
)
4152

4253
def sentinel_set(self, name, option, value):
4354
"""Set Sentinel monitoring parameters for a given master"""
44-
return self.execute_command("SENTINEL SET", name, option, value, bool_resp=True)
55+
return self.execute_command("SENTINEL SET", name, option, value)
4556

4657
def sentinel_slaves(self, service_name):
47-
"""Returns a list of slaves for ``service_name``"""
48-
return self.execute_command("SENTINEL SLAVES", service_name, once=True)
58+
"""
59+
Returns a list of slaves for ``service_name``
60+
61+
Important: This function is called by the Sentinel implementation and is
62+
called directly on the Redis standalone client for sentinels,
63+
so it doesn'tsupport the "once" and "return_responses" options.
64+
"""
65+
return self.execute_command("SENTINEL SLAVES", service_name)
4966

5067
def sentinel_reset(self, pattern):
5168
"""
@@ -56,9 +73,7 @@ def sentinel_reset(self, pattern):
5673
failover in progress), and removes every slave and sentinel already
5774
discovered and associated with the master.
5875
"""
59-
return self.execute_command(
60-
"SENTINEL RESET", pattern, once=True, bool_resp=True
61-
)
76+
return self.execute_command("SENTINEL RESET", pattern, once=True)
6277

6378
def sentinel_failover(self, new_master_name):
6479
"""
@@ -67,9 +82,7 @@ def sentinel_failover(self, new_master_name):
6782
configuration will be published so that the other Sentinels will
6883
update their configurations).
6984
"""
70-
return self.execute_command(
71-
"SENTINEL FAILOVER", new_master_name, bool_resp=True
72-
)
85+
return self.execute_command("SENTINEL FAILOVER", new_master_name)
7386

7487
def sentinel_ckquorum(self, new_master_name):
7588
"""
@@ -80,9 +93,7 @@ def sentinel_ckquorum(self, new_master_name):
8093
This command should be used in monitoring systems to check if a
8194
Sentinel deployment is ok.
8295
"""
83-
return self.execute_command(
84-
"SENTINEL CKQUORUM", new_master_name, once=True, bool_resp=True
85-
)
96+
return self.execute_command("SENTINEL CKQUORUM", new_master_name, once=True)
8697

8798
def sentinel_flushconfig(self):
8899
"""
@@ -100,7 +111,7 @@ def sentinel_flushconfig(self):
100111
This command works even if the previous configuration file is
101112
completely missing.
102113
"""
103-
return self.execute_command("SENTINEL FLUSHCONFIG", bool_resp=True)
114+
return self.execute_command("SENTINEL FLUSHCONFIG")
104115

105116

106117
class AsyncSentinelCommands(SentinelCommands):

redis/sentinel.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -255,26 +255,27 @@ def execute_command(self, *args, **kwargs):
255255
once - If set to True, then execute the resulting command on a single
256256
node at random, rather than across the entire sentinel cluster.
257257
"""
258-
once = bool(kwargs.get("once", False))
259-
if "once" in kwargs.keys():
260-
kwargs.pop("once")
258+
once = bool(kwargs.pop("once", False))
261259

262-
# Check if command suppose to return boolean response.
263-
bool_resp = bool(kwargs.get("bool_resp", False))
264-
if "bool_resp" in kwargs.keys():
265-
kwargs.pop("bool_resp")
260+
# Check if command is supposed to return the original
261+
# responces instead of boolean value.
262+
return_responses = bool(kwargs.pop("return_responses", False))
266263

267264
if once:
268-
return random.choice(self.sentinels).execute_command(*args, **kwargs)
265+
response = random.choice(self.sentinels).execute_command(*args, **kwargs)
266+
if return_responses:
267+
return [response]
268+
else:
269+
return True if response else False
269270

270271
responses = []
271272
for sentinel in self.sentinels:
272273
responses.append(sentinel.execute_command(*args, **kwargs))
273274

274-
if bool_resp:
275-
return reduce(lambda x, y: x and y, responses)
275+
if return_responses:
276+
return responses
276277

277-
return responses
278+
return reduce(lambda x, y: x and y, responses)
278279

279280
def __repr__(self):
280281
sentinel_addresses = []

0 commit comments

Comments
 (0)