diff --git a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs index 207f315d..f92169c4 100644 --- a/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs +++ b/Src/Dlhis.ScheduledTask/TaskAssignment/Controllers/HangfireController.cs @@ -1,5 +1,7 @@ using Dlhis.Basic.Api; using Dlhis.Jip.Api.Request; +using Hangfire; +using Kurisu.AspNetCore.Utils; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -13,11 +15,11 @@ namespace Dlhis.ScheduledTask.TaskAssignment.Controllers; [ApiController] public class HangfireController : ControllerBase { - private readonly ISyncApi _gainfoSyncApi; + private readonly RedisCache _redisCache; - public HangfireController(ISyncApi gainfoSyncApi) + public HangfireController( RedisCache redisCache) { - _gainfoSyncApi = gainfoSyncApi; + _redisCache = redisCache; } /// @@ -28,7 +30,12 @@ public class HangfireController : ControllerBase [HttpPost("sap/person/no")] public async Task PersonManuallySync(SapNoRequest request) { - await _gainfoSyncApi.SyncGainfoByUserCodeAsync(request.Code); + var jobKey = $"hangfire:background:person:{request.Code}"; + await using var handler = await _redisCache.LockAsync(jobKey); + if (handler.Acquired) + { + BackgroundJob.Enqueue(x => x.SyncGainfoByUserCodeAsync(request.Code)); + } } /// @@ -39,6 +46,11 @@ public class HangfireController : ControllerBase [HttpPost("sap/werk/no")] public async Task WerkManuallySync(SapNoRequest request) { - await _gainfoSyncApi.SyncGainfoByWerkAsync(request.Code); + var jobKey = $"hangfire:background:werk:{request.Code}"; + await using var handler = await _redisCache.LockAsync(jobKey); + if (handler.Acquired) + { + BackgroundJob.Enqueue(x => x.SyncGainfoByWerkAsync(request.Code)); + } } } \ No newline at end of file