This commit is contained in:
alex
2023-08-04 10:38:53 +08:00
parent 914529b8a5
commit f0e859a203
3 changed files with 50 additions and 30 deletions

View File

@@ -22,35 +22,47 @@ namespace Tnb.WarehouseMgr
/// 定时任务 /// 定时任务
/// added by ly on 20230802 /// added by ly on 20230802
/// </summary> /// </summary>
public class TimedTaskBackgroundService : BackgroundService public class TimedTaskBackgroundService : IHostedService
{ {
private ISendMessageService? _sendService; private ISendMessageService? _sendService;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
public Task StartAsync(CancellationToken cancellationToken)
{ {
return Task.Run(() =>
await Task.Run(() => {
{ //_sendService = App.GetRequiredService<ISendMessageService>();
_sendService = App.GetRequiredService<ISendMessageService>();
var userManager = App.GetRequiredService<IUserManager>();
List<string> toUserIds = new List<string>() { "25398501929509" };
//生成任务执行
CancellationTokenSource genTaskCTS = new();
CancellationTokenSource kittingOutAddCts = new();
CancellationTokenSource kittingOutShippedCts = new();
CancellationTokenSource setSortingCts = new();
var wareHouseService = App.GetRequiredService<IWareHouseService>();
TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS, toUserIds);
//齐套出库
var kittingOutService = App.GetRequiredService<IWmskittingOutService>(); List<string> toUserIds = new List<string>() { "25398501929509" };
TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds); //生成任务执行
TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds); CancellationTokenSource genTaskCTS = new();
//齐套分拣 CancellationTokenSource kittingOutAddCts = new();
var setSortingService = App.GetRequiredService<IWmsSetSortingService>(); CancellationTokenSource kittingOutShippedCts = new();
TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, toUserIds); CancellationTokenSource setSortingCts = new();
});
var wareHouseService = App.GetRequiredService<IWareHouseService>();
TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS, toUserIds);
//齐套出库
var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds);
TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds);
//齐套分拣
var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
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<CancellationTokenSource, Task> action, CancellationTokenSource cts, List<string>? toUserIds = default) private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, List<string>? toUserIds = default)
{ {
var token = cts.Token; var token = cts.Token;

View File

@@ -64,7 +64,9 @@ public class Startup : AppStartup
services.AddOverideVisualDev(); services.AddOverideVisualDev();
//定时任务 //定时任务
services.AddHostedService(sp => new ConditionalBackgroundService(sp.GetRequiredService<TimedTaskBackgroundService>())); //services.AddHostedService<TimedTaskBackgroundService>();
//services.AddSingleton<TimedTaskBackgroundService>();
//services.AddHostedService(sp => new ConditionalBackgroundService(sp.GetRequiredService<TimedTaskBackgroundService>()));
} }

View File

@@ -42,6 +42,8 @@ using JNPF.Message.Interfaces.Message;
using JNPF.Extras.DatabaseAccessor.SqlSugar.Models; using JNPF.Extras.DatabaseAccessor.SqlSugar.Models;
using Aop.Api.Domain; using Aop.Api.Domain;
using Tnb.WarehouseMgr; using Tnb.WarehouseMgr;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace JNPF.OAuth; namespace JNPF.OAuth;
@@ -141,6 +143,8 @@ public class OAuthService : IDynamicApiController, ITransient
private readonly IMHandler _imHandler; private readonly IMHandler _imHandler;
/// <summary> /// <summary>
/// 初始化一个<see cref="OAuthService"/>类型的新实例. /// 初始化一个<see cref="OAuthService"/>类型的新实例.
/// </summary> /// </summary>
@@ -383,14 +387,14 @@ public class OAuthService : IDynamicApiController, ITransient
[HttpGet("Logout")] [HttpGet("Logout")]
public async Task Logout([FromQuery] string ticket) public async Task Logout([FromQuery] string ticket)
{ {
//用户退出时停止Wms定时任务 modify by ly on 20230804
var conditionalBackgroundService = App.GetRequiredService<ConditionalBackgroundService>();
await conditionalBackgroundService.StopAsync(CancellationToken.None);
var tenantId = _userManager.TenantId ?? "default"; var tenantId = _userManager.TenantId ?? "default";
var userId = _userManager.UserId ?? "admim"; var userId = _userManager.UserId ?? "admim";
var httpContext = _httpContextAccessor.HttpContext; var httpContext = _httpContextAccessor.HttpContext;
httpContext.SignoutToSwagger(); httpContext.SignoutToSwagger();
//用户退出时停止Wms定时任务 modify by ly on 20230804
var timedTaskService = ActivatorUtilities.CreateInstance<TimedTaskBackgroundService>(httpContext.RequestServices);
await timedTaskService.StopAsync(CancellationToken.None);
// 清除IM中的webSocket // 清除IM中的webSocket
var list = await GetOnlineUserList(tenantId); var list = await GetOnlineUserList(tenantId);
@@ -678,9 +682,11 @@ public class OAuthService : IDynamicApiController, ITransient
} }
//启动Wms定时服务 modify by ly on 20230804 //启动Wms定时服务 modify by ly on 20230804
var conditionalBackgroundService = App.GetRequiredService<ConditionalBackgroundService>(); //var conditionalBackgroundService = App.GetRequiredService<ConditionalBackgroundService>();
await conditionalBackgroundService.StartAsync(CancellationToken.None); //await conditionalBackgroundService.StartAsync(CancellationToken.None);
var httpContext = _httpContextAccessor.HttpContext;
var timedTaskService = ActivatorUtilities.CreateInstance<TimedTaskBackgroundService>(httpContext.RequestServices);
await timedTaskService.StartAsync(default);
return new return new
{ {
theme = user.Theme == null ? "classic" : user.Theme, theme = user.Theme == null ? "classic" : user.Theme,