diff --git a/IRaCIS.Core.API/AnonymizeTagSetting.json b/IRaCIS.Core.API/AnonymizeTagSetting.json deleted file mode 100644 index 6576ca04a..000000000 --- a/IRaCIS.Core.API/AnonymizeTagSetting.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "needAnonymizeTag": [ - { //PatientsName - "Group": "0010", - "Element": "0010", - "ReplaceValue": "", - "Enable": true - }, - { // PatientID - "Group": "0010", - "Element": "0020", - "ReplaceValue": "", - "Enable": true - }, - { // IssuerOfPatientID - "Group": "0010", - "Element": "0021", - "ReplaceValue": "", - "Enable": true - }, - { // PatientsBirthDate - "Group": "0010", - "Element": "0030", - "ReplaceValue": "", - "Enable": true - }, - { // PatientsBirthTime - "Group": "0010", - "Element": "0032", - "ReplaceValue": "", - "Enable": false - }, - { // PatientsSex - "Group": "0010", - "Element": "0040", - "ReplaceValue": "", - "Enable": false - }, - { // OtherPatientIDs - "Group": "0010", - "Element": "1000", - "ReplaceValue": "", - "Enable": false - }, - { // OtherPatientNames - "Group": "0010", - "Element": "1001", - "ReplaceValue": "", - "Enable": false - }, - { // OtherPatientNames - "Group": "0010", - "Element": "1005", - "ReplaceValue": "", - "Enable": true - }, - { // PatientBirthName - "Group": "0010", - "Element": "1005", - "ReplaceValue": "", - "Enable": true - }, - { // PatientsAge - "Group": "0010", - "Element": "1010", - "ReplaceValue": "", - "Enable": true - }, - { // PatientsAddress - "Group": "0010", - "Element": "1040", - "ReplaceValue": "", - "Enable": true - }, - { // PatientsMothersBirthName - "Group": "0010", - "Element": "1060", - "ReplaceValue": "", - "Enable": true - }, - { - "Group": "0010", - "Element": "2150", - "ReplaceValue": "", - "Enable": true - }, - { - "Group": "0010", - "Element": "2152", - "ReplaceValue": "", - "Enable": true - }, - { - "Group": "0010", - "Element": "2154", - "ReplaceValue": "", - "Enable": true - }, - { - "Group": "0012", - "Element": "0040", - "ReplaceValue": "XXX", - "Enable": true - } - ] -} \ No newline at end of file diff --git a/IRaCIS.Core.API/IRaCIS.Core.API.csproj b/IRaCIS.Core.API/IRaCIS.Core.API.csproj index a6bbf319e..843813572 100644 --- a/IRaCIS.Core.API/IRaCIS.Core.API.csproj +++ b/IRaCIS.Core.API/IRaCIS.Core.API.csproj @@ -108,9 +108,6 @@ - - Always - Always diff --git a/IRaCIS.Core.API/IRaCIS.Core.API.xml b/IRaCIS.Core.API/IRaCIS.Core.API.xml index ed6960728..50413058f 100644 --- a/IRaCIS.Core.API/IRaCIS.Core.API.xml +++ b/IRaCIS.Core.API/IRaCIS.Core.API.xml @@ -429,6 +429,12 @@ + + + 1:数据上传 2:导出、 3:邮件附件 4:邮件Html 通过 ----new + + + IPLimit限流 启动服务 diff --git a/IRaCIS.Core.API/NLog.config b/IRaCIS.Core.API/NLog.config deleted file mode 100644 index bf1dbbb1b..000000000 --- a/IRaCIS.Core.API/NLog.config +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/IRaCIS.Core.API/Program.cs b/IRaCIS.Core.API/Program.cs index b771b7a5f..ad2c3f425 100644 --- a/IRaCIS.Core.API/Program.cs +++ b/IRaCIS.Core.API/Program.cs @@ -53,24 +53,12 @@ namespace IRaCIS.Core.API }) .Build(); - //// Serilog SerilogExtension.AddSerilogSetup(enviromentName, host.Services); Log.Logger.Warning($"ǰ{enviromentName}"); - - NewId.SetProcessIdProvider(new CurrentProcessIdProvider()); - - - //Ŀ״̬ - await InitCache(host); - - - - - - + host.Run(); @@ -100,28 +88,7 @@ namespace IRaCIS.Core.API webBuilder.UseStartup(); }).UseSerilog() .UseServiceProviderFactory(new AutofacServiceProviderFactory()); - - - private static async Task InitCache(IHost host) - { - var _repository = host.Services.GetService(typeof(IRepository)) as IRepository; - - //ʼ ʻݣҼʻļ - await InternationalizationHelper.InitInternationlizationDataAndWatchJsonFileAsync(_repository); - - var _mediator = host.Services.GetService(typeof(IMediator)) as IMediator; - - await _mediator.Send(new AnonymizeCacheRequest()); - - await _mediator.Send(new TrialStateCacheRequest()); - - - - } - - - - + } } diff --git a/IRaCIS.Core.API/Startup.cs b/IRaCIS.Core.API/Startup.cs index c157a1d94..06851f87d 100644 --- a/IRaCIS.Core.API/Startup.cs +++ b/IRaCIS.Core.API/Startup.cs @@ -25,6 +25,7 @@ using Microsoft.AspNetCore.SignalR; using IRaCIS.Core.Domain.Share; using Microsoft.AspNetCore.StaticFiles; using IRaCIS.Core.Application.Helper; +using IRaCIS.Application.Services.BackGroundJob; namespace IRaCIS.Core.API { @@ -162,7 +163,7 @@ namespace IRaCIS.Core.API } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + public async void Configure(IApplicationBuilder app, IWebHostEnvironment env) { //ػ app.UseLocalization(); @@ -236,6 +237,10 @@ namespace IRaCIS.Core.API endpoints.MapHub("/UploadHub"); }); + var hangfireJobService = app.ApplicationServices.GetRequiredService(); + + await hangfireJobService.InitHangfireJobTaskAsync(); + } } } diff --git a/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs b/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs new file mode 100644 index 000000000..f62423549 --- /dev/null +++ b/IRaCIS.Core.Application/BackGroundJob/IRaCISCHangfireJob.cs @@ -0,0 +1,213 @@ +using EasyCaching.Core; +using Hangfire; +using Hangfire.Storage; +using IRaCIS.Core.Application.Helper; +using IRaCIS.Core.Application.Service; +using IRaCIS.Core.Domain.Share; +using IRaCIS.Core.Infrastructure; +using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; + +namespace IRaCIS.Application.Services.BackGroundJob +{ + + public interface IIRaCISHangfireJob + { + + Task MemoryCacheTrialStatusAsync(); + + Task InitHangfireJobTaskAsync(); + + } + public class IRaCISCHangfireJob : IIRaCISHangfireJob + { + public static string JsonFileFolder = Path.Combine(AppContext.BaseDirectory, StaticData.Folder.Resources); + + private readonly IRepository _trialRepository; + private readonly IEasyCachingProvider _provider; + private readonly ILogger _logger; + private readonly IRepository _systemAnonymizationRepository; + private readonly IRepository _trialEmailNoticeConfigRepository; + private readonly IRepository _internationalizationRepository; + + + + public IRaCISCHangfireJob(IRepository trialRepository, ILogger logger, IEasyCachingProvider provider, IRepository trialEmailNoticeConfigRepository, IRepository internationalizationRepository, IRepository systemAnonymizationRepository) + { + _trialRepository = trialRepository; + _provider = provider; + _logger = logger; + _trialEmailNoticeConfigRepository = trialEmailNoticeConfigRepository; + _internationalizationRepository = internationalizationRepository; + _systemAnonymizationRepository = systemAnonymizationRepository; + } + + public async Task InitHangfireJobTaskAsync() + { + _logger.LogInformation("项目启动 hangfire 任务初始化 执行开始~"); + + + //项目状态 立即加载到缓存中 + await MemoryCacheTrialStatusAsync(); + + await MemoryCacheAnonymizeData(); + + + //创建项目缓存 定时任务 + HangfireJobHelper.AddOrUpdateInitCronJob("RecurringJob_Cache_TrialState", t => t.MemoryCacheTrialStatusAsync(), Cron.Daily()); + + //初始化 + + await InitInternationlizationDataAndWatchJsonFileAsync(); + + //创建邮件定时任务 + await InitSysAndTrialCronJobAsync(); + + + _logger.LogInformation("项目启动 hangfire 任务初始化 执行结束"); + } + + /// + /// 缓存项目状态 + /// + /// + public async Task MemoryCacheTrialStatusAsync() + { + + var list = await _trialRepository.Select(t => new { TrialId = t.Id, TrialStatusStr = t.TrialStatusStr }) + .ToListAsync(); + + list.ForEach(t => _provider.Set(t.TrialId.ToString(), t.TrialStatusStr, TimeSpan.FromDays(7))); + + } + + public async Task MemoryCacheAnonymizeData() + { + var systemAnonymizationList = await _systemAnonymizationRepository.Where(t => t.IsEnable).ToListAsync(); + + _provider.Set(StaticData.Anonymize.Anonymize_AddFixedFiled, systemAnonymizationList.Where(t => t.IsAdd && t.IsFixed).ToList(), TimeSpan.FromDays(7)); + _provider.Set(StaticData.Anonymize.Anonymize_AddIRCInfoFiled, systemAnonymizationList.Where(t => t.IsAdd && t.IsFixed == false).ToList(), TimeSpan.FromDays(7)); + _provider.Set(StaticData.Anonymize.Anonymize_FixedField, systemAnonymizationList.Where(t => t.IsAdd == false && t.IsFixed).ToList(), TimeSpan.FromDays(7)); + _provider.Set(StaticData.Anonymize.Anonymize_IRCInfoField, systemAnonymizationList.Where(t => t.IsAdd == false && t.IsFixed == false).ToList(), TimeSpan.FromDays(7)); + } + + + #region 国际化 初始化 + + + public async Task InitInternationlizationDataAndWatchJsonFileAsync() + { + //查询数据库的数据 + var toJsonList = await _internationalizationRepository.Where(t => t.InternationalizationType == 1).Select(t => new + { + t.Code, + t.Value, + t.ValueCN + }).ToListAsync(); + + //组织成json 文件 + + var usJsonPath = Path.Combine(JsonFileFolder, StaticData.En_US_Json); + var cnJsonPath = Path.Combine(JsonFileFolder, StaticData.Zh_CN_Json); + + + //本地静态文件国际化需要 + foreach (var tojsonItem in toJsonList) + { + StaticData.En_US_Dic[tojsonItem.Code] = tojsonItem.Value; + StaticData.Zh_CN_Dic[tojsonItem.Code] = tojsonItem.ValueCN; + } + + File.WriteAllText(usJsonPath, JsonConvert.SerializeObject(StaticData.En_US_Dic)); + File.WriteAllText(cnJsonPath, JsonConvert.SerializeObject(StaticData.Zh_CN_Dic)); + + + //监测Json文件变更 实时刷新数据 + WatchJsonFile(usJsonPath); + WatchJsonFile(cnJsonPath); + + } + + public void WatchJsonFile(string filePath) + { + if (!File.Exists(filePath)) + { + throw new BusinessValidationFailedException(StaticData.International("IRaCISCHangfireJob_FileNotFound")); + } + + FileSystemWatcher watcher = new FileSystemWatcher(Path.GetDirectoryName(filePath), Path.GetFileName(filePath)); + watcher.Changed += (sender, e) => LoadJsonFile(filePath); + watcher.EnableRaisingEvents = true; + + } + + + private void LoadJsonFile(string filePath) + { + + IConfigurationBuilder builder = new ConfigurationBuilder().AddJsonFile(filePath); + + IConfigurationRoot enConfiguration = builder.Build(); + + foreach (IConfigurationSection section in enConfiguration.GetChildren()) + { + if (filePath.Contains(StaticData.En_US_Json)) + { + StaticData.En_US_Dic[section.Key] = section.Value; + + } + else + { + StaticData.Zh_CN_Dic[section.Key] = section.Value; + } + } + } + + #endregion + + public async Task InitSysAndTrialCronJobAsync() + { + //var deleteJobIdList = await _trialEmailNoticeConfigRepository.Where(t => t.Trial.TrialStatusStr != StaticData.TrialState.TrialOngoing && t.EmailCron != string.Empty && t.IsAutoSend) + // .Select(t => t.TrialId + "_" + t.Id) + // .ToListAsync(); + + //foreach (var jobId in deleteJobIdList) + //{ + // HangfireJobHelper.RemoveCronJob(jobId); + //} + + + var taskInfoList = await _trialEmailNoticeConfigRepository.Where(t => t.Trial.TrialStatusStr == StaticData.TrialState.TrialOngoing && t.EmailCron != string.Empty && t.IsAutoSend) + .Select(t => new { t.Id, t.Code, t.EmailCron, t.BusinessScenarioEnum, t.TrialId }) + .ToListAsync(); + + foreach (var task in taskInfoList) + { + //利用主键作为任务Id + var jobId = $"{task.TrialId}_{task.Id}"; + + HangfireJobHelper.AddOrUpdateTrialCronJob(jobId, task.TrialId, task.BusinessScenarioEnum, task.EmailCron); + } + + var addOrUpdateJobIdList = taskInfoList.Select(t => $"{t.TrialId}_{t.Id}").ToList(); + + var list = JobStorage.Current.GetConnection().GetRecurringJobs().ToList(); + + //项目定时任务都在default 队列 + //var dbJobIdList = JobStorage.Current.GetConnection().GetRecurringJobs().Where(t => t.Queue == "default").Select(t => t.Id).ToList(); + + //var deleteList= dbJobIdList.Except(addOrUpdateJobIdList).ToList(); + + // foreach (var jobId in deleteList) + // { + // HangfireJobHelper.RemoveCronJob(jobId); + // } + + + } + } + + +} diff --git a/IRaCIS.Core.Application/BackGroundJob/IRaCISCacheHangfireJob.cs b/IRaCIS.Core.Application/BackGroundJob/IRaCISCacheHangfireJob.cs deleted file mode 100644 index 80a3d16ac..000000000 --- a/IRaCIS.Core.Application/BackGroundJob/IRaCISCacheHangfireJob.cs +++ /dev/null @@ -1,99 +0,0 @@ -using EasyCaching.Core; -using IRaCIS.Core.Domain.Share; -using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; -using Microsoft.Extensions.Logging; - -namespace IRaCIS.Application.Services.BackGroundJob -{ - - public interface IIRaCISCacheHangfireJob - { - - Task ProjectStartCache(); - Task MemoryCacheTrialStatus(); - - Task MemoryCacheAnonymizeData(); - - Task CacheUserTypePermission(Guid? cacheUserTypeId); - } - public class IRaCISCacheHangfireJob: IIRaCISCacheHangfireJob - { - private readonly IRepository _trialRepository; - private readonly IEasyCachingProvider _provider; - private readonly ILogger _logger; - private readonly IRepository _systemAnonymizationRepository; - - private readonly IRepository _userTypeMenuRepository; - - public IRaCISCacheHangfireJob(IRepository trialRepository, - IRepository systemAnonymizationRepository, IRepository userTypeMenuRepository, - IEasyCachingProvider provider,ILogger logger) - { - _trialRepository = trialRepository; - _provider = provider; - _logger = logger; - _systemAnonymizationRepository = systemAnonymizationRepository; - _userTypeMenuRepository = userTypeMenuRepository; - } - - - public async Task ProjectStartCache() - { - _logger.LogInformation("hangfire 定时缓存项目状态任务开始~"); - try - { - await MemoryCacheTrialStatus(); - - await MemoryCacheAnonymizeData(); - - await CacheUserTypePermission(); - } - catch (Exception e) - { - _logger.LogError("hangfire 定时任务执行失败" + e.Message); - - } - - _logger.LogInformation("hangfire 定时任务执行结束"); - } - - public async Task MemoryCacheTrialStatus() - { - var list = await _trialRepository.Select(t => new { TrialId = t.Id, TrialStatusStr = t.TrialStatusStr }) - .ToListAsync(); - - list.ForEach(t => _provider.Set(t.TrialId.ToString(), t.TrialStatusStr, TimeSpan.FromDays(7))); - - } - - - - - - - - public async Task MemoryCacheAnonymizeData() - { - var systemAnonymizationList = await _systemAnonymizationRepository.Where(t => t.IsEnable).ToListAsync(); - - _provider.Set(StaticData.Anonymize.Anonymize_AddFixedFiled, systemAnonymizationList.Where(t => t.IsAdd && t.IsFixed).ToList(), TimeSpan.FromDays(7)); - _provider.Set(StaticData.Anonymize.Anonymize_AddIRCInfoFiled, systemAnonymizationList.Where(t => t.IsAdd && t.IsFixed == false).ToList(), TimeSpan.FromDays(7)); - _provider.Set(StaticData.Anonymize.Anonymize_FixedField, systemAnonymizationList.Where(t => t.IsAdd == false && t.IsFixed).ToList(), TimeSpan.FromDays(7)); - _provider.Set(StaticData.Anonymize.Anonymize_IRCInfoField, systemAnonymizationList.Where(t => t.IsAdd == false && t.IsFixed == false).ToList(), TimeSpan.FromDays(7)); - } - - public async Task CacheUserTypePermission(Guid? cacheUserTypeId=null) - { - - var permissionList = await _userTypeMenuRepository.Where(t => t.Menu.MenuType == "F") - .WhereIf(cacheUserTypeId != null, t => t.UserTypeId == cacheUserTypeId.Value).Select(t => new { t.UserTypeId, t.Menu.PermissionStr }).ToListAsync(); - - foreach (var userTypeGroup in permissionList.GroupBy(t => t.UserTypeId)) - { - _provider.Set($"{StaticData.CacheKey.UserTypeId}_{userTypeGroup.Key}", userTypeGroup.Select(t => t.PermissionStr).ToList(), TimeSpan.FromDays(7)); - } - } - - - } -} \ No newline at end of file diff --git a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml index 01b3b1464..38559fb74 100644 --- a/IRaCIS.Core.Application/IRaCIS.Core.Application.xml +++ b/IRaCIS.Core.Application/IRaCIS.Core.Application.xml @@ -11927,6 +11927,12 @@ 构造函数注入 + + + 缓存项目状态 + + + 统一返回前端数据包装,之前在控制器包装,现在修改为动态Api 在ResultFilter这里包装,减少重复冗余代码 diff --git a/IRaCIS.Core.Domain/_Config/_StaticData.cs b/IRaCIS.Core.Domain/_Config/_StaticData.cs index d6ca83588..5bfb2f3e5 100644 --- a/IRaCIS.Core.Domain/_Config/_StaticData.cs +++ b/IRaCIS.Core.Domain/_Config/_StaticData.cs @@ -78,7 +78,7 @@ public static class StaticData public static readonly string Resources = "Resources"; public static readonly string IRaCISDataFolder = "IRaCISData"; - + public static readonly string OtherDataFolder = "OtherData"; public static readonly string TrialDataFolder = "TrialData"; public static readonly string SystemDataFolder = "SystemData";