取消定时服务发布订阅模式改为存时间轮询方式

This commit is contained in:
alex
2023-10-10 13:23:38 +08:00
parent 1cfc9e25e5
commit ab9608aec2
6 changed files with 54 additions and 44 deletions

View File

@@ -46,13 +46,13 @@ namespace Tnb.WarehouseMgr
private static Dictionary<string, Func<CancellationToken?, Task>> _timedFuncMap = new(StringComparer.OrdinalIgnoreCase);
static TimedTaskBackgroundService()
{
Task.Run(() =>
{
_timedFuncMap = App.EffectiveTypes.AsParallel().Where(t => !t.Namespace.IsNullOrWhiteSpace() && t.Namespace.Equals("Tnb.WarehouseMgr", StringComparison.OrdinalIgnoreCase)).SelectMany(t => t.GetMethods())
.Where(m => m.GetCustomAttribute<TimedAttribute>() != null)
.ToDictionary(x => x.Name, x =>
(Func<CancellationToken?, Task>)Delegate.CreateDelegate(typeof(Func<CancellationToken?, Task>), App.GetService(x.DeclaringType), x));
});
//Task.Run(() =>
//{
// _timedFuncMap = App.EffectiveTypes.AsParallel().Where(t => !t.Namespace.IsNullOrWhiteSpace() && t.Namespace.Equals("Tnb.WarehouseMgr", StringComparison.OrdinalIgnoreCase)).SelectMany(t => t.GetMethods())
// .Where(m => m.GetCustomAttribute<TimedAttribute>() != null)
// .ToDictionary(x => x.Name, x =>
// (Func<CancellationToken?, Task>)Delegate.CreateDelegate(typeof(Func<CancellationToken?, Task>), App.GetService(x.DeclaringType), x));
//});
}
public TimedTaskBackgroundService(IServiceProvider serviceProvider)
{
@@ -62,42 +62,44 @@ namespace Tnb.WarehouseMgr
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
IsStarted = true;
var queueTask = Task.Run(async () =>
{
//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(stoppingToken);
}
}
}
}, stoppingToken);
// while (channelReader != null && await channelReader.WaitToReadAsync())
// {
// while (channelReader.TryRead(out var message))
// {
// if (_timedFuncMap.ContainsKey(message.TaskName))
// {
// await _timedFuncMap[message.TaskName].Invoke(stoppingToken);
// }
// }
// }
// }, stoppingToken);
var timedTask = Task.Run(() =>
{
_eventPublisher = App.GetRequiredService<IEventPublisher>();
var whSvc = App.GetRequiredService<IWareHouseService>();
TimedTask(token => whSvc.GenTaskExecute(token), stoppingToken, 3);
//齐套出库
var kittingOutService = App.GetRequiredService<IWmskittingOutService>();
TimedTask(token => kittingOutService.KittingOutByAdd(token), stoppingToken, 1);
TimedTask(token => kittingOutService.KittingOutByIsToBeShipped(token), stoppingToken, 1);
TimedTask(token => kittingOutService.KittingOutByAdd(token), stoppingToken, 3);
TimedTask(token => kittingOutService.KittingOutByIsToBeShipped(token), stoppingToken, 3);
//齐套分拣
var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
TimedTask(token => setSortingService.PackSortingByAdd(token), stoppingToken, 1);
TimedTask(token => setSortingService.PackSortingByAdd(token), stoppingToken, 3);
//最低库存检查
var transferSignService = App.GetRequiredService<IWmsPDATransferSignService>();
TimedTask(token => transferSignService.IsMinStorage(token), stoppingToken, 30, TimeSpanUnit.Minutes);
}, stoppingToken);
return Task.WhenAll(queueTask, timedTask);
return timedTask;
}