From 871857954a22d41bc46d97539751e2bbc8700b34 Mon Sep 17 00:00:00 2001 From: zuohuaijun Date: Wed, 16 Apr 2025 22:09:39 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=8E=E4=BC=98=E5=8C=96=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E9=9B=86=E7=BE=A4=E6=8E=A7=E5=88=B6=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Service/Job/JobClusterServer.cs | 35 ++++++++++++++----- .../Admin.NET.Core/SqlSugar/SqlSugarSetup.cs | 2 +- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Admin.NET/Admin.NET.Core/Service/Job/JobClusterServer.cs b/Admin.NET/Admin.NET.Core/Service/Job/JobClusterServer.cs index a7156b10..59a6ddd7 100644 --- a/Admin.NET/Admin.NET.Core/Service/Job/JobClusterServer.cs +++ b/Admin.NET/Admin.NET.Core/Service/Job/JobClusterServer.cs @@ -11,23 +11,31 @@ namespace Admin.NET.Core.Service; /// public class JobClusterServer : IJobClusterServer { - private static readonly SqlSugarRepository _sysJobClusterRep = App.GetRequiredService>(); + private readonly IServiceScopeFactory _serviceScopeFactory; private readonly Random _random = new(DateTime.Now.Millisecond); + public JobClusterServer(IServiceScopeFactory serviceScopeFactory) + { + _serviceScopeFactory = serviceScopeFactory; + } + /// /// 当前作业调度器启动通知 /// /// 作业集群服务上下文 public async void Start(JobClusterContext context) { + using var scope = _serviceScopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService().CopyNew(); + // 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting - if (await _sysJobClusterRep.IsAnyAsync(u => u.ClusterId == context.ClusterId)) + if (await db.Queryable().AnyAsync(u => u.ClusterId == context.ClusterId)) { - await _sysJobClusterRep.AsUpdateable().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync(); + await db.Updateable().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync(); } else { - await _sysJobClusterRep.AsInsertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommandAsync(); + await db.Insertable(new SysJobCluster { ClusterId = context.ClusterId, Status = ClusterStatus.Waiting }).ExecuteCommandAsync(); } } @@ -55,7 +63,10 @@ public class JobClusterServer : IJobClusterServer // 1) 如果作业集群表已有 status 为 ClusterStatus.Working 则继续循环 // 2) 如果作业集群表中还没有其他服务或只有自己,则插入一条集群服务或调用 await WorkNowAsync(clusterId); 之后 return; // 3) 如果作业集群表中没有 status 为 ClusterStatus.Working 的,调用 await WorkNowAsync(clusterId); 之后 return; - if (await _sysJobClusterRep.IsAnyAsync(u => u.Status == ClusterStatus.Working)) continue; + using var scope = _serviceScopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService().CopyNew(); + var sysJobClusterRep = App.GetRequiredService>(); + if (await db.Queryable().AnyAsync(u => u.Status == ClusterStatus.Working)) continue; await WorkNowAsync(clusterId); return; @@ -72,7 +83,9 @@ public class JobClusterServer : IJobClusterServer public async void Stop(JobClusterContext context) { // 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed - await _sysJobClusterRep.UpdateAsync(u => new SysJobCluster { Status = ClusterStatus.Crashed }, u => u.ClusterId == context.ClusterId); + using var scope = _serviceScopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService().CopyNew(); + await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync(); } /// @@ -82,7 +95,9 @@ public class JobClusterServer : IJobClusterServer public async void Crash(JobClusterContext context) { // 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed - await _sysJobClusterRep.UpdateAsync(u => new SysJobCluster { Status = ClusterStatus.Crashed }, u => u.ClusterId == context.ClusterId); + using var scope = _serviceScopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService().CopyNew(); + await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync(); } /// @@ -90,9 +105,11 @@ public class JobClusterServer : IJobClusterServer /// /// 集群 Id /// - private static async Task WorkNowAsync(string clusterId) + private async Task WorkNowAsync(string clusterId) { // 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Working - await _sysJobClusterRep.UpdateAsync(u => new SysJobCluster { Status = ClusterStatus.Working }, u => u.ClusterId == clusterId); + using var scope = _serviceScopeFactory.CreateScope(); + var db = scope.ServiceProvider.GetRequiredService().CopyNew(); + await db.Updateable(new SysJobCluster { Status = ClusterStatus.Working }).Where(u => u.ClusterId == clusterId).ExecuteCommandAsync(); } } \ No newline at end of file diff --git a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs index 2c151a48..9534a09f 100644 --- a/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs +++ b/Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs @@ -437,7 +437,7 @@ public static class SqlSugarSetup try { // 获取系统版本号 - if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId && dbProvider.DbMaintenance.IsAnyTable(dbProvider.EntityMaintenance.GetTableName(typeof(SysConfig))) + if (dbProvider.CurrentConnectionConfig.ConfigId.ToString() == SqlSugarConst.MainConfigId && dbProvider.DbMaintenance.IsAnyTable(dbProvider.EntityMaintenance.GetTableName(typeof(SysConfig)))) { var versionCfg = dbProvider.Queryable().Where(u => u.Code == ConfigConst.SysVersion).First(); oldVerion = versionCfg != null ? CommonUtil.ConvertVersionToLong(versionCfg.Value) : 0;