Wms自定义定时服务代码完善
This commit is contained in:
@@ -4,10 +4,15 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using JNPF;
|
||||
using JNPF.Common.Core.Manager;
|
||||
using JNPF.Common.Dtos.Message;
|
||||
using JNPF.Common.Extension;
|
||||
using JNPF.FriendlyException;
|
||||
using JNPF.Logging;
|
||||
using JNPF.Message.Interfaces.Message;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Tnb.Common.Extension;
|
||||
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
|
||||
using Tnb.WarehouseMgr.Interfaces;
|
||||
|
||||
@@ -19,28 +24,97 @@ namespace Tnb.WarehouseMgr
|
||||
/// </summary>
|
||||
public class TimedTaskBackgroundService : BackgroundService
|
||||
{
|
||||
private ISendMessageService? _sendService;
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
|
||||
await Task.Run(() =>
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
CancellationTokenSource tokenSource = new();
|
||||
CancellationToken token = tokenSource.Token;
|
||||
_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(withOutParamAction: (cts) => Task.Run(async () =>
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
await wareHouseService.GenTaskExecute(tokenSource);
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}, token));
|
||||
await TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS, toUserIds);
|
||||
//齐套出库
|
||||
|
||||
var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
|
||||
await TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds);
|
||||
await TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds);
|
||||
//齐套分拣
|
||||
var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
|
||||
await TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, toUserIds);
|
||||
|
||||
#region 老的方式
|
||||
//TimedTask(withOutParamAction: () => Task.Run(async () =>
|
||||
//{
|
||||
// while (!genTaskToken.IsCancellationRequested)
|
||||
// {
|
||||
// await wareHouseService.GenTaskExecute(genTaskCTS).Catch(ex =>
|
||||
// {
|
||||
|
||||
// });
|
||||
// await Task.Delay(1000);
|
||||
// }
|
||||
//}, genTaskToken));
|
||||
|
||||
|
||||
//CancellationTokenSource kittingOutAddCts = new();
|
||||
//CancellationToken kittingOutAddToken = kittingOutAddCts.Token;
|
||||
|
||||
|
||||
//var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
|
||||
//TimedTask(withOutParamAction: () => Task.Run(async () =>
|
||||
//{
|
||||
// while (!kittingOutAddToken.IsCancellationRequested)
|
||||
// {
|
||||
// await kittingOutService.KittingOutByAdd(kittingOutAddCts);
|
||||
// await Task.Delay(1000);
|
||||
// }
|
||||
//}, kittingOutAddToken));
|
||||
|
||||
|
||||
//CancellationTokenSource kittingOutShippedCts = new();
|
||||
//CancellationToken kittingOutShippedToken = kittingOutShippedCts.Token;
|
||||
|
||||
//TimedTask(withOutParamAction: () => Task.Run(async () =>
|
||||
//{
|
||||
// while (!kittingOutShippedToken.IsCancellationRequested)
|
||||
// {
|
||||
// await kittingOutService.KittingOutByIsToBeShipped(kittingOutShippedCts);
|
||||
// await Task.Delay(1000);
|
||||
// }
|
||||
//}, kittingOutShippedToken));
|
||||
#endregion
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private Task? TimedTask(Func<CancellationTokenSource?, Task>? withOutParamAction = null, Func<TimedtaskInput, Task>? withParemAction = null, CancellationTokenSource? cts = default, TimedtaskInput? parameter = null)
|
||||
private Task? TimedTask(Func<Task>? withOutParamAction = null, Func<TimedtaskInput, Task>? withParemAction = null, TimedtaskInput? parameter = null)
|
||||
{
|
||||
return parameter != null ? withParemAction?.Invoke(parameter) : withOutParamAction?.Invoke(cts);
|
||||
return parameter != null ? withParemAction?.Invoke(parameter) : withOutParamAction?.Invoke();
|
||||
}
|
||||
|
||||
private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, List<string>? toUserIds = default)
|
||||
{
|
||||
var token = cts.Token;
|
||||
return Task.Run(async () =>
|
||||
{
|
||||
while (!token.IsCancellationRequested)
|
||||
{
|
||||
await action(cts).Catch(ex =>
|
||||
{
|
||||
//MessageSendModel messageSendModel = new();
|
||||
//_sendService?.SendMessage();
|
||||
//messageService.SentMessage(toUserIds!, ex.Message, ex.ToString());
|
||||
});
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
}, token);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user