From b3087b8f6401d329bd05cccc7ba2611c054f1a97 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 4 Aug 2023 09:04:11 +0800 Subject: [PATCH 1/4] 1 --- WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs index 3f3b38f1..67bf6a95 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs @@ -60,9 +60,7 @@ namespace Tnb.WarehouseMgr { await action(cts).Catch(ex => { - //MessageSendModel messageSendModel = new(); - //_sendService?.SendMessage(); - //messageService.SentMessage(toUserIds!, ex.Message, ex.ToString()); + //notify operator }); await Task.Delay(1000); } From 04fe68fd1cf88e403a851fca3787e0e598db3ed6 Mon Sep 17 00:00:00 2001 From: FanLian Date: Fri, 4 Aug 2023 10:08:24 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=94=9F=E6=88=90=E9=BD=90=E5=A5=97?= =?UTF-8?q?=E5=85=A5=E5=BA=93=E7=9A=84=E5=8F=82=E6=95=B0=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- WarehouseMgr/Tnb.WarehouseMgr/WmsRobotCallbackService.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WmsRobotCallbackService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WmsRobotCallbackService.cs index 53a612da..eb14d720 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WmsRobotCallbackService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WmsRobotCallbackService.cs @@ -99,8 +99,8 @@ namespace Tnb.WarehouseMgr visualDevModelCrInput.data[nameof(WmsKittingInstock.create_time)] = DateTime.Now; visualDevModelCrInput.data[nameof(WmsKittingInstock.location_id)] = carry!.location_id!; visualDevModelCrInput.data[nameof(WmsKittingInstock.bill_code)] = _billRullService.GetBillNumber("WmsKittingInStk").GetAwaiter().GetResult(); - visualDevModelCrInput.data[nameof(WmsKittingInstock.source_id)] = kittingout.source_id; - visualDevModelCrInput.data[nameof(WmsKittingInstock.source_code)] = kittingout.source_code; + visualDevModelCrInput.data[nameof(WmsKittingInstock.source_id)] = kittingout.id; + visualDevModelCrInput.data[nameof(WmsKittingInstock.source_code)] = kittingout.bill_code; var location = await _db.Queryable().SingleAsync(it => it.id == kittingout.location_id); From b5916b4c0e0be11b83a29c05400d77ba4142eb76 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 4 Aug 2023 10:10:23 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E3=80=81=E7=99=BB=E5=87=BA=E9=80=9A=E7=9F=A5Wms=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E6=9C=8D=E5=8A=A1=E7=9A=84=E5=90=AF=E5=8A=A8=E9=A2=84?= =?UTF-8?q?=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConditionalBackgroundService.cs | 30 +++++++++++++++++++ .../Tnb.WarehouseMgr/WareHouseService.cs | 1 - apihost/Tnb.API.Entry/Startup.cs | 2 +- system/Tnb.OAuth/OAuthService.cs | 9 ++++++ system/Tnb.OAuth/Tnb.OAuth.csproj | 1 + 5 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 WarehouseMgr/Tnb.WarehouseMgr/ConditionalBackgroundService.cs diff --git a/WarehouseMgr/Tnb.WarehouseMgr/ConditionalBackgroundService.cs b/WarehouseMgr/Tnb.WarehouseMgr/ConditionalBackgroundService.cs new file mode 100644 index 00000000..745a3cf2 --- /dev/null +++ b/WarehouseMgr/Tnb.WarehouseMgr/ConditionalBackgroundService.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; + +namespace Tnb.WarehouseMgr +{ + public class ConditionalBackgroundService : IHostedService + { + private readonly IHostedService _backgroundService; + //private readonly Func _condition; + + public ConditionalBackgroundService(IHostedService backgroundService) + { + _backgroundService = backgroundService; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + await _backgroundService.StartAsync(cancellationToken); + } + public async Task StopAsync(CancellationToken cancellationToken) + { + await _backgroundService.StopAsync(cancellationToken); + } + + } +} diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs index 78fabc26..5aacae45 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs @@ -283,7 +283,6 @@ namespace Tnb.WarehouseMgr start = end; end = Math.Min((end + moveNum), arrary.Length); } - foreach (var arr in arrList) { for (int j = 1, len = arr.Length; j <= len; j++) diff --git a/apihost/Tnb.API.Entry/Startup.cs b/apihost/Tnb.API.Entry/Startup.cs index dd5e6ed7..6b72c8c3 100644 --- a/apihost/Tnb.API.Entry/Startup.cs +++ b/apihost/Tnb.API.Entry/Startup.cs @@ -64,7 +64,7 @@ public class Startup : AppStartup services.AddOverideVisualDev(); //定时任务 - services.AddHostedService(); + services.AddHostedService(sp => new ConditionalBackgroundService(sp.GetRequiredService())); } diff --git a/system/Tnb.OAuth/OAuthService.cs b/system/Tnb.OAuth/OAuthService.cs index afebf9dd..ea5d3651 100644 --- a/system/Tnb.OAuth/OAuthService.cs +++ b/system/Tnb.OAuth/OAuthService.cs @@ -41,6 +41,7 @@ using JNPF.Common.Core.Handlers; using JNPF.Message.Interfaces.Message; using JNPF.Extras.DatabaseAccessor.SqlSugar.Models; using Aop.Api.Domain; +using Tnb.WarehouseMgr; namespace JNPF.OAuth; @@ -382,6 +383,10 @@ public class OAuthService : IDynamicApiController, ITransient [HttpGet("Logout")] public async Task Logout([FromQuery] string ticket) { + //用户退出时停止Wms定时任务 modify by ly on 20230804 + var conditionalBackgroundService = App.GetRequiredService(); + await conditionalBackgroundService.StopAsync(CancellationToken.None); + var tenantId = _userManager.TenantId ?? "default"; var userId = _userManager.UserId ?? "admim"; var httpContext = _httpContextAccessor.HttpContext; @@ -672,6 +677,10 @@ public class OAuthService : IDynamicApiController, ITransient } } + //启动Wms定时服务 modify by ly on 20230804 + var conditionalBackgroundService = App.GetRequiredService(); + await conditionalBackgroundService.StartAsync(CancellationToken.None); + return new { theme = user.Theme == null ? "classic" : user.Theme, diff --git a/system/Tnb.OAuth/Tnb.OAuth.csproj b/system/Tnb.OAuth/Tnb.OAuth.csproj index af2ae6c7..418136aa 100644 --- a/system/Tnb.OAuth/Tnb.OAuth.csproj +++ b/system/Tnb.OAuth/Tnb.OAuth.csproj @@ -12,6 +12,7 @@ + From f0e859a2037915fa2f0fa8b46ac96a358f55a088 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 4 Aug 2023 10:38:53 +0800 Subject: [PATCH 4/4] 1 --- .../TimedTaskBackgroundService.cs | 58 +++++++++++-------- apihost/Tnb.API.Entry/Startup.cs | 4 +- system/Tnb.OAuth/OAuthService.cs | 18 ++++-- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs index 67bf6a95..449f99a1 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs @@ -22,35 +22,47 @@ namespace Tnb.WarehouseMgr /// 定时任务 /// added by ly on 20230802 /// - public class TimedTaskBackgroundService : BackgroundService + public class TimedTaskBackgroundService : IHostedService { private ISendMessageService? _sendService; - protected override async Task ExecuteAsync(CancellationToken stoppingToken) + + public Task StartAsync(CancellationToken cancellationToken) { - - await Task.Run(() => - { - _sendService = App.GetRequiredService(); - var userManager = App.GetRequiredService(); - List toUserIds = new List() { "25398501929509" }; - //生成任务执行 - CancellationTokenSource genTaskCTS = new(); - CancellationTokenSource kittingOutAddCts = new(); - CancellationTokenSource kittingOutShippedCts = new(); - CancellationTokenSource setSortingCts = new(); + return Task.Run(() => + { + //_sendService = App.GetRequiredService(); - var wareHouseService = App.GetRequiredService(); - TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS, toUserIds); - //齐套出库 - var kittingOutService = App.GetRequiredService(); - TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds); - TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds); - //齐套分拣 - var setSortingService = App.GetRequiredService(); - TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, toUserIds); - }); + 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, toUserIds); + //齐套出库 + + var kittingOutService = App.GetRequiredService(); + TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds); + TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds); + //齐套分拣 + var setSortingService = App.GetRequiredService(); + TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, toUserIds); + }); } + + public Task StopAsync(CancellationToken cancellationToken) + { + return Task.CompletedTask; + } + + //protected override Task ExecuteAsync(CancellationToken stoppingToken) + //{ + // throw new NotImplementedException(); + //} + private Task TimedTask(Func action, CancellationTokenSource cts, List? toUserIds = default) { var token = cts.Token; diff --git a/apihost/Tnb.API.Entry/Startup.cs b/apihost/Tnb.API.Entry/Startup.cs index 6b72c8c3..148569d4 100644 --- a/apihost/Tnb.API.Entry/Startup.cs +++ b/apihost/Tnb.API.Entry/Startup.cs @@ -64,7 +64,9 @@ public class Startup : AppStartup services.AddOverideVisualDev(); //定时任务 - services.AddHostedService(sp => new ConditionalBackgroundService(sp.GetRequiredService())); + //services.AddHostedService(); + //services.AddSingleton(); + //services.AddHostedService(sp => new ConditionalBackgroundService(sp.GetRequiredService())); } diff --git a/system/Tnb.OAuth/OAuthService.cs b/system/Tnb.OAuth/OAuthService.cs index ea5d3651..37e1e397 100644 --- a/system/Tnb.OAuth/OAuthService.cs +++ b/system/Tnb.OAuth/OAuthService.cs @@ -42,6 +42,8 @@ using JNPF.Message.Interfaces.Message; using JNPF.Extras.DatabaseAccessor.SqlSugar.Models; using Aop.Api.Domain; using Tnb.WarehouseMgr; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; namespace JNPF.OAuth; @@ -141,6 +143,8 @@ public class OAuthService : IDynamicApiController, ITransient private readonly IMHandler _imHandler; + + /// /// 初始化一个类型的新实例. /// @@ -383,14 +387,14 @@ public class OAuthService : IDynamicApiController, ITransient [HttpGet("Logout")] public async Task Logout([FromQuery] string ticket) { - //用户退出时停止Wms定时任务 modify by ly on 20230804 - var conditionalBackgroundService = App.GetRequiredService(); - await conditionalBackgroundService.StopAsync(CancellationToken.None); var tenantId = _userManager.TenantId ?? "default"; var userId = _userManager.UserId ?? "admim"; var httpContext = _httpContextAccessor.HttpContext; httpContext.SignoutToSwagger(); + //用户退出时停止Wms定时任务 modify by ly on 20230804 + var timedTaskService = ActivatorUtilities.CreateInstance(httpContext.RequestServices); + await timedTaskService.StopAsync(CancellationToken.None); // 清除IM中的webSocket var list = await GetOnlineUserList(tenantId); @@ -678,9 +682,11 @@ public class OAuthService : IDynamicApiController, ITransient } //启动Wms定时服务 modify by ly on 20230804 - var conditionalBackgroundService = App.GetRequiredService(); - await conditionalBackgroundService.StartAsync(CancellationToken.None); - + //var conditionalBackgroundService = App.GetRequiredService(); + //await conditionalBackgroundService.StartAsync(CancellationToken.None); + var httpContext = _httpContextAccessor.HttpContext; + var timedTaskService = ActivatorUtilities.CreateInstance(httpContext.RequestServices); + await timedTaskService.StartAsync(default); return new { theme = user.Theme == null ? "classic" : user.Theme,