This commit is contained in:
FanLian
2023-08-09 14:28:53 +08:00
4 changed files with 78 additions and 7 deletions

View File

@@ -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
{
/// <summary>
/// 定时任务自定义异常
/// </summary>
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
{
}
}

View File

@@ -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
/// </summary>
public class TimedTaskBackgroundService : BackgroundService
{
private ISendMessageService? _sendService;
private IEventPublisher _eventPublisher;
protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.Run(() =>
{
//_sendService = App.GetRequiredService<ISendMessageService>();
//_eventPublisher = App.GetRequiredService<IEventPublisher>();
List<string> toUserIds = new List<string>() { "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);
}

View File

@@ -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();
}
}

View File

@@ -25,7 +25,7 @@ namespace Tnb.Common.Extension
return tcs.Task;
}
public static async Task Catch(this Task task, Action<Exception> exceptionHandler)
public static async Task Catch(this Task task, Func<Exception,Task> exceptionHandler)
{
try
{
@@ -33,7 +33,7 @@ namespace Tnb.Common.Extension
}
catch (Exception ex)
{
exceptionHandler(ex);
await exceptionHandler(ex);
}
}