fix: LLM提醒触发加2次重试 — 避免ai-core繁忙时超时
This commit is contained in:
@@ -382,21 +382,35 @@ func triggerLLMReminder(aiCoreURL, internalToken string, r store.Reminder) {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Internal-Token", internalToken)
|
||||
|
||||
// Retry up to 2 times (ai-core may be busy processing a message).
|
||||
var lastErr error
|
||||
for attempt := 0; attempt < 2; attempt++ {
|
||||
if attempt > 0 {
|
||||
time.Sleep(5 * time.Second)
|
||||
// Re-create request body since previous one was consumed.
|
||||
reqBody, _ = json.Marshal(body)
|
||||
req, _ = http.NewRequest("POST", aiCoreURL+"/api/v1/internal/reminder-trigger", bytes.NewReader(reqBody))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Internal-Token", internalToken)
|
||||
}
|
||||
client := &http.Client{Timeout: 30 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
logger.Printf("[ReminderScheduler] LLM提醒生成请求失败: %v", err)
|
||||
return
|
||||
lastErr = err
|
||||
logger.Printf("[ReminderScheduler] LLM触发尝试%%d失败: %%v", attempt+1, err)
|
||||
continue
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||
logger.Printf("[ReminderScheduler] LLM提醒生成返回 %d: %s", resp.StatusCode, string(bodyBytes))
|
||||
resp.Body.Close()
|
||||
logger.Printf("[ReminderScheduler] LLM触发返回 %%d: %%s", resp.StatusCode, string(bodyBytes))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Printf("[ReminderScheduler] LLM提醒已触发: title=%s user=%s", r.Title, r.UserID)
|
||||
resp.Body.Close()
|
||||
logger.Printf("[ReminderScheduler] LLM提醒已触发: title=%%s user=%%s", r.Title, r.UserID)
|
||||
return
|
||||
}
|
||||
logger.Printf("[ReminderScheduler] LLM触发最终失败: %%v", lastErr)
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user