fix enqueue任务执行

This commit is contained in:
2024-11-26 16:42:52 +08:00
parent b0eb954b43
commit 73dd0f9765

View File

@@ -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;
}
/// <summary>
@@ -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<ISyncApi>(x => x.SyncGainfoByUserCodeAsync(request.Code));
}
}
/// <summary>
@@ -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<ISyncApi>(x => x.SyncGainfoByWerkAsync(request.Code));
}
}
}