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
/// </summary>
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<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();
return Task.Run(() =>
{
//_sendService = App.GetRequiredService<ISendMessageService>();
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);
});
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>();
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)
{
var token = cts.Token;

View File

@@ -64,7 +64,9 @@ public class Startup : AppStartup
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 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;
/// <summary>
/// 初始化一个<see cref="OAuthService"/>类型的新实例.
/// </summary>
@@ -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<ConditionalBackgroundService>();
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<TimedTaskBackgroundService>(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<ConditionalBackgroundService>();
await conditionalBackgroundService.StartAsync(CancellationToken.None);
//var conditionalBackgroundService = App.GetRequiredService<ConditionalBackgroundService>();
//await conditionalBackgroundService.StartAsync(CancellationToken.None);
var httpContext = _httpContextAccessor.HttpContext;
var timedTaskService = ActivatorUtilities.CreateInstance<TimedTaskBackgroundService>(httpContext.RequestServices);
await timedTaskService.StartAsync(default);
return new
{
theme = user.Theme == null ? "classic" : user.Theme,