Skip to content

Commit 3081a32

Browse files
dvora-hchayim
andauthored
SHUTDOWN - add support for the new NOW, FORCE and ABORT modifiers (#2150)
* add support for NOW, FORCE and ABORT modifiers * linters * test * linters * test params * fix tests Co-authored-by: Chayim <[email protected]>
1 parent fa0be76 commit 3081a32

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

redis/commands/core.py

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,12 +1153,25 @@ def save(self, **kwargs) -> ResponseT:
11531153
"""
11541154
return self.execute_command("SAVE", **kwargs)
11551155

1156-
def shutdown(self, save: bool = False, nosave: bool = False, **kwargs) -> None:
1156+
def shutdown(
1157+
self,
1158+
save: bool = False,
1159+
nosave: bool = False,
1160+
now: bool = False,
1161+
force: bool = False,
1162+
abort: bool = False,
1163+
**kwargs,
1164+
) -> None:
11571165
"""Shutdown the Redis server. If Redis has persistence configured,
1158-
data will be flushed before shutdown. If the "save" option is set,
1159-
a data flush will be attempted even if there is no persistence
1160-
configured. If the "nosave" option is set, no data flush will be
1161-
attempted. The "save" and "nosave" options cannot both be set.
1166+
data will be flushed before shutdown.
1167+
It is possible to specify modifiers to alter the behavior of the command:
1168+
``save`` will force a DB saving operation even if no save points are configured.
1169+
``nosave`` will prevent a DB saving operation even if one or more save points
1170+
are configured.
1171+
``now`` skips waiting for lagging replicas, i.e. it bypasses the first step in
1172+
the shutdown sequence.
1173+
``force`` ignores any errors that would normally prevent the server from exiting
1174+
``abort`` cancels an ongoing shutdown and cannot be combined with other flags.
11621175
11631176
For more information see https://redis.io/commands/shutdown
11641177
"""
@@ -1169,6 +1182,12 @@ def shutdown(self, save: bool = False, nosave: bool = False, **kwargs) -> None:
11691182
args.append("SAVE")
11701183
if nosave:
11711184
args.append("NOSAVE")
1185+
if now:
1186+
args.append("NOW")
1187+
if force:
1188+
args.append("FORCE")
1189+
if abort:
1190+
args.append("ABORT")
11721191
try:
11731192
self.execute_command(*args, **kwargs)
11741193
except ConnectionError:
@@ -1279,7 +1298,13 @@ async def memory_help(self, **kwargs) -> None:
12791298
return super().memory_help(**kwargs)
12801299

12811300
async def shutdown(
1282-
self, save: bool = False, nosave: bool = False, **kwargs
1301+
self,
1302+
save: bool = False,
1303+
nosave: bool = False,
1304+
now: bool = False,
1305+
force: bool = False,
1306+
abort: bool = False,
1307+
**kwargs,
12831308
) -> None:
12841309
"""Shutdown the Redis server. If Redis has persistence configured,
12851310
data will be flushed before shutdown. If the "save" option is set,
@@ -1296,6 +1321,12 @@ async def shutdown(
12961321
args.append("SAVE")
12971322
if nosave:
12981323
args.append("NOSAVE")
1324+
if now:
1325+
args.append("NOW")
1326+
if force:
1327+
args.append("FORCE")
1328+
if abort:
1329+
args.append("ABORT")
12991330
try:
13001331
await self.execute_command(*args, **kwargs)
13011332
except ConnectionError:

tests/test_commands.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import time
55
from string import ascii_letters
6+
from unittest import mock
67

78
import pytest
89

@@ -4631,6 +4632,19 @@ def test_replicaof(self, r):
46314632
assert r.replicaof("NO ONE")
46324633
assert r.replicaof("NO", "ONE")
46334634

4635+
def test_shutdown(self, r: redis.Redis):
4636+
r.execute_command = mock.MagicMock()
4637+
r.execute_command("SHUTDOWN", "NOSAVE")
4638+
r.execute_command.assert_called_once_with("SHUTDOWN", "NOSAVE")
4639+
4640+
@skip_if_server_version_lt("7.0.0")
4641+
def test_shutdown_with_params(self, r: redis.Redis):
4642+
r.execute_command = mock.MagicMock()
4643+
r.execute_command("SHUTDOWN", "SAVE", "NOW", "FORCE")
4644+
r.execute_command.assert_called_once_with("SHUTDOWN", "SAVE", "NOW", "FORCE")
4645+
r.execute_command("SHUTDOWN", "ABORT")
4646+
r.execute_command.assert_called_with("SHUTDOWN", "ABORT")
4647+
46344648
@pytest.mark.replica
46354649
@skip_if_server_version_lt("2.8.0")
46364650
@skip_if_redis_enterprise()

0 commit comments

Comments
 (0)