|
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() |