定时任务代码调整

This commit is contained in:
alex
2023-08-15 16:54:40 +08:00
parent b7500efd7f
commit 9d780a5694

View File

@@ -57,44 +57,56 @@ namespace Tnb.WarehouseMgr
_serviceProvider = serviceProvider;
}
protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(async () =>
{
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
var queueTask = Task.Run(async () =>
{
var channelReader = _serviceProvider.GetRequiredService<ITaskMessageNotify>().Reader;
var channelReader = _serviceProvider.GetRequiredService<ITaskMessageNotify>().Reader;
CancellationTokenSource? cts = new();
CancellationTokenSource? cts = new();
while (channelReader != null && await channelReader.WaitToReadAsync())
{
while (channelReader.TryRead(out var message))
{
if (_timedFuncMap.ContainsKey(message.TaskName))
{
await _timedFuncMap[message.TaskName].Invoke(cts);
}
}
}
while (channelReader != null && await channelReader.WaitToReadAsync())
{
while (channelReader.TryRead(out var message))
{
if (_timedFuncMap.ContainsKey(message.TaskName))
{
await _timedFuncMap[message.TaskName].Invoke(cts);
}
}
}
}, stoppingToken);
var timedTask = Task.Run(() =>
{
_eventPublisher = App.GetRequiredService<IEventPublisher>();
////生成任务执行
//CancellationTokenSource genTaskCTS = new();
CancellationTokenSource kittingOutAddCts = new();
CancellationTokenSource kittingOutShippedCts = new();
CancellationTokenSource setSortingCts = new();
CancellationTokenSource isMinStorageCts = new();
//var wareHouseService = App.GetRequiredService<IWareHouseService>();
//TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS);
//齐套出库
var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, 1);
TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, 1);
//齐套分拣
var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, 1);
var transferSignService = App.GetRequiredService<IWmsPDATransferSignService>();
TimedTask(cts => transferSignService.IsMinStorage(cts), isMinStorageCts, 30, TimeSpanUnit.Minutes);
});
return Task.WhenAll(queueTask, timedTask);
}
#region
//_eventPublisher = App.GetRequiredService<IEventPublisher>();
//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);
//齐套出库
var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, 1);
TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, 1);
//齐套分拣
var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, 1);
#endregion
}, stoppingToken);
private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, int interval, TimeSpanUnit timeType = TimeSpanUnit.Seconds)
{
@@ -136,6 +148,7 @@ namespace Tnb.WarehouseMgr
TimeSpanUnit.Minutes => Task.Delay(TimeSpan.FromMinutes(interval)),
TimeSpanUnit.Hours => Task.Delay(TimeSpan.FromHours(interval)),
TimeSpanUnit.Days => Task.Delay(TimeSpan.FromDays(interval)),
_ => throw new NotImplementedException()
};
return delayTask;
}