From f0e859a2037915fa2f0fa8b46ac96a358f55a088 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 4 Aug 2023 10:38:53 +0800 Subject: [PATCH] 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,