File: sentinel.py

package info (click to toggle)
python-redis 6.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,432 kB
  • sloc: python: 60,318; sh: 179; makefile: 128
file content (129 lines) | stat: -rw-r--r-- 5,299 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import warnings


class SentinelCommands:
    """
    A class containing the commands specific to redis sentinel. This class is
    to be used as a mixin.
    """

    def sentinel(self, *args):
        """Redis Sentinel's SENTINEL command."""
        warnings.warn(DeprecationWarning("Use the individual sentinel_* methods"))

    def sentinel_get_master_addr_by_name(self, service_name, return_responses=False):
        """
        Returns a (host, port) pair for the given ``service_name`` when return_responses is True,
        otherwise returns a boolean value that indicates if the command was successful.
        """
        return self.execute_command(
            "SENTINEL GET-MASTER-ADDR-BY-NAME",
            service_name,
            once=True,
            return_responses=return_responses,
        )

    def sentinel_master(self, service_name, return_responses=False):
        """
        Returns a dictionary containing the specified masters state, when return_responses is True,
        otherwise returns a boolean value that indicates if the command was successful.
        """
        return self.execute_command(
            "SENTINEL MASTER", service_name, return_responses=return_responses
        )

    def sentinel_masters(self):
        """
        Returns a list of dictionaries containing each master's state.

        Important: This function is called by the Sentinel implementation and is
        called directly on the Redis standalone client for sentinels,
        so it doesn't support the "once" and "return_responses" options.
        """
        return self.execute_command("SENTINEL MASTERS")

    def sentinel_monitor(self, name, ip, port, quorum):
        """Add a new master to Sentinel to be monitored"""
        return self.execute_command("SENTINEL MONITOR", name, ip, port, quorum)

    def sentinel_remove(self, name):
        """Remove a master from Sentinel's monitoring"""
        return self.execute_command("SENTINEL REMOVE", name)

    def sentinel_sentinels(self, service_name, return_responses=False):
        """
        Returns a list of sentinels for ``service_name``, when return_responses is True,
        otherwise returns a boolean value that indicates if the command was successful.
        """
        return self.execute_command(
            "SENTINEL SENTINELS", service_name, return_responses=return_responses
        )

    def sentinel_set(self, name, option, value):
        """Set Sentinel monitoring parameters for a given master"""
        return self.execute_command("SENTINEL SET", name, option, value)

    def sentinel_slaves(self, service_name):
        """
        Returns a list of slaves for ``service_name``

        Important: This function is called by the Sentinel implementation and is
        called directly on the Redis standalone client for sentinels,
        so it doesn't support the "once" and "return_responses" options.
        """
        return self.execute_command("SENTINEL SLAVES", service_name)

    def sentinel_reset(self, pattern):
        """
        This command will reset all the masters with matching name.
        The pattern argument is a glob-style pattern.

        The reset process clears any previous state in a master (including a
        failover in progress), and removes every slave and sentinel already
        discovered and associated with the master.
        """
        return self.execute_command("SENTINEL RESET", pattern, once=True)

    def sentinel_failover(self, new_master_name):
        """
        Force a failover as if the master was not reachable, and without
        asking for agreement to other Sentinels (however a new version of the
        configuration will be published so that the other Sentinels will
        update their configurations).
        """
        return self.execute_command("SENTINEL FAILOVER", new_master_name)

    def sentinel_ckquorum(self, new_master_name):
        """
        Check if the current Sentinel configuration is able to reach the
        quorum needed to failover a master, and the majority needed to
        authorize the failover.

        This command should be used in monitoring systems to check if a
        Sentinel deployment is ok.
        """
        return self.execute_command("SENTINEL CKQUORUM", new_master_name, once=True)

    def sentinel_flushconfig(self):
        """
        Force Sentinel to rewrite its configuration on disk, including the
        current Sentinel state.

        Normally Sentinel rewrites the configuration every time something
        changes in its state (in the context of the subset of the state which
        is persisted on disk across restart).
        However sometimes it is possible that the configuration file is lost
        because of operation errors, disk failures, package upgrade scripts or
        configuration managers. In those cases a way to to force Sentinel to
        rewrite the configuration file is handy.

        This command works even if the previous configuration file is
        completely missing.
        """
        return self.execute_command("SENTINEL FLUSHCONFIG")


class AsyncSentinelCommands(SentinelCommands):
    async def sentinel(self, *args) -> None:
        """Redis Sentinel's SENTINEL command."""
        super().sentinel(*args)