From d02f2aa8dd9f172d2575b701efbe952f86f695b6 Mon Sep 17 00:00:00 2001 From: zhaoxinyu <89237069@qq.com> Date: Thu, 18 Apr 2024 17:17:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BasedataHttpApiHostModule.cs | 2 +- .../Boms/BomAppService.cs | 18 +---- .../Caches/CacheAppService.cs | 71 ++++++++++++++----- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/be/Hosts/Basedata.Host/Win_in.Sfs.Basedata.HttpApi.Host/BasedataHttpApiHostModule.cs b/be/Hosts/Basedata.Host/Win_in.Sfs.Basedata.HttpApi.Host/BasedataHttpApiHostModule.cs index 95407fc63..adb390a0c 100644 --- a/be/Hosts/Basedata.Host/Win_in.Sfs.Basedata.HttpApi.Host/BasedataHttpApiHostModule.cs +++ b/be/Hosts/Basedata.Host/Win_in.Sfs.Basedata.HttpApi.Host/BasedataHttpApiHostModule.cs @@ -127,7 +127,7 @@ public class BasedataHttpApiHostModule : ModuleBase // { // await scope.ServiceProvider // .GetRequiredService().StartAsync().ConfigureAwait(false); - + // } //}); diff --git a/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Boms/BomAppService.cs b/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Boms/BomAppService.cs index cf42f2408..175c29344 100644 --- a/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Boms/BomAppService.cs +++ b/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Boms/BomAppService.cs @@ -22,7 +22,7 @@ public class BomAppService : SfsBaseDataAppServiceBase, IBomAppService { - //private readonly CacheService _cacheService; + private new readonly IBomRepository _repository; private readonly IBomManager _bomManager; @@ -32,29 +32,17 @@ public class BomAppService : public BomAppService(IBomRepository repository , IBomManager bomManager , IDistributedCache cache - //, CacheService cacheService + ) : base(repository, cache) { _repository = repository; _bomManager = bomManager; - //_cacheService = cacheService; base.CreatePolicyName = BomPermissions.Create; base.UpdatePolicyName = BomPermissions.Update; - base.DeletePolicyName = BomPermissions.Delete; - //_cacheService.StartAsync().ConfigureAwait(false); - + base.DeletePolicyName = BomPermissions.Delete; } - - - - - - - - - #region Get [HttpGet("get-by-productitemcode")] diff --git a/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Caches/CacheAppService.cs b/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Caches/CacheAppService.cs index c93b41744..0ea409cf5 100644 --- a/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Caches/CacheAppService.cs +++ b/be/Modules/BaseData/src/Win_in.Sfs.Basedata.Application/Caches/CacheAppService.cs @@ -1,5 +1,7 @@ using System; +using System.Collections.Generic; using System.Threading.Tasks; +using DocumentFormat.OpenXml.Office2010.Drawing; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; @@ -15,6 +17,39 @@ using Win_in.Sfs.Basedata.Domain.Shared; namespace Win_in.Sfs.Basedata.Application; + +public class TaskQueue +{ + private readonly List _tasks = new List(); + + public void Enqueue(string name, Action action) + { + _tasks.Add(new TaskItem { Name = name, Action = action }); + } + + public async Task StartAsync() + { + foreach (var task in _tasks) + { + await Task.Run(task.Action).ConfigureAwait(false); + } + } +} +public class TaskItem +{ + public string Name { get; set; } + public Action Action { get; set; } +} + + + + + + + + + + public class CycleOptions { /// @@ -24,7 +59,7 @@ public class CycleOptions } /// -/// 提供缓存服务的类 +/// 提供缓存服务的类(堵塞以后修改再用) /// public class CacheService:ISingletonDependency { @@ -41,32 +76,32 @@ public class CacheService:ISingletonDependency /// private async Task BomsCycle() { - using var serviceScope = _serviceProvider.CreateScope(); - var repository = serviceScope.ServiceProvider.GetRequiredService(); - var reassigner = new Reassigner(DateTime.Now, new TimeSpan(0, 0, _options.Value.BomsCycle==0?600: _options.Value.BomsCycle)); - await reassigner.RunAsync(async () => - { - Cache.Boms.Clear(); - Cache.Boms = await repository.GetListAsync().ConfigureAwait(false); + + //Task.Run(async () => + //{ + var reassigner = new Reassigner(DateTime.Now, new TimeSpan(0, 0, _options.Value.BomsCycle == 0 ? 60 : _options.Value.BomsCycle)); + await reassigner.RunAsync(async () => + { + using var serviceScope = _serviceProvider.CreateScope(); + var repository = serviceScope.ServiceProvider.GetRequiredService(); + Cache.Boms.Clear(); + Cache.Boms = await repository.GetListAsync().ConfigureAwait(false); - }).ConfigureAwait(false); + }).ConfigureAwait(false); + //}); + ; } /// - /// 异步开始生命周期操作 + /// 异步开始生命周期操作不能堵塞 /// public async Task StartAsync() { - await BomsCycle().ConfigureAwait(false); - } + BomsCycle();//异步处理不能堵塞主任务 - /// - /// 异步重置BOM(Bill of Materials)生命周期操作 - /// - public async Task ResetAsync() - { - await BomsCycle().ConfigureAwait(false); } + + }