diff --git a/src/SJZY.Track.Api/Properties/launchSettings.json b/src/SJZY.Track.Api/Properties/launchSettings.json index cba7d693..58c7579f 100644 --- a/src/SJZY.Track.Api/Properties/launchSettings.json +++ b/src/SJZY.Track.Api/Properties/launchSettings.json @@ -4,12 +4,12 @@ "commandName": "Project", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", - "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SJZY.Expand.ABP.Core;SkyAPM.Agent.AspNetCore", + "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "SJZY.Expand.ABP.Core", "SKYWALKING__SERVICENAME": "Track" }, "dotnetRunMessages": true, - "applicationUrl": "http://*:5054" + "applicationUrl": "http://*:5056" } }, "$schema": "https://json.schemastore.org/launchsettings.json" -} \ No newline at end of file +} diff --git a/src/SJZY.Track.Application/SJZY.Track.Application.csproj b/src/SJZY.Track.Application/SJZY.Track.Application.csproj index 7e97d032..d64b7d17 100644 --- a/src/SJZY.Track.Application/SJZY.Track.Application.csproj +++ b/src/SJZY.Track.Application/SJZY.Track.Application.csproj @@ -20,7 +20,7 @@ - + diff --git a/src/SJZY.Track.Application/Services/DebugApis/NormalService.cs b/src/SJZY.Track.Application/Services/DebugApis/NormalService.cs new file mode 100644 index 00000000..96eaee0f --- /dev/null +++ b/src/SJZY.Track.Application/Services/DebugApis/NormalService.cs @@ -0,0 +1,27 @@ +using Microsoft.AspNetCore.Authorization; +using SJZY.Track.Domain.Aggregates.OrderAggregate; + +namespace SJZY.Track.Application.Services.DebugApis; + +[AllowAnonymous] +[ApiExplorerSettings(GroupName = "调试专用")] +public class NormalService : ApplicationService +{ + private readonly IOrderRepository _orderRepository; + + public NormalService(IOrderRepository orderRepository) + { + _orderRepository = orderRepository; + } + + public async Task Hello(string name) + { + var order = await _orderRepository.GetFirstAsync(x => true); + if (order == null) + { + throw new BusinessException(message: "订单不存在"); + } + + return $"hello {name}"; + } +} diff --git a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs index b0f0133c..6c6b16ae 100644 --- a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs +++ b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningNotifyService.cs @@ -3,6 +3,7 @@ using FreeRedis; using Microsoft.Extensions.Options; using SJZY.Expand.ABP.Core; using SJZY.Expand.ABP.Core.Logger; +using SJZY.Expand.ABP.Core.PublicMethords; using SJZY.Track.Application.Contract.Dtos.Forewarning; using SJZY.Track.Application.Contract.Services; using SJZY.Track.Application.Options; @@ -90,14 +91,18 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep } var redisKey = GetForewarningNotifySetKeyRedisKey(dto.SubSystemId); - long addCounter, delCounter = 0; + long addCounter = 0, delCounter = 0; // 左区间加入预警队列,【只有第一次发生的才会被加入】 - addCounter = await RedisHelper.Client.ZAddNxAsync(redisKey, adds.Select(x => + var left = adds.Select(x => { var entry = new WarningNotifyCacheEntry(x.ruleId, x.orderId, x.counter); return new ZMember(entry.ToString(), x.ts); - }).ToArray()); + }).ToArray(); + if (!left.IsNullOrEmpty()) + { + addCounter = await RedisHelper.Client.ZAddNxAsync(redisKey, left); + } // 右区间移出预警队列,【只要发生了就移除】 var right = dels.Select(x => new WarningNotifyCacheEntry(x.ruleId, x.orderId, x.counter)).ToList(); @@ -106,7 +111,7 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep delCounter = await RedisHelper.Client.ZRemAsync(redisKey, right.Select(x => x.ToString()).ToArray()); } - LogHelper.Info($"本次共计产生{addCounter}条预警,移除{delCounter}条预警,触发缘由是{dto}"); + LogHelper.Info($"{nameof(ForewarningNotifyService)} 本次共计产生{addCounter}条预警,移除{delCounter}条预警,触发缘由是{dto}"); await _warningRecordEsRepository.BulkUpdateAsync(dels); // 标记处理完成 } @@ -119,7 +124,7 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep if (set.IsNullOrEmpty()) { - LogHelper.Info("没有运输时效需要预警"); + LogHelper.Info($"{nameof(ForewarningNotifyService)} 子系统:{subSystemId} 没有运输时效需要预警"); return 0; } @@ -174,7 +179,7 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep return warningRecords; } - private Task SendCoreAsync(int subSystemId, List entries, + private async Task SendCoreAsync(int subSystemId, List entries, List notifyRules) { var userRules = new Dictionary>(); @@ -192,14 +197,16 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep } // 以人的维度发送通知,异步 + var tasks = new List(); + foreach (var userRule in userRules) { - _ = DoNotifyAsync(subSystemId, userRule.Key, userRule.Value, entries, - NotifyMode.SiteInternal); - _ = DoNotifyAsync(subSystemId, userRule.Key, userRule.Value, entries, NotifyMode.FeiShu); + tasks.Add(DoNotifyAsync(subSystemId, userRule.Key, userRule.Value, entries, NotifyMode.SiteInternal)); + tasks.Add(DoNotifyAsync(subSystemId, userRule.Key, userRule.Value, entries, NotifyMode.FeiShu)); } - return Task.CompletedTask; + // 等待所有任务完成 + await Task.WhenAll(tasks); } private string GetForewarningNotifySetKeyRedisKey(int subSystemId) => @@ -266,7 +273,9 @@ public class ForewarningNotifyService : IForewarningNotifyService, ITransientDep if (res.Code != GlobalConst.SuccessCode) { - LogHelper.Error($"发送{mode.ToString()}通知失败,用户:{userId},内容:{msgContent}"); + // todo:失败是否上报重试? + LogHelper.Error( + $"{nameof(ForewarningNotifyService)} 发送{mode.ToString()}通知失败,用户:{userId},内容:\n{msgContent}\n失败报文:{res.ToJson()}"); } } diff --git a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs index ff697642..a2430bd8 100644 --- a/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs +++ b/src/SJZY.Track.Application/Services/ForewarningServices/ForewarningService.cs @@ -67,6 +67,7 @@ public class ForewarningService : ApplicationService /// /// /// + /// /// [HttpGet("EditWarning")] public async Task EditWarning(int warningId, diff --git a/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs b/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs index 341db4fe..655f2995 100644 --- a/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs +++ b/src/SJZY.Track.Application/Services/TimerTaskss/TrackWarningTask.cs @@ -14,6 +14,15 @@ namespace SJZY.Track.Application.Services.TimerTaskss; [ApiExplorerSettings(GroupName = "定时任务")] public class TrackWarningTask : ApplicationService { + private readonly IOptionsSnapshot _options; + private readonly IOrderRepository _orderRepository; + + public TrackWarningTask(IOrderRepository orderRepository, IOptionsSnapshot options) + { + _orderRepository = orderRepository; + _options = options; + } + /// /// 轨迹运输时效预警通知 /// @@ -22,29 +31,23 @@ public class TrackWarningTask : ApplicationService [HttpPost("/TimerTask/SendWarning")] public async Task SendWarningForTaskAsync() { - using var serviceScope = LazyServiceProvider.CreateScope(); - - var configs = serviceScope.ServiceProvider.GetRequiredService>() - .Value; + var configs = _options.Value; if (configs == null || configs.Keys.Count == 0) { throw new BusinessException("未配置子系统"); } - var orderRepository = serviceScope.ServiceProvider.GetRequiredService(); - var subSystemIds = await orderRepository.GetSubSystemListByCodesAsync(configs.Keys.ToList()); + var subSystemIds = await _orderRepository.GetSubSystemListByCodesAsync(configs.Keys.ToList()); foreach (var item in subSystemIds) { var ttl = configs[item.code].WarningNotifyInterval * 60; - await TrackWarningBySubSystemAsync(serviceScope, item.id, ttl); + await TrackWarningBySubSystemAsync(item.id, ttl); } - await Task.Delay(1000); // life-time possible dispose - return true; } - private async Task TrackWarningBySubSystemAsync(IServiceScope serviceScope, int subSystemId, int ttl) + private async Task TrackWarningBySubSystemAsync(int subSystemId, int ttl) { // 如果已经发送过通知,不再发送 var redisKey = $"{GlobalConst.ForewarningNotifyCheckKey}:{subSystemId}"; @@ -60,6 +63,8 @@ public class TrackWarningTask : ApplicationService { try { + using var serviceScope = LazyServiceProvider.CreateScope(); + var forewarningNotifyService = serviceScope.ServiceProvider.GetRequiredService(); var cnt = await forewarningNotifyService.SendNotificationAsync(subSystemId); diff --git a/src/SJZY.Track.Repository/GlobalUsing.cs b/src/SJZY.Track.Repository/GlobalUsing.cs index b124f61f..30edf0ad 100644 --- a/src/SJZY.Track.Repository/GlobalUsing.cs +++ b/src/SJZY.Track.Repository/GlobalUsing.cs @@ -2,6 +2,5 @@ global using SJZY.Expand.ABP.Core; global using SJZY.Expand.ABP.Core.Common.Dto; global using SJZY.Expand.ABP.Core.SqlSugar; global using SJZY.Expand.ABP.Core.SqlSugar.Entitys; -global using SJZY.Expand.ABP.Core.SqlSugar.Tools; global using SqlSugar; global using Volo.Abp.DependencyInjection; diff --git a/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj b/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj index 562b546b..bc16be84 100644 --- a/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj +++ b/src/SJZY.Track.Repository/SJZY.Track.Repository.csproj @@ -8,7 +8,7 @@ - +