为群总结生成增加三次重试机制

- Dify 请求失败时不再立即返回失败
- 群总结生成过程最多重试 3 次
- 增加 2 秒、4 秒递增等待,降低偶发错误影响
- 仅在三次都失败后才返回生成总结失败结果
- 补充重试次数与等待时间日志,便于排查总结异常
This commit is contained in:
liuwei
2026-04-03 09:04:03 +08:00
parent ff421a1b4b
commit 87da8e3b5c

View File

@@ -233,6 +233,10 @@ class MessageSummaryPlugin(MessagePluginInterface):
"Content-Type": "application/json" "Content-Type": "application/json"
} }
max_retries = 3
retry_delays = [2, 4]
for attempt in range(1, max_retries + 1):
try: try:
custom_timeout = ClientTimeout(total=None, connect=10, sock_read=300) custom_timeout = ClientTimeout(total=None, connect=10, sock_read=300)
conn = aiohttp.TCPConnector(keepalive_timeout=60) # 保持连接活跃 conn = aiohttp.TCPConnector(keepalive_timeout=60) # 保持连接活跃
@@ -241,7 +245,7 @@ class MessageSummaryPlugin(MessagePluginInterface):
response.raise_for_status() # 检查请求是否成功 response.raise_for_status() # 检查请求是否成功
response_data = await response.json() response_data = await response.json()
self.LOG.info(f"Dify API响应状态码: {response.status}") self.LOG.info(f"Dify API响应状态码: {response.status}, attempt={attempt}")
self.LOG.debug(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}") self.LOG.debug(f"响应数据: {json.dumps(response_data, ensure_ascii=False, indent=2)}")
# 提取回答内容 # 提取回答内容
@@ -286,16 +290,18 @@ class MessageSummaryPlugin(MessagePluginInterface):
return answer, spath return answer, spath
except aiohttp.ClientError as e: except aiohttp.ClientError as e:
self.LOG.error(f"请求Dify API时出错: {e}") self.LOG.error(f"请求Dify API时出错: attempt={attempt}/{max_retries}, error={e}")
return f"生成总结时出错", None
except json.JSONDecodeError as e: except json.JSONDecodeError as e:
self.LOG.error(f"解析Dify API响应时出错: {e}") self.LOG.error(f"解析Dify API响应时出错: attempt={attempt}/{max_retries}, error={e}")
return "解析API响应时出错", None
except Exception as e: except Exception as e:
self.LOG.error(f"处理总结时出现未知错误: {e}") self.LOG.error(f"处理总结时出现未知错误: attempt={attempt}/{max_retries}, error={e}")
return f"生成总结时出现未知错误", None
if attempt < max_retries:
delay = retry_delays[attempt - 1] if attempt - 1 < len(retry_delays) else retry_delays[-1]
self.LOG.warning(f"群总结生成失败,准备重试: attempt={attempt}/{max_retries}, delay={delay}s")
await asyncio.sleep(delay)
return "生成总结时出错", None
async def daily_summary_job(self): async def daily_summary_job(self):
"""定时任务每天早上9点总结昨天的聊天信息""" """定时任务每天早上9点总结昨天的聊天信息"""