using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aop.Api.Domain; using JNPF; using JNPF.Common.Core.Manager; using JNPF.Common.Dtos.Message; using JNPF.Common.Extension; using JNPF.Common.Security; using JNPF.EventBus; using JNPF.EventHandler; using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Message.Interfaces.Message; using JNPF.Systems.Entitys.System; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; using Tnb.Common.Extension; using Tnb.WarehouseMgr.Entities.Dto.Inputs; using Tnb.WarehouseMgr.Entities.Exceptions; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr { /// /// 定时任务 /// added by ly on 20230802 /// public class TimedTaskBackgroundService : BackgroundService { private IEventPublisher _eventPublisher; protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(() => { //_eventPublisher = App.GetRequiredService(); List toUserIds = new List() { "25398501929509" }; //生成任务执行 CancellationTokenSource genTaskCTS = new(); CancellationTokenSource kittingOutAddCts = new(); CancellationTokenSource kittingOutShippedCts = new(); CancellationTokenSource setSortingCts = new(); var wareHouseService = App.GetRequiredService(); TimedTask(cts => wareHouseService.GenTaskExecute(cts), genTaskCTS, toUserIds); //齐套出库 var kittingOutService = App.GetRequiredService(); TimedTask(cts => kittingOutService.KittingOutByAdd(cts), kittingOutAddCts, toUserIds); TimedTask(cts => kittingOutService.KittingOutByIsToBeShipped(cts), kittingOutShippedCts, toUserIds); //齐套分拣 var setSortingService = App.GetRequiredService(); TimedTask(cts => setSortingService.PackSortingByAdd(cts), setSortingCts, toUserIds); }, stoppingToken); private Task TimedTask(Func action, CancellationTokenSource cts, List? toUserIds = default) { var token = cts.Token; return Task.Run(async () => { while (!token.IsCancellationRequested) { await action(cts).Catch(async ex => { if (ex is TimedTaskException timedTaskEx) { //await _eventPublisher.PublishAsync(new LogEventSource("Log:CreateExLog", options, new SysLogEntity //{ // Id = SnowflakeIdHelper.NextId(), // Category = 4, // IPAddress = NetHelper.Ip, // RequestURL = httpRequest.Path, // RequestMethod = httpRequest.Method, // Json = timedTaskEx + "\n" + context.Exception.StackTrace + "\n" + context.Exception.TargetSite.GetParameters().ToString(), // PlatForm = string.Format("{0}-{1}", userAgent.OS.ToString(), userAgent.RawValue), // CreatorTime = DateTime.Now //})); } }); await Task.Delay(1000); } }, token); } } }