-
Notifications
You must be signed in to change notification settings - Fork 206
Expand file tree
/
Copy path_disposableredis.py
More file actions
146 lines (121 loc) · 4.03 KB
/
_disposableredis.py
File metadata and controls
146 lines (121 loc) · 4.03 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import subprocess
import socket
import redis
import time
import os
import sys
import itertools
from contextlib import contextmanager
REDIS_DEBUGGER = os.environ.get('REDIS_DEBUGGER', None)
REDIS_SHOW_OUTPUT = int(os.environ.get('REDIS_VERBOSE', 1 if REDIS_DEBUGGER else 0))
def get_random_port():
sock = socket.socket()
sock.listen(0)
_, port = sock.getsockname()
sock.close()
return port
class Client(redis.StrictRedis):
def __init__(self, disposable_redis, port):
redis.StrictRedis.__init__(self, port=port)
self.dr = disposable_redis
def retry_with_rdb_reload(self):
yield 1
self.dr.dump_and_reload()
yield 2
class DisposableRedis(object):
def __init__(self, port=None, path='redis-server', **extra_args):
"""
:param port: port number to start the redis server on.
Specify none to automatically generate
:type port: int|None
:param extra_args: any extra arguments kwargs will
be passed to redis server as --key val
"""
self._port = port
# this will hold the actual port the redis is listening on.
# It's equal to `_port` unless `_port` is None
# in that case `port` is randomly generated
self.port = None
self.extra_args = []
for k, v in extra_args.items():
self.extra_args.append('--%s' % k)
if isinstance(v, (list, tuple)):
self.extra_args += list(v)
else:
self.extra_args.append(v)
self.path = path
self.dumped = False
self.errored = False
def _get_output(self):
return '' if REDIS_SHOW_OUTPUT else self.process.stdout.read()
def _start_process(self):
#print("Starting redis process: {}".format(' '.join(self.args)))
if REDIS_DEBUGGER:
debugger = REDIS_DEBUGGER.split()
args = debugger + self.args
else:
args = self.args
stdout = None if REDIS_SHOW_OUTPUT else subprocess.PIPE
self.process = subprocess.Popen(
args,
stdin=sys.stdin,
stdout=stdout,
stderr=sys.stderr
)
while True:
try:
self.client().ping()
break
except redis.ConnectionError:
self.process.poll()
if self.process.returncode is not None:
raise RuntimeError(
"Process has exited with code {}\n. Redis output: {}"
.format(self.process.returncode, self._get_output()))
time.sleep(0.1)
def start(self):
"""
Start the server. To stop the server you should call stop()
accordingly
"""
if self._port is None:
self.port = get_random_port()
else:
self.port = self._port
self.dumpfile = 'dump.%s.rdb' % self.port
self.args = [self.path,
'--port', str(self.port),
'--save', '',
'--dbfilename', self.dumpfile] + self.extra_args
self._start_process()
def stop(self):
self.process.terminate()
if self.dumped:
try:
os.unlink(self.dumpfile)
except OSError:
pass
def __enter__(self):
self.start()
return self.client()
def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()
if exc_val or self.errored:
sys.stderr.write("Redis output: {}\n".format(self._get_output()))
def dump_and_reload(self):
"""
Dump the rdb and reload it, to test for serialization errors
"""
conn = self.client()
conn.save()
self.dumped = True
try:
conn.execute_command('DEBUG', 'RELOAD')
except redis.RedisError as err:
self.errored = True
raise err
def client(self):
"""
:rtype: redis.StrictRedis
"""
return Client(self, self.port)