From 6a81977eea7f2c219618c1bccbf9ea5a647e977a Mon Sep 17 00:00:00 2001 From: stevelee Date: Fri, 13 May 2022 11:38:02 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=B3=A8=E5=85=A5=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...BPA.SaaS.TaskSchedule.Api.Bootstrap.csproj | 3 + .../SetupBootstrap.cs | 27 +- .../SetupRabbitMqConfig.cs | 97 ++++-- .../SetupTaskScheduleConfig.cs | 2 - .../ISystemDictionaryService.cs | 2 +- .../BPA.SaaS.TaskSchedule.Api.Model.csproj | 2 +- .../TaskScheduleApiConfig.cs | 9 +- .../BPA.SaaS.TaskSchedule.Api.Service.csproj | 2 +- .../Queue/SynDataTaskScheduleConsumer.cs | 2 +- .../SystemDictionaryService.cs | 305 +++++++++--------- .../TaskInfoService.cs | 25 +- .../Controllers/HomeController.cs | 71 ++-- 12 files changed, 286 insertions(+), 261 deletions(-) diff --git a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/BPA.SaaS.TaskSchedule.Api.Bootstrap.csproj b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/BPA.SaaS.TaskSchedule.Api.Bootstrap.csproj index c5a51eb..86b6d50 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/BPA.SaaS.TaskSchedule.Api.Bootstrap.csproj +++ b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/BPA.SaaS.TaskSchedule.Api.Bootstrap.csproj @@ -5,6 +5,9 @@ enable enable + + 1701;1702;8602 + diff --git a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupBootstrap.cs b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupBootstrap.cs index 08d25be..71246d7 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupBootstrap.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupBootstrap.cs @@ -8,7 +8,9 @@ using BPA.Component.Extensions; using BPA.Component.LogClient.Extensions; using BPA.Component.SDKCommon; using BPA.Component.WebApiExtensions.Extensions; +using BPA.SaaS.TaskSchedule.Api.DTO; using BPA.SaaS.TaskSchedule.Api.Model; +using BPA.SaaS.TaskSchedule.Api.Repository; using BPA.SaaS.TaskSchedule.Api.Service; using BPA.SaaS.TaskSchedule.Api.Service.QzScheduler; using BPA.SaaS.TaskSchedule.Api.Service.WarnAlarms; @@ -58,13 +60,14 @@ public static class SetupBootstrap services.AddControllersWithViews(mvcOptions => { mvcOptions.AddDefaultMvcOptions(true); }).AddRazorRuntimeCompilation(); services.AddEndpointsApiExplorer(); services.AddServiceSDK(_configuration); + services.AddLangPackage(); services.AddHealthChecks(); services.SetupTaskSchedule(); services.AddHttpClient(typeof(SynRequestHttpJobScheduler).FullName); services.AddHttpClient(typeof(AnyTimeTaskScheduleExecutorService).FullName); services.AddHttpClient(typeof(WechatWebhookWarnAlarm).FullName); - + // services.AddCap(x => // { // var config = services.GetPreConfig(args); @@ -99,16 +102,17 @@ public static class SetupBootstrap public static ContainerBuilder SetupConfigureContainer(this ContainerBuilder builder) { // 注入DB - // builder.RegisterType().InstancePerLifetimeScope(); - // // 服务层 - // builder.RegisterAssemblyTypes(typeof(HomeService).Assembly) - // .Where(t => t.Name.EndsWith("Service")) - // .AsImplementedInterfaces(); - // - // // 仓储层 - // builder.RegisterAssemblyTypes(typeof(HomeRepository).Assembly) - // .Where(t => t.Name.EndsWith("Repository")) - // .AsImplementedInterfaces(); + builder.RegisterType().InstancePerLifetimeScope(); + + // 服务层 + builder.RegisterAssemblyTypes(typeof(SystemDictionaryService).Assembly) + .Where(t => t.Name.EndsWith("Service")) + .AsImplementedInterfaces(); + + // 仓储层 + builder.RegisterAssemblyTypes(typeof(SystemDictionaryRepository).Assembly) + .Where(t => t.Name.EndsWith("Repository")) + .AsImplementedInterfaces(); return builder; } @@ -124,6 +128,7 @@ public static class SetupBootstrap serviceProvider.PreHeatRunRabbitMQ(); serviceProvider.SetDebug(true); serviceProvider.GetService(); + serviceProvider.PreHeatTaskSchedule(); } /// diff --git a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupRabbitMqConfig.cs b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupRabbitMqConfig.cs index eb5bb42..e4b99cc 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupRabbitMqConfig.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupRabbitMqConfig.cs @@ -1,45 +1,78 @@ using BPA.Common.Infrastructure.BizLogs; using BPA.Common.Infrastructure.Queue; +using BPA.Component.RabbitMQClient.Enums; using BPA.Component.RabbitMQClient.Extensions; using BPA.Component.RabbitMQClient.Provider; +using BPA.SaaS.TaskSchedule.Api.IService; using BPA.SaaS.TaskSchedule.Api.Model; +using BPA.SaaS.TaskSchedule.Api.Service.Queue; using Microsoft.Extensions.DependencyInjection; -namespace BPA.SaaS.TaskSchedule.Api.Bootstrap +namespace BPA.SaaS.TaskSchedule.Api.Bootstrap; + +/// +/// RabbitMQ配置 +/// +public static class SetupRabbitMqConfig { /// - /// RabbitMQ配置 + /// 使用消息队列 + /// + /// + /// + public static IServiceCollection AddRabbitMQ(this IServiceCollection services) + { + services.AddRabbitMQProvider(sp => sp.GetService().RabbitMqConfig); + services.AddRabbitMQProducer(); + + services.AddRabbitMQProducer(); + services.AddRabbitMQConsumer(); + + services.AddRabbitMQProducer(); + services.AddRabbitMQConsumer(); + + return services; + } + + /// + /// 预热消息队列 /// - public static class SetupRabbitMqConfig + /// + /// + public static void PreHeatRunRabbitMQ(this IServiceProvider serviceProvider) { - /// - /// 使用消息队列 - /// - /// - /// - public static IServiceCollection AddRabbitMQ(this IServiceCollection services) - { - //注入 RabbitMQProvider - services.AddRabbitMQProvider(sp => sp.GetService()?.RabbitMqConfig); - //注入 业务日志 生产者 - services.AddRabbitMQProducer(); - - return services; - } - - /// - /// 预热消息队列 - /// - /// - /// - public static IServiceProvider PreHeatRunRabbitMQ(this IServiceProvider serviceProvider) - { - var rabbitMqProvider = serviceProvider.UseRabbitMQProvider(); - var exchangeConfig = rabbitMqProvider.BuildExchangeConfig(MqNameConfig.BizLogExchange); - exchangeConfig.UseExchange(); - exchangeConfig.UseProducer(); - - return serviceProvider; - } + var rabbitMqProvider = serviceProvider.UseRabbitMQProvider(); + + var exchangeConfig = rabbitMqProvider.BuildExchangeConfig(MqNameConfig.BizLogExchange); + exchangeConfig.UseExchange(); + exchangeConfig.UseProducer(); + + #region SynData + + var synDataExchangeConfig = rabbitMqProvider.BuildExchangeConfig(MqNameConfig.SynDataTaskScheduleExchange, + ExchangeType.Fanout); + synDataExchangeConfig.UseExchange(); + synDataExchangeConfig.UseProducer(); + + var synDataQueueConfig = rabbitMqProvider.BuildQueueConfig(MqNameConfig.SynDataTaskScheduleQueue, + MqNameConfig.SynDataTaskScheduleExchange); + synDataQueueConfig.UseQueue(); + synDataQueueConfig.UseConsumer(); + + #endregion + + #region AnyTime + + var anyTimeExchangeConfig = rabbitMqProvider.BuildExchangeConfig(MqNameConfig.AnyTimeTaskScheduleExchange, + ExchangeType.X_Delayed_Message); + anyTimeExchangeConfig.UseExchange(); + anyTimeExchangeConfig.UseProducer(); + + var anyTimeQueueConfig = rabbitMqProvider.BuildQueueConfig(MqNameConfig.AnyTimeTaskScheduleQueue, + MqNameConfig.AnyTimeTaskScheduleExchange); + anyTimeQueueConfig.UseQueue(); + anyTimeQueueConfig.UseMultipleConsumer(5); + + #endregion } } \ No newline at end of file diff --git a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupTaskScheduleConfig.cs b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupTaskScheduleConfig.cs index 9ae0e79..e4a73ba 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupTaskScheduleConfig.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Bootstrap/SetupTaskScheduleConfig.cs @@ -31,13 +31,11 @@ namespace BPA.SaaS.TaskSchedule.Api.Bootstrap /// public static void PreHeatTaskSchedule(this IServiceProvider serviceProvider) { - //LogProvider.SetCurrentLogProvider(new QuartzLogProvider()); //初始化字典 serviceProvider.GetService().Start(); //启动任务 serviceProvider.GetService().Start(); //启动日志任务 - //serviceProvider.GetService().Start(); } } diff --git a/src/BPA.SaaS.TaskSchedule.Api.IService/ISystemDictionaryService.cs b/src/BPA.SaaS.TaskSchedule.Api.IService/ISystemDictionaryService.cs index 83c9383..65411c6 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.IService/ISystemDictionaryService.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.IService/ISystemDictionaryService.cs @@ -7,7 +7,7 @@ namespace BPA.SaaS.TaskSchedule.Api.IService /// /// 系统字典 /// - public interface ISystemDictionaryService : ISynData + public interface ISystemDictionaryService /*: ISynData*/ { /// /// 保存 diff --git a/src/BPA.SaaS.TaskSchedule.Api.Model/BPA.SaaS.TaskSchedule.Api.Model.csproj b/src/BPA.SaaS.TaskSchedule.Api.Model/BPA.SaaS.TaskSchedule.Api.Model.csproj index 742a192..5e4d8f6 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Model/BPA.SaaS.TaskSchedule.Api.Model.csproj +++ b/src/BPA.SaaS.TaskSchedule.Api.Model/BPA.SaaS.TaskSchedule.Api.Model.csproj @@ -13,7 +13,7 @@ - + diff --git a/src/BPA.SaaS.TaskSchedule.Api.Model/TaskScheduleApiConfig.cs b/src/BPA.SaaS.TaskSchedule.Api.Model/TaskScheduleApiConfig.cs index 66f4f58..084c063 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Model/TaskScheduleApiConfig.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Model/TaskScheduleApiConfig.cs @@ -2,7 +2,6 @@ using System.Net.NetworkInformation; using System.Net.Sockets; using BPA.Component.ApolloClient; -using BPA.Component.DbClient; using BPA.Component.MongoClient; using BPA.Component.RabbitMQClient.Connection; using Com.Ctrip.Framework.Apollo; @@ -34,17 +33,11 @@ public class TaskScheduleApiConfig : ApolloBPAConfig [AutoWrite] public MongoConfig TaskScheduleMongoConfig { get; set; } - /// - /// 环境名称 测试 | 生产 - /// - [AutoWrite] - public string EvnName { private set; get; } - /// /// 报警器 /// [AutoWrite] - public List WarnAlarms { private set; get; } = new List(); + public List WarnAlarms { private set; get; } = new(); /// /// 跟踪ID head名称 diff --git a/src/BPA.SaaS.TaskSchedule.Api.Service/BPA.SaaS.TaskSchedule.Api.Service.csproj b/src/BPA.SaaS.TaskSchedule.Api.Service/BPA.SaaS.TaskSchedule.Api.Service.csproj index 4f95526..a3394cd 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Service/BPA.SaaS.TaskSchedule.Api.Service.csproj +++ b/src/BPA.SaaS.TaskSchedule.Api.Service/BPA.SaaS.TaskSchedule.Api.Service.csproj @@ -6,7 +6,7 @@ enable - + diff --git a/src/BPA.SaaS.TaskSchedule.Api.Service/Queue/SynDataTaskScheduleConsumer.cs b/src/BPA.SaaS.TaskSchedule.Api.Service/Queue/SynDataTaskScheduleConsumer.cs index e6220ce..28e8743 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Service/Queue/SynDataTaskScheduleConsumer.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Service/Queue/SynDataTaskScheduleConsumer.cs @@ -30,7 +30,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service.Queue { //字典 var service = scope.ServiceProvider.GetRequiredService(); - service.OnDataChange(msgBody); + // service.OnDataChange(msgBody); } else if (msgBody.DataType == 2) { diff --git a/src/BPA.SaaS.TaskSchedule.Api.Service/SystemDictionaryService.cs b/src/BPA.SaaS.TaskSchedule.Api.Service/SystemDictionaryService.cs index bcb5dc9..4d4b38e 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Service/SystemDictionaryService.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Service/SystemDictionaryService.cs @@ -10,176 +10,173 @@ using BPA.SaaS.TaskSchedule.Api.Service.Queue; using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; -namespace BPA.SaaS.TaskSchedule.Api.Service +namespace BPA.SaaS.TaskSchedule.Api.Service; + +public class SystemDictionaryService : ISystemDictionaryService { - public class SystemDictionaryService : ISystemDictionaryService + private readonly Lazy systemDictionaryRepository; + private readonly TaskScheduleLangPackge langPackage; + private static readonly MemoryCache memoryCache = new(Options.Create(new MemoryCacheOptions())); + private readonly SynDataTaskScheduleProducer synDataTaskScheduleProducer; + + public SystemDictionaryService(TaskScheduleLangPackge langPackage, + Lazy systemDictionaryRepository, + SynDataTaskScheduleProducer synDataTaskScheduleProducer) { - private readonly Lazy systemDictionaryRepository; - private readonly TaskScheduleLangPackge langPackge; - private readonly static MemoryCache memoryCache = new MemoryCache(Options.Create(new MemoryCacheOptions())); - private readonly SynDataTaskScheduleProducer synDataTaskScheduleProducer; + this.langPackage = langPackage; + this.systemDictionaryRepository = systemDictionaryRepository; + this.synDataTaskScheduleProducer = synDataTaskScheduleProducer; + } - public SystemDictionaryService( - TaskScheduleLangPackge langPackge, - Lazy systemDictionaryRepository, - SynDataTaskScheduleProducer synDataTaskScheduleProducer) + /// + /// 保存 + /// + /// + /// + public BaseResult Save(SaveSystemDictionaryReq req) + { + if (req.IsNull()) + return BaseResult.Build(langPackage.ParamInvalid); + req.Key = req.Key.ToTrim(); + var cmd = 1; + if (req.Id == 0) { - this.langPackge = langPackge; - this.systemDictionaryRepository = systemDictionaryRepository; - this.synDataTaskScheduleProducer = synDataTaskScheduleProducer; + var has = systemDictionaryRepository.Value.Queryable() + .Any(x => x.Type == req.Type && x.Key == req.Key && x.IsDelete == false); + if (has) + return BaseResult.Build(langPackage.KeyExist); + var entity = new CrmSystemDictionary + { + Id = BPAUniqueIdBulder.NextLong(), + Key = req.Key, + Type = req.Type, + Value = req.Value.ToTrim(), + ValueExt = req.ValueExt.ToTrim(), + IsDelete = false + }; + req.Id = entity.Id; + systemDictionaryRepository.Value.Add(entity); + cmd = 1; } - - /// - /// 保存 - /// - /// - /// - public BaseResult Save(SaveSystemDictionaryReq req) + else { - if (req.IsNull()) - return BaseResult.Build(langPackge.ParamInvalid); - req.Key = req.Key.ToTrim(); - var cmd = 1; - if (req.Id == 0) + var entity = systemDictionaryRepository.Value.Queryable().FirstOrDefault(x => x.Id == req.Id && x.IsDelete == false); + if (entity == null) + return BaseResult.Build(langPackage.KeyNotExist); + var has = systemDictionaryRepository.Value.Queryable() + .Any(x => x.Id != req.Id && x.Type == req.Type && x.Key == req.Key && x.IsDelete == false); + if (has) + return BaseResult.Build(langPackage.KeyExist); + req.Value = req.Value.ToTrim(); + req.ValueExt = req.ValueExt.ToTrim(); + systemDictionaryRepository.Value.UpdateOne(x => new CrmSystemDictionary { - var has = systemDictionaryRepository.Value.Queryable() - .Any(x => x.Type == req.Type && x.Key == req.Key && x.IsDelete == false); - if (has) - return BaseResult.Build(langPackge.KeyExist); - var entity = new CrmSystemDictionary - { - Id = BPAUniqueIdBulder.NextLong(), - Key = req.Key, - Type = req.Type, - Value = req.Value.ToTrim(), - ValueExt = req.ValueExt.ToTrim(), - IsDelete = false - }; - req.Id = entity.Id; - systemDictionaryRepository.Value.Add(entity); - cmd = 1; - } - else - { - var entity = systemDictionaryRepository.Value.Queryable().FirstOrDefault(x => x.Id == req.Id && x.IsDelete == false); - if (entity == null) - return BaseResult.Build(langPackge.KeyNotExist); - var has = systemDictionaryRepository.Value.Queryable() - .Any(x => x.Id != req.Id && x.Type == req.Type && x.Key == req.Key && x.IsDelete == false); - if (has) - return BaseResult.Build(langPackge.KeyExist); - req.Value = req.Value.ToTrim(); - req.ValueExt = req.ValueExt.ToTrim(); - systemDictionaryRepository.Value.UpdateOne(x => new CrmSystemDictionary - { - Type = req.Type, - Value = req.Value, - ValueExt = req.ValueExt - }, x => x.Id == req.Id); - cmd = 2; - } - - //触发数据改变 - synDataTaskScheduleProducer.Enqueue(new SynDataTaskScheduleMsgBody {Cmd = cmd, DataId = req.Id, DataType = 1}); - Thread.Sleep(1000); - return BaseResult.BuildOK(); + Type = req.Type, + Value = req.Value, + ValueExt = req.ValueExt + }, x => x.Id == req.Id); + cmd = 2; } - /// - /// 删除 - /// - /// - /// - public BaseResult Delete(DeleteSystemDictionaryReq req) - { - var entity = systemDictionaryRepository.Value.Queryable().First(s => s.Id == req.Id); - if (entity == null || entity.IsDelete) - return BaseResult.Build(langPackge.KeyNotExist); - //更改为删除 - systemDictionaryRepository.Value.UpdateOne(x => new CrmSystemDictionary {IsDelete = true}, x => x.Id == req.Id); - //触发数据改变 - synDataTaskScheduleProducer.Enqueue(new SynDataTaskScheduleMsgBody {Cmd = 3, DataId = req.Id, DataType = 1}); - Thread.Sleep(1000); - return BaseResult.BuildOK(); - } + //触发数据改变 + synDataTaskScheduleProducer.Enqueue(new SynDataTaskScheduleMsgBody {Cmd = cmd, DataId = req.Id, DataType = 1}); + Thread.Sleep(1000); + return BaseResult.BuildOK(); + } - /// - /// 获取所有的 - /// - /// - public BaseResult> GetAll() - { - var rows = systemDictionaryRepository.Value.Queryable().Where(x => x.IsDelete == false).Select(x => new SystemDictionaryDTO - { - Id = x.Id, - Type = x.Type, - Key = x.Key, - Value = x.Value, - ValueExt = x.ValueExt - }).ToList(); - return BaseResult>.BuildOK(rows); - } + /// + /// 删除 + /// + /// + /// + public BaseResult Delete(DeleteSystemDictionaryReq req) + { + var entity = systemDictionaryRepository.Value.Queryable().First(s => s.Id == req.Id); + if (entity == null || entity.IsDelete) + return BaseResult.Build(langPackage.KeyNotExist); + //更改为删除 + systemDictionaryRepository.Value.UpdateOne(x => new CrmSystemDictionary {IsDelete = true}, x => x.Id == req.Id); + //触发数据改变 + synDataTaskScheduleProducer.Enqueue(new SynDataTaskScheduleMsgBody {Cmd = 3, DataId = req.Id, DataType = 1}); + Thread.Sleep(1000); + return BaseResult.BuildOK(); + } - /// - /// 获取缓存的字典信息 - /// - /// - /// - /// - public SystemDictionaryDTO GetCache(EnumSystemDictionaryType type, string key) + /// + /// 获取所有的 + /// + /// + public BaseResult> GetAll() + { + var rows = systemDictionaryRepository.Value.Queryable().Where(x => x.IsDelete == false).Select(x => new SystemDictionaryDTO { - var rkey = TaskScheduleRedisKey.DictionaryKey(type, key); - return memoryCache.Get(rkey); - } + Id = x.Id, + Type = x.Type, + Key = x.Key, + Value = x.Value, + ValueExt = x.ValueExt + }).ToList(); + return BaseResult>.BuildOK(rows); + } - /// - /// 当数据改变 - /// - /// - public void OnDataChange(SynDataTaskScheduleMsgBody msgBody) - { - var entity = systemDictionaryRepository.Value.Queryable().FirstOrDefault(f => f.Id == msgBody.DataId); - if (entity == null) return; - var rkey = TaskScheduleRedisKey.DictionaryKey(entity.Type, entity.Key); - if (entity.IsDelete) - memoryCache.Remove(rkey); - else - memoryCache.Set(rkey, new SystemDictionaryDTO - { - Id = entity.Id, - Key = entity.Key, - Type = entity.Type, - Value = entity.Value, - ValueExt = entity.ValueExt - }); - } + /// + /// 获取缓存的字典信息 + /// + /// + /// + /// + public SystemDictionaryDTO GetCache(EnumSystemDictionaryType type, string key) + { + var rkey = TaskScheduleRedisKey.DictionaryKey(type, key); + return memoryCache.Get(rkey); + } - /// - /// 启动执行一次 - /// - public void Start() - { - // systemDictionaryRepository.Value.UpdateMany(x => new CrmSystemDictionary { IsDelete = false }, x => x.Id > 0); - var rows = systemDictionaryRepository.Value.Queryable().Where(x => x.IsDelete == false).Select(x => new SystemDictionaryDTO + /// + /// 当数据改变 + /// + /// + public void OnDataChange(SynDataTaskScheduleMsgBody msgBody) + { + var entity = systemDictionaryRepository.Value.Queryable().FirstOrDefault(f => f.Id == msgBody.DataId); + if (entity == null) return; + var rkey = TaskScheduleRedisKey.DictionaryKey(entity.Type, entity.Key); + if (entity.IsDelete) + memoryCache.Remove(rkey); + else + memoryCache.Set(rkey, new SystemDictionaryDTO { - Id = x.Id, - Type = x.Type, - Key = x.Key, - Value = x.Value, - ValueExt = x.ValueExt - }).ToList(); - rows.Each(item => + Id = entity.Id, + Key = entity.Key, + Type = entity.Type, + Value = entity.Value, + ValueExt = entity.ValueExt + }); + } + + /// + /// 启动执行一次 + /// + public void Start() + { + var rows = systemDictionaryRepository.Value.Queryable().Where(x => x.IsDelete == false).Select(x => new SystemDictionaryDTO + { + Id = x.Id, + Type = x.Type, + Key = x.Key, + Value = x.Value, + ValueExt = x.ValueExt + }).ToList(); + rows.Each(item => + { + var rkey = TaskScheduleRedisKey.DictionaryKey(item.Type, item.Key); + memoryCache.Set(rkey, new SystemDictionaryDTO { - var rkey = TaskScheduleRedisKey.DictionaryKey(item.Type, item.Key); - memoryCache.Set(rkey, new SystemDictionaryDTO - { - Id = item.Id, - Key = item.Key, - Type = item.Type, - Value = item.Value, - ValueExt = item.ValueExt - }); + Id = item.Id, + Key = item.Key, + Type = item.Type, + Value = item.Value, + ValueExt = item.ValueExt }); - } + }); } } \ No newline at end of file diff --git a/src/BPA.SaaS.TaskSchedule.Api.Service/TaskInfoService.cs b/src/BPA.SaaS.TaskSchedule.Api.Service/TaskInfoService.cs index d9c4fbe..b5a8c3d 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.Service/TaskInfoService.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.Service/TaskInfoService.cs @@ -27,7 +27,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service { private static readonly MemoryCache memoryCache = new(Options.Create(new MemoryCacheOptions())); private readonly Lazy taskInfoRepository; - private readonly TaskScheduleLangPackge langPackge; + private readonly TaskScheduleLangPackge langPackage; private readonly Lazy schedulerManager; private readonly Lazy taskLogRepository; private readonly Lazy taskLogService; @@ -38,8 +38,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service /// /// 构造方法 /// - public TaskInfoService( - TaskScheduleLangPackge langPackge, + public TaskInfoService(TaskScheduleLangPackge langPackage, Lazy taskInfoRepository, Lazy schedulerManager, Lazy taskLogRepository, @@ -48,7 +47,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service SynDataTaskScheduleProducer synDataTaskScheduleProducer, Lazy systemDictionaryService) { - this.langPackge = langPackge; + this.langPackage = langPackage; this.taskInfoRepository = taskInfoRepository; this.schedulerManager = schedulerManager; this.taskLogRepository = taskLogRepository; @@ -66,24 +65,24 @@ namespace BPA.SaaS.TaskSchedule.Api.Service public BaseResult Save(SaveTaskInfoReq req) { if (req.IsNull()) - return BaseResult.Build(langPackge.ParamInvalid); + return BaseResult.Build(langPackage.ParamInvalid); var isNew = req.Id <= 0; req.Name = req.Name.ToTrim(); req.BizSysApiName = req.BizSysApiName.ToTrim(); req.RequestURL = req.RequestURL.ToTrim(); if (req.RequestURL.IsNull()) - return BaseResult.Build(langPackge.ParamInvalid, "请求地址不能为空!"); + return BaseResult.Build(langPackage.ParamInvalid, "请求地址不能为空!"); if (req.RequestURLType == EnumRequestURLType.BizSysApi && req.BizSysApiName.IsNull()) - return BaseResult.Build(langPackge.ParamInvalid, "业务系统Api名称不能为空!"); + return BaseResult.Build(langPackage.ParamInvalid, "业务系统Api名称不能为空!"); if (req.RequestURLType == EnumRequestURLType.BizSysApi && !req.RequestURL.StartsWith("/")) - return BaseResult.Build(langPackge.ParamInvalid, "请求地址不能以 / 开头!"); + return BaseResult.Build(langPackage.ParamInvalid, "请求地址不能以 / 开头!"); if (req.RequestURLType == EnumRequestURLType.Custom && !req.RequestURL.ToLower().StartsWith("http")) - return BaseResult.Build(langPackge.ParamInvalid, "请求地址必须以http开头!"); + return BaseResult.Build(langPackage.ParamInvalid, "请求地址必须以http开头!"); if (req.TriggerType == EnumTriggerType.CronTrigger) { (var checkResult, var msg) = schedulerManager.Value.CheckCronExpression(req.CronExpression); if (!checkResult) - return BaseResult.Build(langPackge.ParamInvalid, $"Cron表达式校验错误:{msg}!"); + return BaseResult.Build(langPackage.ParamInvalid, $"Cron表达式校验错误:{msg}!"); } //校验任务名称是否存在 @@ -91,7 +90,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service .WhereIF(!isNew, x => x.Id != req.Id) .Any(x => x.Name == req.Name && x.IsDelete == false); if (hasName) - return BaseResult.Build(langPackge.TaskExist); + return BaseResult.Build(langPackage.TaskExist); if (req.TaskType == EnumTaskType.AnyTime) req.TriggerType = EnumTriggerType.Nothing; var statusIsChange = false; @@ -132,7 +131,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service { entity = taskInfoRepository.Value.Queryable().FirstOrDefault(x => x.Id == req.Id); if (entity.IsNull()) - return BaseResult.Build(langPackge.TaskNotExist); + return BaseResult.Build(langPackage.TaskNotExist); //检查状态是否改变 statusIsChange = entity.Status != req.Status; entity.BizSysApiName = req.BizSysApiName; @@ -202,7 +201,7 @@ namespace BPA.SaaS.TaskSchedule.Api.Service { var dto = GetCache(req.Id); if (dto.IsNull()) - return BaseResult.Build(langPackge.TaskNotExist); + return BaseResult.Build(langPackage.TaskNotExist); //标记任务删除 taskInfoRepository.Value.UpdateOne(x => new CrmTaskInfo {IsDelete = true}, x => x.Id == req.Id); if (dto.TaskType == EnumTaskType.AnyTime) diff --git a/src/BPA.SaaS.TaskSchedule.Api.WebApi/Controllers/HomeController.cs b/src/BPA.SaaS.TaskSchedule.Api.WebApi/Controllers/HomeController.cs index 1ef5be7..df0006d 100644 --- a/src/BPA.SaaS.TaskSchedule.Api.WebApi/Controllers/HomeController.cs +++ b/src/BPA.SaaS.TaskSchedule.Api.WebApi/Controllers/HomeController.cs @@ -20,28 +20,25 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers /// /// taskInfoService /// - private Lazy taskInfoService; + private ITaskInfoService taskInfoService; /// /// taskLogService /// - private Lazy taskLogService; - - private Lazy systemDictionaryService; + private ITaskLogService taskLogService; + /// + /// systemDictionaryService + /// + private ISystemDictionaryService systemDictionaryService; /// /// HomeController /// - /// - /// - /// - /// - public HomeController( - ILogger logger, - Lazy taskInfoService, - Lazy taskLogService, - Lazy systemDictionaryService) + public HomeController(ILogger logger, + ITaskInfoService taskInfoService, + ITaskLogService taskLogService, + ISystemDictionaryService systemDictionaryService) { _logger = logger; this.taskInfoService = taskInfoService; @@ -59,12 +56,12 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers if (data is BaseResult result) { result.TrackId = Request.HttpContext.TraceIdentifier; - result.Render(LangType.Zn); + result.Render(); } - + return base.Json(data); } - + /// /// Index /// @@ -73,7 +70,7 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers { return View(); } - + /// /// 保存 /// @@ -82,9 +79,9 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers [HttpPost] public IActionResult Save(SaveTaskInfoReq req) { - return Json(taskInfoService.Value.Save(req)); + return Json(taskInfoService.Save(req)); } - + /// /// 删除 /// @@ -93,9 +90,9 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers [HttpPost] public IActionResult Delete(DeleteTaskInfoReq req) { - return Json(taskInfoService.Value.Delete(req)); + return Json(taskInfoService.Delete(req)); } - + /// /// 获取所有的任务信息 /// @@ -103,9 +100,9 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers [HttpPost] public IActionResult GetAll() { - return Json(taskInfoService.Value.GetAll()); + return Json(taskInfoService.GetAll()); } - + /// /// 执行一次 /// @@ -114,9 +111,9 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers [HttpPost] public IActionResult DoOnce(DoOnceReq req) { - return Json(taskInfoService.Value.DoOnce(req)); + return Json(taskInfoService.DoOnce(req)); } - + /// /// 添加任意时间任务日志 /// @@ -124,18 +121,18 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers /// [HttpPost] public IActionResult AddAnyTimeLog(AddAnyTimeLogReq req) - => Json(taskLogService.Value.AddAnyTimeLog(req)); - + => Json(taskLogService.AddAnyTimeLog(req)); + /// /// 清理日志 /// [HttpPost] public IActionResult Clear() { - taskLogService.Value.Clear(); + taskLogService.Clear(); return Ok(); } - + /// /// 分页查询日志 /// @@ -143,30 +140,30 @@ namespace BPA.SaaS.TaskSchedule.Api.WebApi.Controllers /// [HttpPost] public IActionResult GetPage(GetTaskLogPageReq req) - => Json(taskLogService.Value.GetPage(req)); - - + => Json(taskLogService.GetPage(req)); + + /// /// 保存 /// /// /// [HttpPost] - public IActionResult SaveDic(SaveSystemDictionaryReq req) => Json(systemDictionaryService.Value.Save(req)); - + public IActionResult SaveDic(SaveSystemDictionaryReq req) => Json(systemDictionaryService.Save(req)); + /// /// 删除 /// /// /// [HttpPost] - public IActionResult DeleteDic(DeleteSystemDictionaryReq req) => Json(systemDictionaryService.Value.Delete(req)); - + public IActionResult DeleteDic(DeleteSystemDictionaryReq req) => Json(systemDictionaryService.Delete(req)); + /// /// 获取所有的 /// /// [HttpPost] - public IActionResult GetAllDic() => Json(systemDictionaryService.Value.GetAll()); + public IActionResult GetAllDic() => Json(systemDictionaryService.GetAll()); } } \ No newline at end of file