From 60ae4b2555793dda5b0172ebef37bc5191a0ea18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B5=87=E6=96=87=E9=BE=99?= Date: Tue, 19 Nov 2024 14:52:33 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E6=9C=80=E5=A4=A7=E9=87=8D?= =?UTF-8?q?=E8=AF=95=E6=AC=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ds-wms-service/DS.WMS.Core/ServiceBase.cs | 16 ++++-- .../TaskInteraction/Entity/TaskJob.cs | 18 +++++++ .../Method/FeeBillTaskService.cs | 13 ++--- .../TaskInteraction/Method/TaskService.cs | 54 +++++++++++++++---- 4 files changed, 77 insertions(+), 24 deletions(-) diff --git a/ds-wms-service/DS.WMS.Core/ServiceBase.cs b/ds-wms-service/DS.WMS.Core/ServiceBase.cs index 1fe72147..041211ae 100644 --- a/ds-wms-service/DS.WMS.Core/ServiceBase.cs +++ b/ds-wms-service/DS.WMS.Core/ServiceBase.cs @@ -25,7 +25,7 @@ namespace DS.WMS.Core /// /// 获取业务库访问对象 /// - protected ISaasDbService SaasService { get; private set; } + readonly ISaasDbService saasService; readonly Lazy commonService; @@ -37,17 +37,22 @@ namespace DS.WMS.Core { get { - _tenantDb ??= SaasService.GetBizDbScopeById(User.TenantId); + _tenantDb ??= saasService.GetBizDbScopeById(User.TenantId); return _tenantDb; } } - IUser _user; + readonly IUser _user; /// /// 获取用户相关信息 /// public virtual IUser User => _user; + /// + /// 当前用户ID + /// + public readonly long UserId; + /// /// 日志记录对象 /// @@ -61,8 +66,11 @@ namespace DS.WMS.Core { ServiceProvider = serviceProvider; _user = serviceProvider.GetRequiredService(); + if (_user != null) + UserId = long.Parse(User.UserId); + Db = serviceProvider.GetRequiredService(); - SaasService = serviceProvider.GetRequiredService(); + saasService = serviceProvider.GetRequiredService(); commonService = new Lazy(serviceProvider.GetRequiredService()); Logger = new Lazy>(serviceProvider.GetRequiredService>()); } diff --git a/ds-wms-service/DS.WMS.Core/TaskInteraction/Entity/TaskJob.cs b/ds-wms-service/DS.WMS.Core/TaskInteraction/Entity/TaskJob.cs index d4157a9e..a1a96d38 100644 --- a/ds-wms-service/DS.WMS.Core/TaskInteraction/Entity/TaskJob.cs +++ b/ds-wms-service/DS.WMS.Core/TaskInteraction/Entity/TaskJob.cs @@ -58,6 +58,24 @@ namespace DS.WMS.Core.TaskInteraction.Entity [SugarColumn(ColumnDescription = "创建时间", IsNullable = false)] public DateTime CreateTime { get; set; } + /// + /// 更新人 + /// + [SugarColumn(ColumnDescription = "更新人", IsNullable = true)] + public long? UpdateBy { get; set; } + + /// + /// 更新时间 + /// + [SugarColumn(ColumnDescription = "更新时间", IsNullable = true)] + public DateTime? UpdateTime { get; set; } + + /// + /// 重试次数 + /// + [SugarColumn(ColumnDescription = "重试次数", IsNullable = false)] + public int RetryTimes { get; set; } + /// /// 业务ID /// diff --git a/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/FeeBillTaskService.cs b/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/FeeBillTaskService.cs index 06de87cf..8cf17a9c 100644 --- a/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/FeeBillTaskService.cs +++ b/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/FeeBillTaskService.cs @@ -19,11 +19,6 @@ namespace DS.WMS.Core.TaskInteraction.Method /// public class FeeBillTaskService : TaskService, IFeeBillTaskService { - /// - /// 当前用户ID - /// - public readonly long UserId; - readonly ILogger logger; /// /// 初始化 @@ -32,7 +27,6 @@ namespace DS.WMS.Core.TaskInteraction.Method public FeeBillTaskService(IServiceProvider provider) : base(provider) { logger = provider.GetRequiredService>(); - UserId = long.Parse(User.UserId); } /// @@ -41,19 +35,20 @@ namespace DS.WMS.Core.TaskInteraction.Method /// /// /// - public override Task CreateTaskAsync(TaskCreationRequest request, bool useTransaction = true) + public override async Task CreateTaskAsync(TaskCreationRequest request, bool useTransaction = true) { if (request.TaskType == TaskBaseTypeEnum.BILL_SENDING) { + var interval = await GetConfigValueAsync(DEFAULT_JOB_INTERVAL, 60); request.Jobs ??= []; request.Jobs.Add(new TaskJob { TypeName = typeof(BillSendingTaskJob).FullName!, - NextTriggerTime = DateTime.Now.AddMinutes(60) + NextTriggerTime = DateTime.Now.AddMinutes(interval) }); } - return base.CreateTaskAsync(request, useTransaction); + return await base.CreateTaskAsync(request, useTransaction); } /// diff --git a/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/TaskService.cs b/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/TaskService.cs index 5a4f29d4..97c85bd1 100644 --- a/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/TaskService.cs +++ b/ds-wms-service/DS.WMS.Core/TaskInteraction/Method/TaskService.cs @@ -37,7 +37,8 @@ namespace DS.WMS.Core.TaskInteraction.Method public class TaskService : ServiceBase, ITaskService, IAuditTaskService { internal const long PERMISSION_ID = 1815294400855674880; - internal const string BillSendingInterval = "BillSendingInterval"; + internal const string DEFAULT_JOB_INTERVAL = "DefaultJobInterval"; + internal const string JOB_MAX_RETRY = "JOB_MAX_RETRY"; /// /// 获取支持审核的任务类型 @@ -688,10 +689,7 @@ namespace DS.WMS.Core.TaskInteraction.Method break; } - var value = await Db.Queryable().Where(x => x.Code == BillSendingInterval).Select(x => x.Value).FirstAsync(); - if (!int.TryParse(value, out int interval)) - interval = 60; - + var interval = await GetConfigValueAsync(DEFAULT_JOB_INTERVAL, 60); var list = await TenantDb.Queryable().Where(x => x.TaskId == task.Id && x.Status != TaskJobStatus.Completed) .Select(x => new TaskJob { @@ -703,7 +701,7 @@ namespace DS.WMS.Core.TaskInteraction.Method if (jobStatus == TaskJobStatus.Ready) //重新设置下次触发时间 foreach (var item in list) { - item.NextTriggerTime = item.NextTriggerTime.HasValue ? + item.NextTriggerTime = item.NextTriggerTime.HasValue ? item.NextTriggerTime.Value.AddMinutes(interval) : DateTime.Now.AddMinutes(interval); } @@ -1398,7 +1396,7 @@ namespace DS.WMS.Core.TaskInteraction.Method /// 备注 /// 任务类型,不指定任务类型则删除全部任务 /// - public async Task DeleteAsync(long id, BusinessType? businessType, string? remark = null, params TaskBaseTypeEnum[] taskTypes) + public virtual async Task DeleteAsync(long id, BusinessType? businessType, string? remark = null, params TaskBaseTypeEnum[] taskTypes) { if (!await HasAuthorizedAsync()) return DataResult.SuccessedWithDesc(nameof(MultiLanguageConst.ModuleUnauthorized)); @@ -1471,15 +1469,40 @@ namespace DS.WMS.Core.TaskInteraction.Method await ManagerService.SetTaskBaseInfoPropertyWithBsno(id, taskType, null, null, x => x.TASK_DESP == description); } + /// + /// 获取系统配置值 + /// + /// 值类型 + /// 配值名称 + /// 未获取到配值时的默认值 + /// + protected internal async Task GetConfigValueAsync(string key, T? defaultValue = default) + { + var value = await Db.Queryable().Where(x => x.Code == key).Select(x => x.Value).FirstAsync(); + if (string.IsNullOrEmpty(value)) + return defaultValue; + + try + { + return (T)Convert.ChangeType(value, typeof(T)); + } + catch + { + return defaultValue; + } + } + /// /// 运行定时任务 /// /// - public async Task RunJobAsync() + public async Task RunJobAsync() { + var maxRetry = await GetConfigValueAsync(JOB_MAX_RETRY, 10); var jobs = await TenantDb.Queryable() .InnerJoin((x, y) => x.TaskId == y.Id) - .Where((x, y) => y.TaskStatus == TaskStatusEnum.Create && x.Status == TaskJobStatus.Ready && x.NextTriggerTime <= SqlFunc.GetDate()) + .Where((x, y) => y.TaskStatus == TaskStatusEnum.Create && x.Status == TaskJobStatus.Ready && + x.NextTriggerTime <= SqlFunc.GetDate() && x.RetryTimes <= maxRetry) .Select((x, y) => new TaskJob { Id = x.Id, @@ -1492,6 +1515,8 @@ namespace DS.WMS.Core.TaskInteraction.Method if (jobs.Count == 0) return; + long userId = long.Parse(User.UserId); + var dtNow = DateTime.Now; var jobContext = new TaskJobContext(); await TenantDb.Ado.BeginTranAsync(); try @@ -1514,6 +1539,7 @@ namespace DS.WMS.Core.TaskInteraction.Method catch (Exception ex) { item.LastException = $"执行作业({item.Id})时发生错误:" + ex.Message + Environment.NewLine + ex.StackTrace; + item.RetryTimes++; jobContext.Result = DataResult.Failed(item.LastException); continue; } @@ -1523,6 +1549,9 @@ namespace DS.WMS.Core.TaskInteraction.Method item.Status = TaskJobStatus.Completed; item.NextTriggerTime = null; } + + item.UpdateBy = userId; + item.UpdateTime = dtNow; } } @@ -1530,7 +1559,10 @@ namespace DS.WMS.Core.TaskInteraction.Method { x.Status, x.NextTriggerTime, - x.LastException + x.LastException, + x.RetryTimes, + x.UpdateBy, + x.UpdateTime }).ExecuteCommandAsync(); var finishedJobs = jobs.FindAll(x => x.Status == TaskJobStatus.Completed); @@ -1545,8 +1577,8 @@ namespace DS.WMS.Core.TaskInteraction.Method x.TaskId, x.Status }).ToListAsync(); + var groups = list.GroupBy(x => x.TaskId); - DateTime dtNow = DateTime.Now; foreach (var group in groups) { if (group.Count() == group.Count(x => x.Status == TaskJobStatus.Completed))