新增 任务异常后重试机制

This commit is contained in:
小肥羊 2024-11-26 11:47:53 +08:00
parent 3e936e71f6
commit 935c6a9eb1
5 changed files with 44 additions and 24 deletions

View File

@ -21,9 +21,9 @@
<PropertyColumn Property="c=>c.LastEnum" Width="120" /> <PropertyColumn Property="c=>c.LastEnum" Width="120" />
<PropertyColumn Property="c=>c.ApiToken" Width="120" /> <PropertyColumn Property="c=>c.ApiToken" Width="120" />
<PropertyColumn Property="c=>c.ComeFrom" Width="120" /> <PropertyColumn Property="c=>c.ComeFrom" Width="120" />
<PropertyColumn Property="c=>c.MediaUrl" /> <PropertyColumn Property="c=>c.MediaUrl" Width="300" />
<PropertyColumn Property="c=>c.TotalTokens" Width="100" /> <PropertyColumn Property="c=>c.TotalTokens" Width="100" />
<PropertyColumn Property="c=>c.CreateTime" Width="100" /> <PropertyColumn Property="c=>c.CreateTime" />
</ColumnDefinitions> </ColumnDefinitions>
<ExpandTemplate Context="rowData" > <ExpandTemplate Context="rowData" >
<Descriptions Title="任务详情" Bordered> <Descriptions Title="任务详情" Bordered>

View File

@ -15,7 +15,7 @@
"DB": { "DB": {
"ConnectionString": "AllowLoadLocalInfile=true;Server=10.255.255.3;Port=3306;Database=learn.videoanalysis;User ID=marking;Password=qwe123!@#;CharSet=utf8mb4;pooling=true;SslMode=None", "ConnectionString": "AllowLoadLocalInfile=true;Server=10.255.255.3;Port=3306;Database=learn.videoanalysis;User ID=marking;Password=qwe123!@#;CharSet=utf8mb4;pooling=true;SslMode=None",
"SqlType": "MySql", "SqlType": "MySql",
"UpdateTable": true "UpdateTable": false
} }
} }
} }

View File

@ -7,6 +7,7 @@ using System.Net.Http;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Net.Http.Json; using System.Net.Http.Json;
using AntDesign; using AntDesign;
using OneOf.Types;
/// <summary> /// <summary>
/// https://platform.moonshot.cn/docs/api-reference /// https://platform.moonshot.cn/docs/api-reference
@ -185,9 +186,24 @@ namespace VideoAnalysisCore.AICore.ChatGPT.KIMI
var client = _httpClientFactory.CreateClient(); var client = _httpClientFactory.CreateClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiKey); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", ApiKey);
var request = ToHttpRequest(path); var request = ToHttpRequest(path);
var maxRestart = 4;
var errorMSG = new Exception[maxRestart];
for (int i = 0; i < maxRestart; i++)
{
try
{
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);
} }
catch (Exception e)
{
errorMSG[i] = e;
}
Thread.Sleep(1000);
}
string error = string.Join('\r', errorMSG.Select(s => "\r\n"+ s.Message + "\r\n" + s.StackTrace + "\r\n >>>>>>>>"));
throw new Exception("重试后仍旧失败!\r\n" + error);
}
private HttpRequestMessage ToHttpRequest(string path) private HttpRequestMessage ToHttpRequest(string path)
{ {

View File

@ -36,8 +36,8 @@ namespace VideoAnalysisCore.AICore.FFMPGE
var outputFile = new OutputFile(Path.Combine(task.LocalPath(), Path.GetFileNameWithoutExtension(filePath) + ".wav")); var outputFile = new OutputFile(Path.Combine(task.LocalPath(), Path.GetFileNameWithoutExtension(filePath) + ".wav"));
var ffmpeg = new Engine(FFmpegPath); var ffmpeg = new Engine(FFmpegPath);
ffmpeg.Progress += OnProgress; //ffmpeg.Progress += OnProgress;
ffmpeg.Data += OnData; //ffmpeg.Data += OnData;
ffmpeg.Complete += OnComplete; ffmpeg.Complete += OnComplete;
ffmpeg.Error += (sender, e) => ffmpeg.Error += (sender, e) =>
{ {

View File

@ -100,10 +100,10 @@ namespace VideoAnalysisCore.Common
} }
/// <summary> /// <summary>
/// 获取任务进度 /// 缓存GPT任务缓存
/// </summary> /// </summary>
/// <param name="taskId"></param> /// <param name="taskId"></param>
public static void SetTaskGPTCached(object taskId, object data) public static void SetTaskGPTCached(object taskId, object? data)
{ {
Redis.Set(RedisExpandKey.TaskGPT(taskId), data, 3600); Redis.Set(RedisExpandKey.TaskGPT(taskId), data, 3600);
} }
@ -281,9 +281,11 @@ namespace VideoAnalysisCore.Common
{ {
if (taskId is null) return; if (taskId is null) return;
var tID = long.Parse(taskId); var tID = long.Parse(taskId);
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> " + key + " " + taskId);
if (action is not null) if (action is not null)
{ {
for (int i = 0; i < 3; i++)
{
Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 开始执行 " + key + " " + taskId);
try try
{ {
Redis.HMSet(RedisExpandKey.Task(taskId), "LastEnum", key); Redis.HMSet(RedisExpandKey.Task(taskId), "LastEnum", key);
@ -292,13 +294,15 @@ namespace VideoAnalysisCore.Common
.SetColumns(it => it.LastEnum == key) .SetColumns(it => it.LastEnum == key)
.Where(it => it.Id == tID) .Where(it => it.Id == tID)
.ExecuteCommandAsync(); .ExecuteCommandAsync();
await action(taskId); await action(taskId);
return;
} }
catch (Exception ex) catch (Exception ex)
{ {
await SetTaskErrorMessage(tID, ex) Thread.Sleep(1000);
.ConfigureAwait(false);//不切回上下文 Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + "-> 稍后后重试." + key + " " + taskId );
await SetTaskErrorMessage(tID, ex);
}
} }
} }
else else