From 9d780a5694be594db5ff79f1b38e3b1eb023b56d Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 15 Aug 2023 16:54:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E4=BB=BB=E5=8A=A1=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TimedTaskBackgroundService.cs | 79 +++++++++++-------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs index 9ed682ae..d5b06ffd 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs @@ -57,44 +57,56 @@ namespace Tnb.WarehouseMgr _serviceProvider = serviceProvider; } - protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(async () => - { + protected override Task ExecuteAsync(CancellationToken stoppingToken) + { + var queueTask = Task.Run(async () => + { - var channelReader = _serviceProvider.GetRequiredService().Reader; + var channelReader = _serviceProvider.GetRequiredService().Reader; - CancellationTokenSource? cts = new(); + CancellationTokenSource? cts = new(); - while (channelReader != null && await channelReader.WaitToReadAsync()) - { - while (channelReader.TryRead(out var message)) - { - if (_timedFuncMap.ContainsKey(message.TaskName)) - { - await _timedFuncMap[message.TaskName].Invoke(cts); - } - } - } + while (channelReader != null && await channelReader.WaitToReadAsync()) + { + while (channelReader.TryRead(out var message)) + { + if (_timedFuncMap.ContainsKey(message.TaskName)) + { + await _timedFuncMap[message.TaskName].Invoke(cts); + } + } + } + + }, stoppingToken); + + var timedTask = Task.Run(() => + { + _eventPublisher = App.GetRequiredService(); + ////生成任务执行 + //CancellationTokenSource genTaskCTS = new(); + CancellationTokenSource kittingOutAddCts = new(); + CancellationTokenSource kittingOutShippedCts = new(); + CancellationTokenSource setSortingCts = new(); + CancellationTokenSource isMinStorageCts = new(); + //var wareHouseService = App.GetRequiredService(); + //TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS); + //齐套出库 + var kittingOutService = App.GetRequiredService(); + TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, 1); + TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, 1); + //齐套分拣 + var setSortingService = App.GetRequiredService(); + TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, 1); + + var transferSignService = App.GetRequiredService(); + TimedTask(cts => transferSignService.IsMinStorage(cts), isMinStorageCts, 30, TimeSpanUnit.Minutes); + + }); + + return Task.WhenAll(queueTask, timedTask); + } - #region 定时 - //_eventPublisher = App.GetRequiredService(); - //List toUserIds = new List() { "25398501929509" }; - ////生成任务执行 - //CancellationTokenSource genTaskCTS = new(); - //CancellationTokenSource kittingOutAddCts = new(); - //CancellationTokenSource kittingOutShippedCts = new(); - //CancellationTokenSource setSortingCts = new(); - //var wareHouseService = App.GetRequiredService(); - //TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS); - //齐套出库 - var kittingOutService = App.GetRequiredService(); - TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, 1); - TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, 1); - //齐套分拣 - var setSortingService = App.GetRequiredService(); - TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, 1); - #endregion - }, stoppingToken); private Task TimedTask(Func action, CancellationTokenSource cts, int interval, TimeSpanUnit timeType = TimeSpanUnit.Seconds) { @@ -136,6 +148,7 @@ namespace Tnb.WarehouseMgr TimeSpanUnit.Minutes => Task.Delay(TimeSpan.FromMinutes(interval)), TimeSpanUnit.Hours => Task.Delay(TimeSpan.FromHours(interval)), TimeSpanUnit.Days => Task.Delay(TimeSpan.FromDays(interval)), + _ => throw new NotImplementedException() }; return delayTask; }