diff --git a/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs new file mode 100644 index 00000000..adb7c426 --- /dev/null +++ b/WarehouseMgr/Tnb.WarehouseMgr.Entities/Exceptions/TimedTaskException.cs @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SqlSugar; + +namespace Tnb.WarehouseMgr.Entities.Exceptions +{ + /// + /// 定时任务自定义异常 + /// + public class TimedTaskException : Exception + { + public string Method { get; set; } + public string UserId { get; set; } + public string UserName { get; set; } + public string RequestURL { get; set; } + public string RequestMethod { get; set; } + public ConnectionConfigOptions options { get; set; } + + public TimedTaskException(string message, + string method, + string userId, + string userName, + string requestUrl, + string requestMethod, + ConnectionConfigOptions options, + Exception innerException) : base(message, innerException) + { + this.Method = method; + this.UserId = userId; + this.UserName = userName; + this.RequestURL = requestUrl; + this.RequestMethod = requestMethod; + this.options = options; + } + } + + public static class TimedTaskExceptionExtensions + { + + } +} diff --git a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs index 0ffae671..a15bbabf 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/TimedTaskBackgroundService.cs @@ -3,17 +3,24 @@ 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 @@ -24,11 +31,11 @@ namespace Tnb.WarehouseMgr /// public class TimedTaskBackgroundService : BackgroundService { - private ISendMessageService? _sendService; + private IEventPublisher _eventPublisher; protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(() => { - //_sendService = App.GetRequiredService(); + //_eventPublisher = App.GetRequiredService(); List toUserIds = new List() { "25398501929509" }; //生成任务执行 CancellationTokenSource genTaskCTS = new(); @@ -55,9 +62,22 @@ namespace Tnb.WarehouseMgr { while (!token.IsCancellationRequested) { - await action(cts).Catch(ex => + await action(cts).Catch(async ex => { - //notify operator + 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); } diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs index 04c45c03..122217d6 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs @@ -45,6 +45,7 @@ using Tnb.WarehouseMgr.Entities.Dto; using Tnb.WarehouseMgr.Entities.Dto.Inputs; using Tnb.WarehouseMgr.Entities.Entity; using Tnb.WarehouseMgr.Entities.Enums; +using Tnb.WarehouseMgr.Entities.Exceptions; using Tnb.WarehouseMgr.Interfaces; namespace Tnb.WarehouseMgr @@ -219,6 +220,7 @@ namespace Tnb.WarehouseMgr public async Task GenTaskExecute(CancellationTokenSource? cts = default) { Stopwatch sw = Stopwatch.StartNew(); + CancellationTokenSource agvCts = new(); var db = _db.CopyNew(); try { @@ -352,11 +354,15 @@ namespace Tnb.WarehouseMgr reqBody.taskChainPriority = 0; reqBody.taskList = v; Log.Information($"请求参数:{JsonConvert.SerializeObject(reqBody)}"); - var respBody = await HttpClientHelper.PostStreamAsync(url, reqBody, CancellationToken.None); + var respBody = await HttpClientHelper.PostStreamAsync(url, reqBody, agvCts.Token); Log.Information($"调用Agv接口响应结果:{respBody}"); } } } + catch (Exception ex) when (ex is HttpRequestException hReqEx) + { + agvCts.Cancel(); + } catch (Exception ex) { Log.Error("生成预任务执行时出现错误", ex); @@ -366,6 +372,7 @@ namespace Tnb.WarehouseMgr } finally { + agvCts.Dispose(); cts?.Dispose(); } } diff --git a/common/Tnb.Common/Extension/TaskExtensions.cs b/common/Tnb.Common/Extension/TaskExtensions.cs index e31f008f..910ad30e 100644 --- a/common/Tnb.Common/Extension/TaskExtensions.cs +++ b/common/Tnb.Common/Extension/TaskExtensions.cs @@ -25,7 +25,7 @@ namespace Tnb.Common.Extension return tcs.Task; } - public static async Task Catch(this Task task, Action exceptionHandler) + public static async Task Catch(this Task task, Func exceptionHandler) { try { @@ -33,7 +33,7 @@ namespace Tnb.Common.Extension } catch (Exception ex) { - exceptionHandler(ex); + await exceptionHandler(ex); } }