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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 is_alive(self):
  39. return self.REDIS is not None
  40. def exist(self, k):
  41. if not self.REDIS: return
  42. try:
  43. return self.REDIS.exists(k)
  44. except Exception as e:
  45. logging.warning("[EXCEPTION]exist" + str(k) + "||" + str(e))
  46. self.__open__()
  47. def get(self, k):
  48. if not self.REDIS: return
  49. try:
  50. return self.REDIS.get(k)
  51. except Exception as e:
  52. logging.warning("[EXCEPTION]get" + str(k) + "||" + str(e))
  53. self.__open__()
  54. def set_obj(self, k, obj, exp=3600):
  55. try:
  56. self.REDIS.set(k, json.dumps(obj, ensure_ascii=False), exp)
  57. return True
  58. except Exception as e:
  59. logging.warning("[EXCEPTION]set_obj" + str(k) + "||" + str(e))
  60. self.__open__()
  61. return False
  62. def set(self, k, v, exp=3600):
  63. try:
  64. self.REDIS.set(k, v, exp)
  65. return True
  66. except Exception as e:
  67. logging.warning("[EXCEPTION]set" + str(k) + "||" + str(e))
  68. self.__open__()
  69. return False
  70. def transaction(self, key, value, exp=3600):
  71. try:
  72. pipeline = self.REDIS.pipeline(transaction=True)
  73. pipeline.set(key, value, exp, nx=True)
  74. pipeline.execute()
  75. return True
  76. except Exception as e:
  77. logging.warning("[EXCEPTION]set" + str(key) + "||" + str(e))
  78. self.__open__()
  79. return False
  80. def queue_product(self, queue, message, exp=settings.SVR_QUEUE_RETENTION) -> bool:
  81. try:
  82. payload = {"message": json.dumps(message)}
  83. pipeline = self.REDIS.pipeline()
  84. pipeline.xadd(queue, payload)
  85. pipeline.expire(queue, exp)
  86. pipeline.execute()
  87. return True
  88. except Exception as e:
  89. logging.warning("[EXCEPTION]producer" + str(queue) + "||" + str(e))
  90. return False
  91. def queue_consumer(self, queue_name, group_name, consumer_name, msg_id=b">") -> Payload:
  92. try:
  93. group_info = self.REDIS.xinfo_groups(queue_name)
  94. if not any(e["name"] == group_name for e in group_info):
  95. self.REDIS.xgroup_create(
  96. queue_name,
  97. group_name,
  98. id="$",
  99. mkstream=True
  100. )
  101. args = {
  102. "groupname": group_name,
  103. "consumername": consumer_name,
  104. "count": 1,
  105. "block": 10000,
  106. "streams": {queue_name: msg_id},
  107. }
  108. messages = self.REDIS.xreadgroup(**args)
  109. if not messages:
  110. return None
  111. stream, element_list = messages[0]
  112. msg_id, payload = element_list[0]
  113. res = Payload(self.REDIS, queue_name, group_name, msg_id, payload)
  114. return res
  115. except Exception as e:
  116. if 'key' in str(e):
  117. pass
  118. else:
  119. logging.warning("[EXCEPTION]consumer" + str(queue_name) + "||" + str(e))
  120. return None
  121. REDIS_CONN = RedisDB()