😎优化作业集群控制访问数据库

This commit is contained in:
zuohuaijun 2025-04-16 22:09:39 +08:00
parent b5c04181c3
commit 871857954a
2 changed files with 27 additions and 10 deletions

View File

@ -11,23 +11,31 @@ namespace Admin.NET.Core.Service;
/// </summary> /// </summary>
public class JobClusterServer : IJobClusterServer public class JobClusterServer : IJobClusterServer
{ {
private static readonly SqlSugarRepository<SysJobCluster> _sysJobClusterRep = App.GetRequiredService<SqlSugarRepository<SysJobCluster>>(); private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly Random _random = new(DateTime.Now.Millisecond); private readonly Random _random = new(DateTime.Now.Millisecond);
public JobClusterServer(IServiceScopeFactory serviceScopeFactory)
{
_serviceScopeFactory = serviceScopeFactory;
}
/// <summary> /// <summary>
/// 当前作业调度器启动通知 /// 当前作业调度器启动通知
/// </summary> /// </summary>
/// <param name="context">作业集群服务上下文</param> /// <param name="context">作业集群服务上下文</param>
public async void Start(JobClusterContext context) public async void Start(JobClusterContext context)
{ {
using var scope = _serviceScopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<ISqlSugarClient>().CopyNew();
// 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting // 在作业集群表中,如果 clusterId 不存在,则新增一条(否则更新一条),并设置 status 为 ClusterStatus.Waiting
if (await _sysJobClusterRep.IsAnyAsync(u => u.ClusterId == context.ClusterId)) if (await db.Queryable<SysJobCluster>().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<SysJobCluster>().SetColumns(u => u.Status == ClusterStatus.Waiting).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
} }
else 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 则继续循环 // 1) 如果作业集群表已有 status 为 ClusterStatus.Working 则继续循环
// 2) 如果作业集群表中还没有其他服务或只有自己,则插入一条集群服务或调用 await WorkNowAsync(clusterId); 之后 return; // 2) 如果作业集群表中还没有其他服务或只有自己,则插入一条集群服务或调用 await WorkNowAsync(clusterId); 之后 return;
// 3) 如果作业集群表中没有 status 为 ClusterStatus.Working 的,调用 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<ISqlSugarClient>().CopyNew();
var sysJobClusterRep = App.GetRequiredService<SqlSugarRepository<SysJobCluster>>();
if (await db.Queryable<SysJobCluster>().AnyAsync(u => u.Status == ClusterStatus.Working)) continue;
await WorkNowAsync(clusterId); await WorkNowAsync(clusterId);
return; return;
@ -72,7 +83,9 @@ public class JobClusterServer : IJobClusterServer
public async void Stop(JobClusterContext context) public async void Stop(JobClusterContext context)
{ {
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed // 在作业集群表中,更新 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<ISqlSugarClient>().CopyNew();
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
} }
/// <summary> /// <summary>
@ -82,7 +95,9 @@ public class JobClusterServer : IJobClusterServer
public async void Crash(JobClusterContext context) public async void Crash(JobClusterContext context)
{ {
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Crashed // 在作业集群表中,更新 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<ISqlSugarClient>().CopyNew();
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Crashed }).Where(u => u.ClusterId == context.ClusterId).ExecuteCommandAsync();
} }
/// <summary> /// <summary>
@ -90,9 +105,11 @@ public class JobClusterServer : IJobClusterServer
/// </summary> /// </summary>
/// <param name="clusterId">集群 Id</param> /// <param name="clusterId">集群 Id</param>
/// <returns></returns> /// <returns></returns>
private static async Task WorkNowAsync(string clusterId) private async Task WorkNowAsync(string clusterId)
{ {
// 在作业集群表中,更新 clusterId 的 status 为 ClusterStatus.Working // 在作业集群表中,更新 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<ISqlSugarClient>().CopyNew();
await db.Updateable(new SysJobCluster { Status = ClusterStatus.Working }).Where(u => u.ClusterId == clusterId).ExecuteCommandAsync();
} }
} }

View File

@ -437,7 +437,7 @@ public static class SqlSugarSetup
try 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<SysConfig>().Where(u => u.Code == ConfigConst.SysVersion).First(); var versionCfg = dbProvider.Queryable<SysConfig>().Where(u => u.Code == ConfigConst.SysVersion).First();
oldVerion = versionCfg != null ? CommonUtil.ConvertVersionToLong(versionCfg.Value) : 0; oldVerion = versionCfg != null ? CommonUtil.ConvertVersionToLong(versionCfg.Value) : 0;