fix(schedule): 修复日程生成在连续失败时无限重试的问题

先前的日程生成逻辑使用无限循环进行重试。在 LLM 服务持续失败或返回无效数据的情况下,这可能导致程序陷入死循环。

本次修改将重试机制改为有固定上限(3次)的循环。如果所有尝试均失败,将记录错误并返回 None,从而确保程序的健壮性。
This commit is contained in:
minecraft1024a
2025-09-23 14:13:44 +08:00
parent b6792149f9
commit b7fba5c8ed

View File

@@ -94,11 +94,10 @@ class ScheduleLLMGenerator:
请你扮演我以我的身份和口吻为我生成一份完整的24小时日程表。 请你扮演我以我的身份和口吻为我生成一份完整的24小时日程表。
""" """
attempt = 0 max_retries = 3
while True: for attempt in range(1, max_retries + 1):
attempt += 1
try: try:
logger.info(f"正在生成日程 (第 {attempt} 次尝试)") logger.info(f"正在生成日程 (第 {attempt}/{max_retries} 次尝试)")
prompt = base_prompt prompt = base_prompt
if attempt > 1: if attempt > 1:
failure_hint = f""" failure_hint = f"""
@@ -118,12 +117,16 @@ class ScheduleLLMGenerator:
return schedule_data return schedule_data
else: else:
logger.warning(f"{attempt} 次生成的日程验证失败,继续重试...") logger.warning(f"{attempt} 次生成的日程验证失败,继续重试...")
await asyncio.sleep(2)
except Exception as e: except Exception as e:
logger.error(f"{attempt} 次生成日程失败: {e}") logger.error(f"{attempt} 次生成日程失败: {e}")
logger.info("继续重试...")
await asyncio.sleep(3) if attempt < max_retries:
logger.info("2秒后继续重试...")
await asyncio.sleep(2)
logger.error("所有尝试都失败,无法生成日程,将会在下次启动时自动重试")
return None
@staticmethod @staticmethod
def _validate_schedule_with_pydantic(schedule_data) -> bool: def _validate_schedule_with_pydantic(schedule_data) -> bool: