继续调整,手动开启定时任务相关逻辑代码,并处理相关bug

This commit is contained in:
alex
2023-09-20 18:20:15 +08:00
parent 4220ca4b8b
commit 8ce7bb540e
12 changed files with 53 additions and 43 deletions

View File

@@ -42,7 +42,7 @@ namespace Tnb.WarehouseMgr
public bool IsStarted { get; set; }
private IEventPublisher _eventPublisher = default!;
private readonly IServiceProvider _serviceProvider;
private static Dictionary<string, Func<CancellationTokenSource?, Task>> _timedFuncMap = new(StringComparer.OrdinalIgnoreCase);
private static Dictionary<string, Func<CancellationToken?, Task>> _timedFuncMap = new(StringComparer.OrdinalIgnoreCase);
static TimedTaskBackgroundService()
{
Task.Run(() =>
@@ -50,7 +50,7 @@ namespace Tnb.WarehouseMgr
_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<CancellationTokenSource?, Task>)Delegate.CreateDelegate(typeof(Func<CancellationTokenSource?, Task>), App.GetService(x.DeclaringType), x));
(Func<CancellationToken?, Task>)Delegate.CreateDelegate(typeof(Func<CancellationToken?, Task>), App.GetService(x.DeclaringType), x));
});
}
public TimedTaskBackgroundService(IServiceProvider serviceProvider)
@@ -74,7 +74,7 @@ namespace Tnb.WarehouseMgr
{
if (_timedFuncMap.ContainsKey(message.TaskName))
{
await _timedFuncMap[message.TaskName].Invoke(cts);
await _timedFuncMap[message.TaskName].Invoke(stoppingToken);
}
}
}
@@ -83,24 +83,16 @@ namespace Tnb.WarehouseMgr
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);
TimedTask(cts => kittingOutService.KittingOutByAdd(stoppingToken), stoppingToken, 1);
TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(stoppingToken), stoppingToken, 1);
//齐套分拣
var setSortingService = App.GetRequiredService<IWmsSetSortingService>();
TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, 1);
TimedTask(cts => setSortingService.PackSortingByAdd(cts), stoppingToken, 1);
//最低库存检查
var transferSignService = App.GetRequiredService<IWmsPDATransferSignService>();
TimedTask(cts => transferSignService.IsMinStorage(cts), isMinStorageCts, 30, TimeSpanUnit.Minutes);
TimedTask(cts => transferSignService.IsMinStorage(cts), stoppingToken, 30, TimeSpanUnit.Minutes);
}, stoppingToken);
@@ -109,14 +101,14 @@ namespace Tnb.WarehouseMgr
private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, int interval, TimeSpanUnit timeType = TimeSpanUnit.Seconds)
private Task TimedTask(Func<CancellationToken, Task> action, CancellationToken ct, int interval, TimeSpanUnit timeType = TimeSpanUnit.Seconds)
{
var token = cts.Token;
var token = ct;
return Task.Factory.StartNew(async () =>
{
while (!token.IsCancellationRequested)
{
await action(cts).Catch(async ex =>
await action(ct).Catch(async ex =>
{
if (ex is TimedTaskException timedTaskEx and not null)
{
@@ -137,7 +129,7 @@ namespace Tnb.WarehouseMgr
});
await TaskDelay(timeType, interval);
}
}, cts.Token, TaskCreationOptions.None, new CustomerTaskScheduler());
}, ct, TaskCreationOptions.None, new CustomerTaskScheduler());
#region ThreadPool 线线饿
//return Task.Run(async () =>
@@ -172,6 +164,7 @@ namespace Tnb.WarehouseMgr
public override Task StopAsync(CancellationToken cancellationToken)
{
IsStarted = false;
return Task.CompletedTask;
//return base.StopAsync(cancellationToken);