优化 任务异常后重试机制

This commit is contained in:
小肥羊 2024-11-26 14:47:23 +08:00
parent 935c6a9eb1
commit 90cf56efec
3 changed files with 12 additions and 4 deletions

View File

@ -157,6 +157,8 @@ namespace Learn.VideoAnalysis.Controllers
{ {
if (!ModelState.IsValid) return BadRequest(ModelState); if (!ModelState.IsValid) return BadRequest(ModelState);
if(await videoTaskDB.IsAnyAsync(s=>s.TagId == req.TagId) )
return BadRequest("重复添加");
// 自动映射属性到哈希 // 自动映射属性到哈希
var task = new VideoTask() var task = new VideoTask()
{ {

View File

@ -183,15 +183,15 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
private async Task<HttpResponseMessage> PostJsonStreamAsync(string path, string json) private async Task<HttpResponseMessage> PostJsonStreamAsync(string path, string json)
{ {
var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiKey);
var request = ToHttpRequest(path);
var maxRestart = 4; var maxRestart = 4;
var errorMSG = new Exception[maxRestart]; var errorMSG = new Exception[maxRestart];
for (int i = 0; i < maxRestart; i++) for (int i = 0; i < maxRestart; i++)
{ {
try try
{ {
var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiKey);
var request = ToHttpRequest(path);
request.Content = new StringContent(json, Encoding.UTF8, "application/json"); request.Content = new StringContent(json, Encoding.UTF8, "application/json");
return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
} }

View File

@ -283,6 +283,7 @@ namespace VideoAnalysisCore.Common
var tID = long.Parse(taskId); var tID = long.Parse(taskId);
if (action is not null) if (action is not null)
{ {
var errArr = new Exception[3];
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 开始执行 " + key + " " + taskId); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 开始执行 " + key + " " + taskId);
@ -299,11 +300,16 @@ namespace VideoAnalysisCore.Common
} }
catch (Exception ex) catch (Exception ex)
{ {
errArr[i] = ex;
Console.WriteLine("====================[出现异常]====================");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
Console.WriteLine("==============================================");
Thread.Sleep(1000); Thread.Sleep(1000);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 稍后后重试." + key + " " + taskId ); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 稍后后重试." + key + " " + taskId );
await SetTaskErrorMessage(tID, ex);
} }
} }
await SetTaskErrorMessage(tID, errArr.First());
} }
else else
{ {