Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHUTDOWN - add support for the new NOW, FORCE and ABORT modifiers #2150

Merged
merged 7 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1109,12 +1109,25 @@ def save(self, **kwargs) -> ResponseT:
"""
return self.execute_command("SAVE", **kwargs)

def shutdown(self, save: bool = False, nosave: bool = False, **kwargs) -> None:
def shutdown(
self,
save: bool = False,
nosave: bool = False,
now: bool = False,
force: bool = False,
abort: bool = False,
**kwargs,
) -> None:
"""Shutdown the Redis server. If Redis has persistence configured,
data will be flushed before shutdown. If the "save" option is set,
a data flush will be attempted even if there is no persistence
configured. If the "nosave" option is set, no data flush will be
attempted. The "save" and "nosave" options cannot both be set.
data will be flushed before shutdown.
It is possible to specify modifiers to alter the behavior of the command:
``save`` will force a DB saving operation even if no save points are configured.
``nosave`` will prevent a DB saving operation even if one or more save points
are configured.
``now`` skips waiting for lagging replicas, i.e. it bypasses the first step in
the shutdown sequence.
``force`` ignores any errors that would normally prevent the server from exiting
``abort`` cancels an ongoing shutdown and cannot be combined with other flags.

For more information see https://redis.io/commands/shutdown
"""
Expand All @@ -1125,6 +1138,12 @@ def shutdown(self, save: bool = False, nosave: bool = False, **kwargs) -> None:
args.append("SAVE")
if nosave:
args.append("NOSAVE")
if now:
args.append("NOW")
if force:
args.append("FORCE")
if abort:
args.append("ABORT")
try:
self.execute_command(*args, **kwargs)
except ConnectionError:
Expand Down Expand Up @@ -1235,7 +1254,13 @@ async def memory_help(self, **kwargs) -> None:
return super().memory_help(**kwargs)

async def shutdown(
self, save: bool = False, nosave: bool = False, **kwargs
self,
save: bool = False,
nosave: bool = False,
now: bool = False,
force: bool = False,
abort: bool = False,
**kwargs,
) -> None:
"""Shutdown the Redis server. If Redis has persistence configured,
data will be flushed before shutdown. If the "save" option is set,
Expand All @@ -1252,6 +1277,12 @@ async def shutdown(
args.append("SAVE")
if nosave:
args.append("NOSAVE")
if now:
args.append("NOW")
if force:
args.append("FORCE")
if abort:
args.append("ABORT")
try:
await self.execute_command(*args, **kwargs)
except ConnectionError:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import time
from string import ascii_letters
from unittest import mock

import pytest

Expand Down Expand Up @@ -4633,6 +4634,18 @@ def test_replicaof(self, r):
assert r.replicaof("NO ONE")
assert r.replicaof("NO", "ONE")

def test_shutdown(self):
r = redis.Redis(port=6380)
r.shutdown = mock.MagicMock()
r.shutdown()
r.shutdown.assert_called_once()

@skip_if_server_version_lt("7.0.0")
def test_shutdown_with_params(self):
r = redis.Redis(port=6380)
r.shutdown(save=True, now=True, force=True, abort=False)
r.shutdown.assert_called_with(save=True, now=True, force=True, abort=False)

@pytest.mark.replica
@skip_if_server_version_lt("2.8.0")
@skip_if_redis_enterprise()
Expand Down