修复 尝试解决多线程sql异常问题

This commit is contained in:
小肥羊 2025-10-22 14:18:00 +08:00
parent 1cb53fe405
commit 1151068185
5 changed files with 68 additions and 67 deletions

View File

@ -69,11 +69,11 @@ namespace Learn.VideoAnalysis.Components.Pages
/// 重试 /// 重试
/// </summary> /// </summary>
/// <param name="query"></param> /// <param name="query"></param>
async void ReStart() async Task ReStart()
{ {
await redisManager.ClearTaskError(reStartTask.Id); await redisManager.ClearTaskError(reStartTask.Id);
_=Task.Run(() => await Task.Run(async () =>
redisManager.InsertChannel((RedisChannelEnum)selectEnum, reStartTask.Id) await redisManager.InsertChannel((RedisChannelEnum)selectEnum, reStartTask.Id)
); );
modalShow = false; modalShow = false;
} }

View File

@ -76,7 +76,8 @@ namespace VideoAnalysisCore.Common.Expand
if (ex.Parametres == null) return; if (ex.Parametres == null) return;
//var originColor = Console.ForegroundColor; //var originColor = Console.ForegroundColor;
//Console.ForegroundColor = ConsoleColor.DarkRed; //Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"【{DateTime.Now}——错误SQL - [{config.ConfigId}]】\r\n"+ ex.Message + "\r\n" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n"); Console.WriteLine($"【{DateTime.Now}——错误SQL - [{config.ConfigId}]】\r\n"+ ex.Message + "\r\n"
+ UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
Console.WriteLine(); Console.WriteLine();
//Console.ForegroundColor = originColor; //Console.ForegroundColor = originColor;
}; };

View File

@ -301,8 +301,6 @@ namespace VideoAnalysisCore.Common
public async void InitChannel() public async void InitChannel()
{ {
if (Redis is null) throw new Exception("redis未初始化"); if (Redis is null) throw new Exception("redis未初始化");
//处理之前程序结束前未能执行完的情况 //处理之前程序结束前未能执行完的情况
var oldTaskCount = Redis.LLen(RedisExpandKey.IDTask); var oldTaskCount = Redis.LLen(RedisExpandKey.IDTask);
if (oldTaskCount > 0) if (oldTaskCount > 0)
@ -378,7 +376,7 @@ namespace VideoAnalysisCore.Common
Console.WriteLine($"{DateTime.Now} =>服务端不接收任务"); Console.WriteLine($"{DateTime.Now} =>服务端不接收任务");
return; return;
} }
Task.Run(async () => Task.Run(() =>
{ {
lock (Redis) lock (Redis)
{ {
@ -396,7 +394,6 @@ namespace VideoAnalysisCore.Common
} }
}); });
} }
/// <summary> /// <summary>
@ -437,8 +434,9 @@ namespace VideoAnalysisCore.Common
/// <returns></returns> /// <returns></returns>
public async Task<bool> SetTaskError(long taskID, string? error) public async Task<bool> SetTaskError(long taskID, string? error)
{ {
var vDB = AppCommon.Services.GetService<Repository<VideoTask>>();
Redis.HMSet(RedisExpandKey.Task(taskID), "ErrorMessage", error); Redis.HMSet(RedisExpandKey.Task(taskID), "ErrorMessage", error);
return await videoTaskDB.AsUpdateable() return await vDB.CopyNew().AsUpdateable()
.SetColumns(it => it.ErrorMessage == error)//SetColumns是可以叠加的 写2个就2个字段赋值 .SetColumns(it => it.ErrorMessage == error)//SetColumns是可以叠加的 写2个就2个字段赋值
.Where(it => it.Id == taskID) .Where(it => it.Id == taskID)
.ExecuteCommandAsync() == 1; .ExecuteCommandAsync() == 1;
@ -464,14 +462,11 @@ namespace VideoAnalysisCore.Common
{ {
Redis.HMSet(RedisExpandKey.Task(taskId), "LastEnum", key); Redis.HMSet(RedisExpandKey.Task(taskId), "LastEnum", key);
Redis.HMSet(RedisExpandKey.Task(taskId), "Progress", 0); Redis.HMSet(RedisExpandKey.Task(taskId), "Progress", 0);
lock (Redis) var vDB = AppCommon.Services.GetService<Repository<VideoTask>>();
{ await vDB.CopyNew().AsUpdateable()
videoTaskDB.AsUpdateable()
.SetColumns(it => it.LastEnum == key) .SetColumns(it => it.LastEnum == key)
.Where(it => it.Id == tID) .Where(it => it.Id == tID)
.ExecuteCommand(); .ExecuteCommandAsync();
}
await action(taskId); await action(taskId);
return; return;
} }

View File

@ -28,12 +28,17 @@ namespace VideoAnalysisCore.Common
: DbScoped.Sugar; : DbScoped.Sugar;
return; return;
} }
else
{
var c = t.GetCustomAttribute<TenantAttribute>(); var c = t.GetCustomAttribute<TenantAttribute>();
if (!CID.ContainsKey(t)) if (!CID.ContainsKey(t))
CID.Add(t, c?.configId); CID.Add(t, c?.configId);
base.Context = c != null base.Context = c != null
? DbScoped.Sugar.GetConnectionScope(c.configId) ? DbScoped.Sugar.GetConnectionScope(c.configId)
: DbScoped.Sugar; : DbScoped.Sugar;
//这个变量也要保证是线程安全的尽量CopyNew在方法中使用
base.Context = base.Context.CopyNew();
}
} }
} }
} }

View File

@ -74,7 +74,7 @@
<PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.10.32" /> <PackageReference Include="org.k2fsa.sherpa.onnx" Version="1.10.32" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.7" /> <PackageReference Include="SixLabors.ImageSharp" Version="3.1.7" />
<PackageReference Include="SqlSugar.IOC" Version="2.0.0" /> <PackageReference Include="SqlSugar.IOC" Version="2.0.0" />
<PackageReference Include="SqlSugarCore" Version="5.1.4.170" /> <PackageReference Include="SqlSugarCore" Version="5.1.4.205" />
<PackageReference Include="UserCenter.Model" Version="1.3.5" /> <PackageReference Include="UserCenter.Model" Version="1.3.5" />
<PackageReference Include="Whisper.net" Version="1.5.0" /> <PackageReference Include="Whisper.net" Version="1.5.0" />
<PackageReference Include="Whisper.net.Runtime" Version="1.5.0" /> <PackageReference Include="Whisper.net.Runtime" Version="1.5.0" />