修复 执行部分任务时 没使用await等待导致的数据库连接异常

This commit is contained in:
小肥羊 2025-10-27 10:01:11 +08:00
parent 0a654c72ed
commit 5a6dc49615
2 changed files with 12 additions and 13 deletions

View File

@ -113,41 +113,40 @@ namespace VideoAnalysisCore.Common
public void Init() public void Init()
{ {
var SubscribeList = RedisManager.SubscribeList; var SubscribeList = RedisManager.SubscribeList;
SubscribeList.Add(RedisChannelEnum., (task) => SubscribeList.Add(RedisChannelEnum., async (task) =>
{ {
using var scope = AppCommon.Services?.CreateScope(); using var scope = AppCommon.Services?.CreateScope();
if (scope is null || scope.ServiceProvider.GetService<DownloadFile>() is null) if (scope is null || scope.ServiceProvider.GetService<DownloadFile>() is null)
throw new Exception("DownloadFile 未注入"); throw new Exception("DownloadFile 未注入");
else else
return scope.ServiceProvider.GetService<DownloadFile>()?.RunTask(task) ?? Task.CompletedTask; await scope.ServiceProvider.GetService<DownloadFile>()?.RunTask(task) ;
}); });
SubscribeList.Add(RedisChannelEnum., FFMPGE.RunAsync); SubscribeList.Add(RedisChannelEnum., FFMPGE.RunAsync);
SubscribeList.Add(RedisChannelEnum., senseVoice.RunTask); SubscribeList.Add(RedisChannelEnum., senseVoice.RunTask);
//SubscribeList.Add(RedisChannelEnum.解析说话人,Speaker.Run); //SubscribeList.Add(RedisChannelEnum.解析说话人,Speaker.Run);
SubscribeList.Add(RedisChannelEnum.AI课程类型, SubscribeList.Add(RedisChannelEnum.AI课程类型, async (task) =>
(task) =>
{ {
using var scope = AppCommon.Services?.CreateScope(); using var scope = AppCommon.Services?.CreateScope();
if (scope is null || scope.ServiceProvider.GetService<IBserGPT>() is null) if (scope is null || scope.ServiceProvider.GetService<IBserGPT>() is null)
throw new Exception("IBserGPT 未注入"); throw new Exception("IBserGPT 未注入");
else else
return scope.ServiceProvider.GetService<IBserGPT>()?.GetVideoType(task) ?? Task.CompletedTask; await scope.ServiceProvider.GetService<IBserGPT>()?.GetVideoType(task);
}); });
SubscribeList.Add(RedisChannelEnum.AI模型分析, (task) => SubscribeList.Add(RedisChannelEnum.AI模型分析, async (task) =>
{ {
using var scope = AppCommon.Services?.CreateScope(); using var scope = AppCommon.Services?.CreateScope();
if (scope is null || scope.ServiceProvider.GetService<IBserGPT>() is null) if (scope is null || scope.ServiceProvider.GetService<IBserGPT>() is null)
throw new Exception("IBserGPT 未注入"); throw new Exception("IBserGPT 未注入");
else else
return scope.ServiceProvider.GetService<IBserGPT>()?.GetKnow(task) ?? Task.CompletedTask; await scope.ServiceProvider?.GetService<IBserGPT>()?.GetKnow(task);
}); });
SubscribeList.Add(RedisChannelEnum.AI分析试题, (task) => SubscribeList.Add(RedisChannelEnum.AI分析试题, async (task) =>
{ {
using var scope = AppCommon.Services?.CreateScope(); using var scope = AppCommon.Services?.CreateScope();
if (scope is null || scope.ServiceProvider.GetService<IBserGPT>() is null) if (scope is null || scope.ServiceProvider.GetService<IBserGPT>() is null)
throw new Exception("IBserGPT 未注入"); throw new Exception("IBserGPT 未注入");
else else
return scope.ServiceProvider.GetService<IBserGPT>()?.GetVideoQuestion(task) ?? Task.CompletedTask; await scope.ServiceProvider?.GetService<IBserGPT>()?.GetVideoQuestion(task);
}); });
SubscribeList.Add(RedisChannelEnum., redisManager.TaskEnd); SubscribeList.Add(RedisChannelEnum., redisManager.TaskEnd);
@ -314,7 +313,8 @@ namespace VideoAnalysisCore.Common
if (oldTaskCount > 0) if (oldTaskCount > 0)
{ {
var oldTaskArr = Redis.LRange(RedisExpandKey.IDTask, 0, oldTaskCount); var oldTaskArr = Redis.LRange(RedisExpandKey.IDTask, 0, oldTaskCount);
Redis.LTrim(RedisExpandKey.IDTask, 1, 0);//删除 redis 列表 //不自动清理未完成任务 等待执行完毕/失败后自动清理
//Redis.LTrim(RedisExpandKey.IDTask, 1, 0);//删除 redis 列表
foreach (var oldTask in oldTaskArr) foreach (var oldTask in oldTaskArr)
{ {
_ = Task.Run(async () => _ = Task.Run(async () =>

View File

@ -26,7 +26,6 @@ namespace VideoAnalysisCore.Common
base.Context = CID[t] != null base.Context = CID[t] != null
? DbScoped.Sugar.GetConnectionScope(CID[t]) ? DbScoped.Sugar.GetConnectionScope(CID[t])
: DbScoped.Sugar; : DbScoped.Sugar;
return;
} }
else else
{ {
@ -36,9 +35,9 @@ namespace VideoAnalysisCore.Common
base.Context = c != null base.Context = c != null
? DbScoped.Sugar.GetConnectionScope(c.configId) ? DbScoped.Sugar.GetConnectionScope(c.configId)
: DbScoped.Sugar; : DbScoped.Sugar;
}
//这个变量也要保证是线程安全的尽量CopyNew在方法中使用 //这个变量也要保证是线程安全的尽量CopyNew在方法中使用
base.Context = base.Context.CopyNew(); base.Context = base.Context.CopyNew();
} }
} }
} }
}