将定时任务改为,发布、订阅模式的消息队列执行任务
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Aop.Api.Domain;
|
||||
@@ -17,9 +20,12 @@ using JNPF.Message.Interfaces.Message;
|
||||
using JNPF.Systems.Entitys.System;
|
||||
using Mapster;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Natasha.CSharp;
|
||||
using Tnb.Common.Extension;
|
||||
using Tnb.WarehouseMgr.Entities.Attributes;
|
||||
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
|
||||
using Tnb.WarehouseMgr.Entities.Exceptions;
|
||||
using Tnb.WarehouseMgr.Interfaces;
|
||||
@@ -33,27 +39,58 @@ namespace Tnb.WarehouseMgr
|
||||
public class TimedTaskBackgroundService : BackgroundService
|
||||
{
|
||||
private IEventPublisher _eventPublisher = default!;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private static Dictionary<string, Func<CancellationTokenSource?, Task>> _timedFuncMap = new(StringComparer.OrdinalIgnoreCase);
|
||||
static TimedTaskBackgroundService()
|
||||
{
|
||||
_timedFuncMap = App.EffectiveTypes.AsParallel().Where(t => !t.Namespace.IsNullOrWhiteSpace() && t.Namespace.Contains("Tnb.WarehouseMgr")).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));
|
||||
}
|
||||
public TimedTaskBackgroundService(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(() =>
|
||||
protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(async () =>
|
||||
{
|
||||
_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, toUserIds);
|
||||
//齐套出库
|
||||
var channelReader = _serviceProvider.GetRequiredService<ITaskMessageNotify>().Reader;
|
||||
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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, 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);
|
||||
#endregion
|
||||
}, stoppingToken);
|
||||
|
||||
private Task TimedTask(Func<CancellationTokenSource, Task> action, CancellationTokenSource cts, List<string>? toUserIds = default)
|
||||
|
||||
Reference in New Issue
Block a user