fix 优化任务分配速度

This commit is contained in:
2024-05-30 23:01:21 +08:00
parent bce58ef938
commit 7f14b1fdc5
13 changed files with 195 additions and 47 deletions

View File

@@ -1,4 +1,5 @@
using Dlhis.Basic.Api.Dto; using Dlhis.Basic.Api.Dto;
using Dlhis.Basic.Api.Request;
using Kurisu.RemoteCall.Attributes; using Kurisu.RemoteCall.Attributes;
using Kurisu.RemoteCall.Default; using Kurisu.RemoteCall.Default;
@@ -25,4 +26,13 @@ public interface IDevicePermissionApi
/// <returns></returns> /// <returns></returns>
[Post("/api/devicepermissions/re-permission")] [Post("/api/devicepermissions/re-permission")]
Task<DefaultApiResult<int>> RePermissionUsersInfoAsync(PermissionUsersInfoDto beforeDto); Task<DefaultApiResult<int>> RePermissionUsersInfoAsync(PermissionUsersInfoDto beforeDto);
/// <summary>
/// 任务生成
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[Post("/api/devicepermissions/assignment-generate")]
Task<DefaultApiResult> GeneratePermissionsAsync(GeneratePermissionIdRequest request);
} }

View File

@@ -0,0 +1,7 @@
namespace Dlhis.Basic.Api.Request
{
public class GeneratePermissionIdRequest
{
public long Id { get; set; }
}
}

View File

@@ -32,6 +32,16 @@ public class DevicePermissionsController : ControllerBase
await _devicePermissionService.AssignAsync(request); await _devicePermissionService.AssignAsync(request);
} }
[AllowAnonymous]
[HttpPost("assignment-generate")]
[Transactional]
[IgnoreTenant]
public async Task GeneratePermissionsAsync(GeneratePermissionIdRequest request)
{
await _devicePermissionService.GeneratePermissionsAsync(request.Id);
}
/// <summary> /// <summary>
/// 当前类型的权限分配 /// 当前类型的权限分配
/// </summary> /// </summary>

View File

@@ -0,0 +1,7 @@
namespace Dlhis.Basic.Device.Request
{
public class GeneratePermissionIdRequest
{
public long Id { get; set; }
}
}

View File

@@ -10,9 +10,12 @@ public interface IDevicePermissionService : IScopeDependency
/// <summary> /// <summary>
/// 保存设备权限 /// 保存设备权限
/// </summary> /// </summary>
/// <param name="reuqest"></param> /// <param name="request"></param>
/// <returns></returns> /// <returns></returns>
Task AssignAsync(DevicePermissionSaveRequest reuqest); Task AssignAsync(DevicePermissionSaveRequest request);
Task GeneratePermissionsAsync(long id);
/// <summary> /// <summary>
/// 获取分配的权限 /// 获取分配的权限

View File

@@ -5,11 +5,14 @@ using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums; using Dlhis.Entity.Device.Enums;
using Dlhis.Entity.Sys.Entities; using Dlhis.Entity.Sys.Entities;
using Dlhis.Entity.Sys.Enums; using Dlhis.Entity.Sys.Enums;
using Dlhis.Scheduled.Api;
using Kurisu.AspNetCore.Authentication.Abstractions; using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.Entity; using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils;
using Kurisu.AspNetCore.Utils.Extensions; using Kurisu.AspNetCore.Utils.Extensions;
using SqlSugar; using SqlSugar;
using GeneratePermissionIdRequest = Dlhis.Scheduled.Api.Request.GeneratePermissionIdRequest;
namespace Dlhis.Basic.Device.Services.Implements; namespace Dlhis.Basic.Device.Services.Implements;
@@ -17,16 +20,51 @@ public class DevicePermissionService : IDevicePermissionService
{ {
private readonly IDbContext _dbContext; private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser; private readonly ICurrentUser _currentUser;
private readonly RedisCache _redisCache;
private readonly IHangfireApi _hangfireApi;
public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser) public DevicePermissionService(IDbContext dbContext, ICurrentUser currentUser, RedisCache redisCache, IHangfireApi hangfireApi)
{ {
_dbContext = dbContext; _dbContext = dbContext;
_currentUser = currentUser; _currentUser = currentUser;
_redisCache = redisCache;
_hangfireApi = hangfireApi;
} }
public async Task AssignAsync(DevicePermissionSaveRequest request) public async Task AssignAsync(DevicePermissionSaveRequest request)
{ {
var currentTenantId = _currentUser.GetStringTenantId();
await using var handler = await _redisCache.LockAsync($"permission:generate:{currentTenantId}");
handler.Acquired.ThrowIfFalse("请勿频繁分配设备权限");
var tenantId = request.AssignType == AssignTypeEnumType.Department ? request.TenantId : _currentUser.GetStringTenantId(); var tenantId = request.AssignType == AssignTypeEnumType.Department ? request.TenantId : _currentUser.GetStringTenantId();
request.TenantId = tenantId;
var json = request.ToJson();
var id = await _dbContext.InsertReturnIdentityAsync(new DevicePermissionAssignmentEntity
{
Assignment = json,
IsHandler = false,
Result = null,
TenantId = currentTenantId
});
await _hangfireApi.GeneratePermissionsAsync(new GeneratePermissionIdRequest()
{
Id = id
});
}
public async Task GeneratePermissionsAsync(long id)
{
var assignment = await _dbContext.Queryable<DevicePermissionAssignmentEntity>().SingleAsync(x => x.Id == id);
if (assignment == null) return;
if (assignment.IsHandler) return;
try
{
var request = assignment.Assignment.ToObject<DevicePermissionSaveRequest>();
await GenerateDeviceTasksAsync(async () => await GenerateDeviceTasksAsync(async () =>
{ {
@@ -35,7 +73,7 @@ public class DevicePermissionService : IDevicePermissionService
.Where(x => x.AssignType == request.AssignType) .Where(x => x.AssignType == request.AssignType)
.Where(x => x.AssignValue == request.AssignValue) .Where(x => x.AssignValue == request.AssignValue)
.Where(x => x.UniqueType == request.UniqueType) .Where(x => x.UniqueType == request.UniqueType)
.Where(x => x.TenantId == tenantId) .Where(x => x.TenantId == request.TenantId)
.ToListAsync(); .ToListAsync();
await _dbContext.DeleteReallyAsync(p); await _dbContext.DeleteReallyAsync(p);
@@ -49,13 +87,19 @@ public class DevicePermissionService : IDevicePermissionService
AssignValue = request.AssignValue, AssignValue = request.AssignValue,
UniqueType = request.UniqueType, UniqueType = request.UniqueType,
UniqueValue = code, UniqueValue = code,
TenantId = tenantId TenantId = request.TenantId
}).ToList(); }).ToList();
await _dbContext.InsertAsync(devicePermissions); await _dbContext.InsertAsync(devicePermissions);
} }
}, null); }, null);
} }
finally
{
assignment.IsHandler = true;
await _dbContext.UpdateAsync(assignment);
}
}
public async Task<List<Guid>> GetAssignmentsAsync(DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue) public async Task<List<Guid>> GetAssignmentsAsync(DeviceAssignmentTypeEnumType uniqueType, AssignTypeEnumType assignType, string assignValue)
{ {

View File

@@ -54,20 +54,22 @@ public class SqlSugarPack : BaseAppPack
public override void Configure(IApplicationBuilder app, IWebHostEnvironment env) public override void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
var isGenerate = Configuration.GetSection("SqlSugarOptions").GetValue<bool>("Generate"); var isGenerate = Configuration.GetSection("SqlSugarOptions").GetValue<bool>("Generate");
if (isGenerate) if (!isGenerate)
{ {
return;
}
using var scope = app.ApplicationServices.CreateScope(); using var scope = app.ApplicationServices.CreateScope();
var db = scope.ServiceProvider.GetService<IDbContext>(); var db = scope.ServiceProvider.GetService<IDbContext>();
// var tables = App.ActiveTyps.Where(x => x.IsDefined(typeof(SugarTable), false)) var tables = App.ActiveTypes.Where(x => x.IsDefined(typeof(SugarTable), false))
// .Where(x => !x.IsDefined(typeof(SkipScanAttribute), false)) .Where(x => !x.IsDefined(typeof(SkipScanAttribute), false))
// .ToArray(); .ToArray();
//
// db.Client.DbMaintenance.CreateDatabase(); db.Client.DbMaintenance.CreateDatabase();
// db.Client.CodeFirst.InitTables(tables); db.Client.CodeFirst.InitTables(tables);
//
// var faceDb = db.ChangeDb("face"); var faceDb = db.ChangeDb("face");
// faceDb.Client.DbMaintenance.CreateDatabase(); faceDb.Client.DbMaintenance.CreateDatabase();
// faceDb.Client.CodeFirst.InitTables<GainfoFaceEntity>(); faceDb.Client.CodeFirst.InitTables<GainfoFaceEntity>();
}
} }
} }

View File

@@ -0,0 +1,32 @@
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar;
using SqlSugar;
namespace Dlhis.Entity.Basic.Entity;
/// <summary>
/// 设备权限分配
/// </summary>
[SugarTable(TableName = "DevicePermissionAssignment", TableDescription = "设备权限分配")]
public class DevicePermissionAssignmentEntity : SugarBaseEntity, ITenantId
{
/// <summary>
/// 分配json
/// </summary>
[SugarColumn(IsNullable = false, ColumnDataType = "varchar(1000)", ColumnDescription = "分配json")]
public string Assignment { get; set; }
/// <summary>
/// 是否处理
/// </summary>
[SugarColumn(ColumnDataType = "bit(1)", ColumnDescription = "是否处理")]
public bool IsHandler { get; set; }
/// <summary>
/// 处理结果
/// </summary>
[SugarColumn(ColumnDescription = "处理结果")]
public string Result { get; set; }
public string TenantId { get; set; }
}

View File

@@ -4,6 +4,7 @@ using Kurisu.RemoteCall.Default;
namespace Dlhis.Scheduled.Api; namespace Dlhis.Scheduled.Api;
[RequestLog]
[EnableRemoteClient(Name = "hangfire-service", BaseUrl = "${InternalRequest:ScheduledRequestUrl}")] [EnableRemoteClient(Name = "hangfire-service", BaseUrl = "${InternalRequest:ScheduledRequestUrl}")]
public interface IHangfireApi public interface IHangfireApi
{ {
@@ -14,4 +15,8 @@ public interface IHangfireApi
/// <returns></returns> /// <returns></returns>
[Post("api/hangfire/sap/person/no")] [Post("api/hangfire/sap/person/no")]
Task<DefaultApiResult> SyncSapPersonAsync(SapNoRequest request); Task<DefaultApiResult> SyncSapPersonAsync(SapNoRequest request);
[Auth]
[Post("api/hangfire/assignment-generate")]
Task<DefaultApiResult> GeneratePermissionsAsync(GeneratePermissionIdRequest request);
} }

View File

@@ -0,0 +1,7 @@
namespace Dlhis.Scheduled.Api.Request
{
public class GeneratePermissionIdRequest
{
public long Id { get; set; }
}
}

View File

@@ -1,4 +1,5 @@
using Dlhis.ScheduledTask.Common.Request; using Dlhis.Basic.Api.Request;
using Dlhis.ScheduledTask.Common.Request;
namespace Dlhis.ScheduledTask.Common.Services; namespace Dlhis.ScheduledTask.Common.Services;
@@ -20,4 +21,7 @@ public interface IHangfireService : IScopeDependency
/// <param name="request"></param> /// <param name="request"></param>
/// <returns></returns> /// <returns></returns>
Task CreateSapWerkSyncBackgroundJobAsync(SapNoRequest request); Task CreateSapWerkSyncBackgroundJobAsync(SapNoRequest request);
void GeneratePermissions(GeneratePermissionIdRequest request);
} }

View File

@@ -1,4 +1,6 @@
using Dlhis.Entity.Basic.Entity; using Dlhis.Basic.Api;
using Dlhis.Basic.Api.Request;
using Dlhis.Entity.Basic.Entity;
using Dlhis.ScheduledTask.Common.Request; using Dlhis.ScheduledTask.Common.Request;
using Hangfire; using Hangfire;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
@@ -33,7 +35,7 @@ public class HangfireService : IHangfireService
{ {
var jobKey = $"hangfire:background:werk:{request.Code}"; var jobKey = $"hangfire:background:werk:{request.Code}";
using var handler = await _redisCache.LockAsync(jobKey); await using var handler = await _redisCache.LockAsync(jobKey);
if (handler.Acquired) if (handler.Acquired)
{ {
var werkInfo = await _dbContext.Queryable<WerkEntity>().Where(x => x.Werk == request.Code).SingleAsync(); var werkInfo = await _dbContext.Queryable<WerkEntity>().Where(x => x.Werk == request.Code).SingleAsync();
@@ -44,4 +46,10 @@ public class HangfireService : IHangfireService
BackgroundJob.Enqueue<IGainfoUpdateService>(x => x.TodoByWerkAsync(request.Code)); BackgroundJob.Enqueue<IGainfoUpdateService>(x => x.TodoByWerkAsync(request.Code));
} }
} }
/// <inheritdoc />
public void GeneratePermissions(GeneratePermissionIdRequest request)
{
BackgroundJob.Enqueue<IDevicePermissionApi>(x => x.GeneratePermissionsAsync(request));
}
} }

View File

@@ -1,4 +1,6 @@
using Dlhis.ScheduledTask.Common.Request; using Dlhis.Basic.Api;
using Dlhis.Basic.Api.Request;
using Dlhis.ScheduledTask.Common.Request;
using Dlhis.ScheduledTask.Common.Services; using Dlhis.ScheduledTask.Common.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
@@ -42,4 +44,11 @@ public class HangfireController : ControllerBase
{ {
await _hangfireService.CreateSapWerkSyncBackgroundJobAsync(request); await _hangfireService.CreateSapWerkSyncBackgroundJobAsync(request);
} }
[HttpPost("assignment-generate")]
public void GeneratePermissionsAsync(GeneratePermissionIdRequest request)
{
_hangfireService.GeneratePermissions(request);
}
} }