2021-11-26 03:12:53 +00:00
|
|
|
import json
|
2022-02-07 11:01:13 +00:00
|
|
|
import threading
|
2022-11-17 05:44:50 +00:00
|
|
|
import time
|
2021-11-26 03:12:53 +00:00
|
|
|
|
2022-11-17 05:44:50 +00:00
|
|
|
import redis
|
2022-06-28 09:23:20 +00:00
|
|
|
from django.core.cache import cache
|
2022-11-17 05:44:50 +00:00
|
|
|
from redis.client import PubSub
|
2021-01-26 09:54:12 +00:00
|
|
|
|
2021-11-26 03:12:53 +00:00
|
|
|
from common.db.utils import safe_db_connection
|
|
|
|
from common.utils import get_logger
|
|
|
|
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
|
2021-01-26 09:54:12 +00:00
|
|
|
|
2022-03-11 04:56:22 +00:00
|
|
|
def get_redis_client(db=0):
|
2022-06-28 09:23:20 +00:00
|
|
|
client = cache.client.get_client()
|
|
|
|
assert isinstance(client, redis.Redis)
|
|
|
|
return client
|
2021-01-26 09:54:12 +00:00
|
|
|
|
|
|
|
|
2022-11-17 05:44:50 +00:00
|
|
|
class RedisPubSub:
|
|
|
|
def __init__(self, ch, db=10):
|
2021-01-26 09:54:12 +00:00
|
|
|
self.ch = ch
|
2022-11-17 05:44:50 +00:00
|
|
|
self.db = db
|
|
|
|
self.redis = get_redis_client(db)
|
|
|
|
|
|
|
|
def subscribe(self, _next, error=None, complete=None):
|
|
|
|
ps = self.redis.pubsub()
|
|
|
|
ps.subscribe(self.ch)
|
|
|
|
sub = Subscription(self, ps)
|
|
|
|
sub.keep_handle_msg(_next, error, complete)
|
|
|
|
return sub
|
|
|
|
|
|
|
|
def resubscribe(self, _next, error=None, complete=None):
|
|
|
|
self.redis = get_redis_client(self.db)
|
|
|
|
self.subscribe(_next, error, complete)
|
|
|
|
|
|
|
|
def publish(self, data):
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
self.redis.publish(self.ch, data_json)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class Subscription:
|
|
|
|
def __init__(self, pb: RedisPubSub, sub: PubSub):
|
|
|
|
self.pb = pb
|
|
|
|
self.ch = pb.ch
|
2022-02-07 11:01:13 +00:00
|
|
|
self.sub = sub
|
2022-11-17 05:44:50 +00:00
|
|
|
self.unsubscribed = False
|
2021-01-26 09:54:12 +00:00
|
|
|
|
2022-02-07 11:01:13 +00:00
|
|
|
def _handle_msg(self, _next, error, complete):
|
2021-11-26 03:12:53 +00:00
|
|
|
"""
|
|
|
|
handle arg is the pub published
|
2022-02-07 11:01:13 +00:00
|
|
|
:param _next: next msg handler
|
|
|
|
:param error: error msg handler
|
|
|
|
:param complete: complete msg handler
|
2021-11-26 03:12:53 +00:00
|
|
|
:return:
|
|
|
|
"""
|
2022-02-07 11:01:13 +00:00
|
|
|
msgs = self.sub.listen()
|
|
|
|
|
|
|
|
if error is None:
|
|
|
|
error = lambda m, i: None
|
|
|
|
|
|
|
|
if complete is None:
|
|
|
|
complete = lambda: None
|
2021-11-26 03:12:53 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
for msg in msgs:
|
|
|
|
if msg["type"] != "message":
|
|
|
|
continue
|
2022-02-07 11:01:13 +00:00
|
|
|
item = None
|
2021-11-26 03:12:53 +00:00
|
|
|
try:
|
|
|
|
item_json = msg['data'].decode()
|
|
|
|
item = json.loads(item_json)
|
|
|
|
|
|
|
|
with safe_db_connection():
|
2022-02-07 11:01:13 +00:00
|
|
|
_next(item)
|
2021-11-26 03:12:53 +00:00
|
|
|
except Exception as e:
|
2022-02-07 11:01:13 +00:00
|
|
|
error(msg, item)
|
2022-03-08 10:47:39 +00:00
|
|
|
logger.error('Subscribe handler handle msg error: {}'.format(e))
|
2021-11-26 03:12:53 +00:00
|
|
|
except Exception as e:
|
2022-11-17 05:44:50 +00:00
|
|
|
if self.unsubscribed:
|
|
|
|
logger.debug('Subscription unsubscribed')
|
|
|
|
else:
|
|
|
|
logger.error('Consume msg error: {}'.format(e))
|
|
|
|
self.retry(_next, error, complete)
|
|
|
|
return
|
2021-11-26 03:12:53 +00:00
|
|
|
|
|
|
|
try:
|
2022-02-07 11:01:13 +00:00
|
|
|
complete()
|
2021-11-26 03:12:53 +00:00
|
|
|
except Exception as e:
|
2022-02-07 11:01:13 +00:00
|
|
|
logger.error('Complete subscribe error: {}'.format(e))
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
self.unsubscribe()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error("Redis observer close error: {}".format(e))
|
|
|
|
|
|
|
|
def keep_handle_msg(self, _next, error, complete):
|
|
|
|
t = threading.Thread(target=self._handle_msg, args=(_next, error, complete))
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
return t
|
|
|
|
|
|
|
|
def unsubscribe(self):
|
2022-11-17 05:44:50 +00:00
|
|
|
self.unsubscribed = True
|
2022-02-07 11:01:13 +00:00
|
|
|
try:
|
|
|
|
self.sub.close()
|
|
|
|
except Exception as e:
|
|
|
|
logger.error('Unsubscribe msg error: {}'.format(e))
|
|
|
|
|
2022-11-17 05:44:50 +00:00
|
|
|
def retry(self, _next, error, complete):
|
|
|
|
logger.info('Retry subscribe channel: {}'.format(self.ch))
|
|
|
|
times = 0
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
self.unsubscribe()
|
|
|
|
self.pb.resubscribe(_next, error, complete)
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
logger.error('Retry #{} {} subscribe channel error: {}'.format(times, self.ch, e))
|
|
|
|
times += 1
|
|
|
|
time.sleep(times * 2)
|