Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

redis_conn.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import json
  2. import redis
  3. import logging
  4. from rag import settings
  5. from rag.utils import singleton
  6. class Payload:
  7. def __init__(self, consumer, queue_name, group_name, msg_id, message):
  8. self.__consumer = consumer
  9. self.__queue_name = queue_name
  10. self.__group_name = group_name
  11. self.__msg_id = msg_id
  12. self.__message = json.loads(message['message'])
  13. def ack(self):
  14. try:
  15. self.__consumer.xack(self.__queue_name, self.__group_name, self.__msg_id)
  16. return True
  17. except Exception as e:
  18. logging.warning("[EXCEPTION]ack" + str(self.__queue_name) + "||" + str(e))
  19. return False
  20. def get_message(self):
  21. return self.__message
  22. @singleton
  23. class RedisDB:
  24. def __init__(self):
  25. self.REDIS = None
  26. self.config = settings.REDIS
  27. self.__open__()
  28. def __open__(self):
  29. try:
  30. self.REDIS = redis.StrictRedis(host=self.config["host"].split(":")[0],
  31. port=int(self.config.get("host", ":6379").split(":")[1]),
  32. db=int(self.config.get("db", 1)),
  33. password=self.config.get("password"),
  34. decode_responses=True)
  35. except Exception as e:
  36. logging.warning("Redis can't be connected.")
  37. return self.REDIS
  38. def health(self, queue_name):
  39. self.REDIS.ping()
  40. return self.REDIS.xinfo_groups(queue_name)[0]
  41. def is_alive(self):
  42. return self.REDIS is not None
  43. def exist(self, k):
  44. if not self.REDIS: return
  45. try:
  46. return self.REDIS.exists(k)
  47. except Exception as e:
  48. logging.warning("[EXCEPTION]exist" + str(k) + "||" + str(e))
  49. self.__open__()
  50. def get(self, k):
  51. if not self.REDIS: return
  52. try:
  53. return self.REDIS.get(k)
  54. except Exception as e:
  55. logging.warning("[EXCEPTION]get" + str(k) + "||" + str(e))
  56. self.__open__()
  57. def set_obj(self, k, obj, exp=3600):
  58. try:
  59. self.REDIS.set(k, json.dumps(obj, ensure_ascii=False), exp)
  60. return True
  61. except Exception as e:
  62. logging.warning("[EXCEPTION]set_obj" + str(k) + "||" + str(e))
  63. self.__open__()
  64. return False
  65. def set(self, k, v, exp=3600):
  66. try:
  67. self.REDIS.set(k, v, exp)
  68. return True
  69. except Exception as e:
  70. logging.warning("[EXCEPTION]set" + str(k) + "||" + str(e))
  71. self.__open__()
  72. return False
  73. def transaction(self, key, value, exp=3600):
  74. try:
  75. pipeline = self.REDIS.pipeline(transaction=True)
  76. pipeline.set(key, value, exp, nx=True)
  77. pipeline.execute()
  78. return True
  79. except Exception as e:
  80. logging.warning("[EXCEPTION]set" + str(key) + "||" + str(e))
  81. self.__open__()
  82. return False
  83. def queue_product(self, queue, message, exp=settings.SVR_QUEUE_RETENTION) -> bool:
  84. try:
  85. payload = {"message": json.dumps(message)}
  86. pipeline = self.REDIS.pipeline()
  87. pipeline.xadd(queue, payload)
  88. pipeline.expire(queue, exp)
  89. pipeline.execute()
  90. return True
  91. except Exception as e:
  92. logging.warning("[EXCEPTION]producer" + str(queue) + "||" + str(e))
  93. return False
  94. def queue_consumer(self, queue_name, group_name, consumer_name, msg_id=b">") -> Payload:
  95. try:
  96. group_info = self.REDIS.xinfo_groups(queue_name)
  97. if not any(e["name"] == group_name for e in group_info):
  98. self.REDIS.xgroup_create(
  99. queue_name,
  100. group_name,
  101. id="$",
  102. mkstream=True
  103. )
  104. args = {
  105. "groupname": group_name,
  106. "consumername": consumer_name,
  107. "count": 1,
  108. "block": 10000,
  109. "streams": {queue_name: msg_id},
  110. }
  111. messages = self.REDIS.xreadgroup(**args)
  112. if not messages:
  113. return None
  114. stream, element_list = messages[0]
  115. msg_id, payload = element_list[0]
  116. res = Payload(self.REDIS, queue_name, group_name, msg_id, payload)
  117. return res
  118. except Exception as e:
  119. if 'key' in str(e):
  120. pass
  121. else:
  122. logging.warning("[EXCEPTION]consumer" + str(queue_name) + "||" + str(e))
  123. return None
  124. REDIS_CONN = RedisDB()