|
@@ -13,6 +13,7 @@ import os
|
|
|
from typing import List, Tuple
|
|
|
from src.core import singleton
|
|
|
from redis import StrictRedis, ConnectionPool
|
|
|
+from time import sleep
|
|
|
|
|
|
SERVE_HOST = os.environ.get("SERVE_HOST")
|
|
|
# SERVE_HOST = "192.168.100.159"
|
|
@@ -209,9 +210,18 @@ class RedisHandler:
|
|
|
if expire:
|
|
|
self.redis.expire(newkey, expire)
|
|
|
|
|
|
- def publish(self, channel, message):
|
|
|
+ def publish(self, channel, message, retries=3):
|
|
|
"""发布消息到指定频道"""
|
|
|
- self.redis.publish(channel, message)
|
|
|
+ # self.redis.publish(channel, message)
|
|
|
+ for attempt in range(retries):
|
|
|
+ try:
|
|
|
+ self.redis.publish(channel, message)
|
|
|
+ return # 发布成功,退出方法
|
|
|
+ except Exception as e:
|
|
|
+ print( "Failed to publish message to channel '%s' on attempt %d: %s",channel, attempt + 1, e)
|
|
|
+ if attempt < retries - 1:
|
|
|
+ sleep(1) # 重试前等待一定时间
|
|
|
+ raise Exception(f"Failed to publish message to channel '{channel}' after {retries} attempts")
|
|
|
def __del__(self):
|
|
|
self.redis.close()
|
|
|
|