fix 并发执行问题,保密人员余额迁移

This commit is contained in:
2024-06-04 21:38:12 +08:00
parent 9af6472be6
commit 582393e45d
31 changed files with 400 additions and 274 deletions

View File

@@ -1,4 +1,4 @@
namespace Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Attributes;
[AttributeUsage(AttributeTargets.Class)]
public class RecurringJobAttribute : Attribute

View File

@@ -1,8 +1,9 @@
using System.Reflection;
using Dlhis.ScheduledTask.Attributes;
using Kurisu.AspNetCore.DataAccess.Entity;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils;
using Kurisu.Scheduled.Abstractions;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask;
@@ -23,18 +24,27 @@ public abstract class BaseJob : IJob
using (var scope = _serviceProvider.CreateScope())
{
Logger = scope.ServiceProvider.GetService<ILogger<BaseJob>>();
var recurringJob = this.GetType().GetCustomAttribute<RecurringJobAttribute>()!;
var key = recurringJob.UniqueJobKey;
var name = recurringJob!.Name;
var redisCache = scope.ServiceProvider.GetService<RedisCache>();
await using var handler = await redisCache.LockAsync($"scheduled:{key}");
DbContext = scope.ServiceProvider.GetService<IDbContext>();
var sqlSugarOptionsService = scope.ServiceProvider.GetService<ISqlSugarOptionsService>();
DbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
var sqlSugarOptionsService = scope.ServiceProvider.GetService<ISqlSugarOptionsService>();
sqlSugarOptionsService.IgnoreTenant = true;
var name = this.GetType().GetCustomAttribute<RecurringJobAttribute>()!.Name;
Logger.LogDebug("[{name}]开始执行...", name);
try
{
await RunAsync(scope.ServiceProvider);
if (handler.Acquired)
{
Logger.LogInformation("[{name}]开始执行...", name);
await RunAsync(scope.ServiceProvider);
}
}
catch (Exception ex)
{
@@ -49,5 +59,5 @@ public abstract class BaseJob : IJob
}
}
public abstract Task RunAsync(IServiceProvider serviceProvider);
}
protected abstract Task RunAsync(IServiceProvider serviceProvider);
}

View File

@@ -1,10 +1,10 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.Sys.Api;
using Dlhis.Sys.Api.Response;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Scheduled.Attributes;
using Newtonsoft.Json;
namespace Dlhis.ScheduledTask.Jobs;
@@ -26,7 +26,7 @@ public class CreateDefinitelyGroupJob : BaseJob
private static readonly string _remark = "系统自动生成";
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
//获取所有基地信息
var tenantResult = await _tenantApi.GetTenantsInternalAsync();

View File

@@ -1,8 +1,7 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
using Dlhis.ScheduledTask.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -17,7 +16,7 @@ public class CreateGetUploadRecordJob : BaseJob
{
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var devices = await DbContext.Queryable<DeviceEntity>()
.Where(x => x.Enable)

View File

@@ -1,8 +1,7 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
using Dlhis.ScheduledTask.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -17,7 +16,7 @@ public class CreateSyncServerDateTimeJob : BaseJob
{
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var deviceUniqueNos = await DbContext.Queryable<DeviceTaskEntity>()
.Where(x => x.Status == DeviceTaskStatusEnumType.Wait || x.Status == DeviceTaskStatusEnumType.Process)

View File

@@ -1,6 +1,5 @@
using Dlhis.Entity.Common;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
using Dlhis.ScheduledTask.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -15,7 +14,7 @@ public class DeleteApiLogTaskJob : BaseJob
{
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var before = DateTime.Now.Date.AddDays(-7);
await DbContext.ChangeDb("log").Client.Deleteable<ApiLogEntity>().Where(x => x._ts <= before).ExecuteCommandAsync();

View File

@@ -2,9 +2,9 @@
using System.Net.Sockets;
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.ScheduledTask.Attributes;
using Kurisu.AspNetCore.CustomClass;
using Kurisu.AspNetCore.Utils;
using Kurisu.Scheduled.Attributes;
using Newtonsoft.Json;
namespace Dlhis.ScheduledTask.Jobs;
@@ -22,7 +22,7 @@ public class HeartbeatJob : BaseJob
_redisCache = redisCache;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
const string key = "heartbeat-devices";
await _redisCache.KeyDeleteAsync(key);

View File

@@ -4,13 +4,13 @@ using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.Entity.Canteen.Entities;
using Dlhis.Entity.Canteen.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Request;
using Dlhis.ScheduledTask.Common.Services;
using Hangfire;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
using Kurisu.AspNetCore.Utils;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -31,7 +31,7 @@ public class MealSupplementHandlerJob : BaseJob
_jinkoJIPService = jinkoJIPService;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
//查询待处理餐补
var mealSupplements = await DbContext.Queryable<MealSupplementRecordEntity>()
@@ -49,11 +49,11 @@ public class MealSupplementHandlerJob : BaseJob
if (gainfo == null)
{
await DbContext.Updateable<MealSupplementRecordEntity>()
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Error)
.SetColumns(x => x.Result == "人员档案不存在")
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UniqueId == item.UniqueId)
.ExecuteCommandAsync();
.SetColumns(x => x.Status == MealSupplementStatusEnumtype.Error)
.SetColumns(x => x.Result == "人员档案不存在")
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UniqueId == item.UniqueId)
.ExecuteCommandAsync();
//后台任务获取人员
BackgroundJob.Enqueue<IGainfoUpdateService>(x => x.TodoByCodeAsync(item.Code));
@@ -65,14 +65,14 @@ public class MealSupplementHandlerJob : BaseJob
try
{
var r = await _jinkoJIPService.SyncMealSupplementRecordsAsync(new List<SyncMealSupplementRecordRequest>
{
new()
{
new()
{
UniqueId = item.UniqueId,
Status = "FAIL",
Msg = "人员离职"
}
});
UniqueId = item.UniqueId,
Status = "FAIL",
Msg = "人员离职"
}
});
await DbContext.Updateable<MealSupplementRecordEntity>()
.SetColumns(x => x.Result == r)
@@ -81,11 +81,9 @@ public class MealSupplementHandlerJob : BaseJob
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UniqueId == item.UniqueId)
.ExecuteCommandAsync();
}
catch
{
}
continue;
@@ -132,21 +130,20 @@ public class MealSupplementHandlerJob : BaseJob
var b = mealUser.Balance += mealSupplement.Amount;
await DbContext.Updateable<GainfoMealEntity>()
.SetColumns(x => x.Version == x.Version + 1)
.SetColumns(x => x.Balance == b)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UserCode == mealUser.UserCode && x.Version == mealUser.Version)
.ExecuteCommandThrowVersionAsync();
.SetColumns(x => x.Version == x.Version + 1)
.SetColumns(x => x.Balance == b)
.SetColumns(x => x.ModifiedTime == DateTime.Now)
.Where(x => x.UserCode == mealUser.UserCode && x.Version == mealUser.Version)
.ExecuteCommandThrowVersionAsync();
}, 5);
var r = await _jinkoJIPService.SyncMealSupplementRecordsAsync(new List<SyncMealSupplementRecordRequest>
{
new()
{
UniqueId = mealSupplement.UniqueId,
Status = "SUCCESS",
Msg = "操作成功"
UniqueId = mealSupplement.UniqueId,
Status = "SUCCESS",
Msg = "操作成功"
}
});
@@ -170,7 +167,7 @@ public class MealSupplementHandlerJob : BaseJob
CostDate = mealSupplement.Date,
CostDateTime = DateTime.Now,
UserNo = gainfo.No,
No = SnowFlakeHelper.Instance.NextId().ToString(),
No = mealSupplement.UniqueId, //使用DHR流水号
UserCode = gainfo.Code,
TenantId = mealSupplement.Factory,
Status = BalanceRecordStatusEnumType.Valid,
@@ -228,9 +225,9 @@ public class MealSupplementHandlerJob : BaseJob
{
new()
{
UniqueId = mealSupplement.UniqueId,
Status = "SUCCESS",
Msg = "操作成功"
UniqueId = mealSupplement.UniqueId,
Status = "SUCCESS",
Msg = "操作成功"
}
});
@@ -257,4 +254,4 @@ public class MealSupplementHandlerJob : BaseJob
.ExecuteCommandAsync();
}
}
}
}

View File

@@ -1,9 +1,7 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Dto;
using Dlhis.ScheduledTask.Common.Services;
using Hangfire;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -20,7 +18,7 @@ public class SyncOrganizationJob : BaseJob
_jinkoSapService = jinkoSapService;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var orgResult = await _jinkoSapService.GetSapOrganizationsAsync();

View File

@@ -1,12 +1,9 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Device.Entities;
using Dlhis.Entity.Device.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Request;
using Dlhis.ScheduledTask.Common.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
using Microsoft.Extensions.Logging;
using SqlSugar;
namespace Dlhis.ScheduledTask.Jobs;
@@ -24,7 +21,7 @@ public class SyncOriginCardInfoJob : BaseJob
_jinkoJIPService = jinkoJIPService;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
//考勤设备(不需要Enable参数)
var devices = await DbContext.Queryable<DeviceEntity>()

View File

@@ -1,9 +1,8 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Services;
using Hangfire;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -17,7 +16,7 @@ public class SyncPersonJob : BaseJob
{
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var werks = await DbContext.Queryable<WerkEntity>()
.Where(x => x.Enable)

View File

@@ -1,8 +1,7 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Dto;
using Dlhis.ScheduledTask.Common.Services;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -19,7 +18,7 @@ public class SyncPositionJob : BaseJob
_jinkoSapService = jinkoSapService;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var positionResult = await _jinkoSapService.GetPositionsAsync();

View File

@@ -1,9 +1,8 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Basic.Enums;
using Dlhis.ScheduledTask.Attributes;
using Dlhis.ScheduledTask.Common.Dto;
using Kurisu.AspNetCore.Utils;
using Kurisu.Scheduled;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Jobs;
@@ -20,7 +19,7 @@ public class SyncShanxiGainfoJob : BaseJob
_redisCache = redisCache;
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var key = "gainfo:shanxi";
await _redisCache.KeyDeleteAsync(key);

View File

@@ -1,8 +1,8 @@
using Dlhis.Entity.Basic.Entity;
using Dlhis.Entity.Sys.Entities;
using Dlhis.ScheduledTask.Attributes;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Scheduled.Attributes;
using Microsoft.AspNetCore.Identity;
namespace Dlhis.ScheduledTask.Jobs;
@@ -17,7 +17,7 @@ public class UpdateUserPasswordJob : BaseJob
{
}
public override async Task RunAsync(IServiceProvider serviceProvider)
protected override async Task RunAsync(IServiceProvider serviceProvider)
{
var identityDbContext = DbContext.ChangeDb("identity");

View File

@@ -1,10 +1,10 @@
using System.Linq.Expressions;
using System.Reflection;
using Dlhis.ScheduledTask.Attributes;
using Hangfire;
using Hangfire.Common;
using Kurisu.AspNetCore.Startup;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Scheduled.Attributes;
namespace Dlhis.ScheduledTask.Packs;