Completed
Push — master ( 8205ad...0b3f02 )
by Andrew
31s
created

patch_smembers()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
1
import logging
2
3
import redis
4
import tornadoredis
5
6
from chat.settings import ALL_REDIS_ROOM, TORNADO_REDIS_PORT
7
8
logger = logging.getLogger(__name__)
9
10
11
def new_read(instance, *args, **kwargs):
12
	try:
13
		return instance.old_read(*args, **kwargs)
14
	except Exception as e:
15
		redis_online = sync_redis.hgetall(ALL_REDIS_ROOM)
16
		logger.error("Error occurred during reading tornadoredis connection. Redis online  %s", redis_online)
17
		raise e
18
19
20
def patch_read(tornado_redis):
21
	fabric = type(tornado_redis.connection.read)
22
	tornado_redis.connection.old_read = tornado_redis.connection.read
23
	tornado_redis.connection.read = fabric(new_read, tornado_redis.connection)
24
25
26
def new_hget(instance, *args, **kwargs):
27
	res = instance.hget(*args, **kwargs)
28
	return res.decode('utf-8') if res else None
29
30
31
def patch_hget(arg_red):
32
	fabric = type(arg_red.hget)
33
	arg_red.shget = fabric(new_hget, arg_red)
34
35
36
def patch_hgetall(arg_red):
37
	fabric = type(arg_red.hgetall)
38
	arg_red.shgetall = fabric(new_hgetall, arg_red)
39
40
41
def new_hgetall(instance, *args, **kwargs):
42
	res = instance.hgetall(*args, **kwargs) # neither key or value are null
43
	return {k.decode('utf-8'): res[k].decode('utf-8') for k in res}
44
45
46
def patch_smembers(arg_red):
47
	fabric = type(arg_red.smembers)
48
	arg_red.ssmembers = fabric(new_smembers, arg_red)
49
50
51
def new_smembers(instance, *args, **kwargs):
52
	res = instance.smembers(*args, **kwargs) # neither key or value are null
53
	return [k.decode('utf-8') for k in res]
54
55
56
# # global connection to read synchronously
57
sync_redis = redis.StrictRedis(port=TORNADO_REDIS_PORT)
58
patch_hget(sync_redis)
59
patch_hgetall(sync_redis)
60
patch_smembers(sync_redis)
61
# patch(sync_redis)
62
# Redis connection cannot be shared between publishers and subscribers.
63
async_redis_publisher = tornadoredis.Client(port=TORNADO_REDIS_PORT)
64
patch_read(async_redis_publisher)
65
async_redis_publisher.connect()