Просмотр исходного кода

!1282 完善EventBus重试机制
Merge pull request !1282 from koy0755/完善EventBus重试处理器

zuohuaijun 2 лет назад
Родитель
Сommit
cbf899a93a

+ 31 - 3
Admin.NET/Admin.NET.Core/EventBus/RetryEventHandlerExecutor.cs

@@ -4,6 +4,9 @@
 //
 // 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
 
+using Furion.EventBus;
+using Furion.Logging.Extensions;
+
 namespace Admin.NET.Core;
 
 /// <summary>
@@ -11,12 +14,37 @@ namespace Admin.NET.Core;
 /// </summary>
 public class RetryEventHandlerExecutor : IEventHandlerExecutor
 {
+    //private class Retry
+
     public async Task ExecuteAsync(EventHandlerExecutingContext context, Func<EventHandlerExecutingContext, Task> handler)
     {
-        // 如果执行失败,每隔 1s 重试,最多三次
+        var eventSubscribeAttribute = context.Attribute;
+        // 判断是否自定义了重试失败回调服务
+        var fallbackPolicyService = eventSubscribeAttribute?.FallbackPolicy == null
+            ? null
+            : App.GetService(eventSubscribeAttribute.FallbackPolicy) as IEventFallbackPolicy;
+
         await Retry.InvokeAsync(async () =>
         {
-            await handler(context);
-        }, 3, 1000);
+            try
+            {
+                await handler(context);
+            }
+            catch (Exception ex)
+            {
+                Log.Error($"Invoke EventHandler {context.Source.EventId} Error", ex);
+                throw;
+            }
+        }
+        , eventSubscribeAttribute?.NumRetries ?? 0
+        , eventSubscribeAttribute?.RetryTimeout ?? 1000
+        , exceptionTypes: eventSubscribeAttribute?.ExceptionTypes
+        , fallbackPolicy: fallbackPolicyService == null ? null : async (Exception ex) => { await fallbackPolicyService.CallbackAsync(context, ex); }
+        , retryAction: (total, times) =>
+        {
+            // 输出重试日志
+            Log.Warning($"Retrying {times}/{total} times for  EventHandler {context.Source.EventId}");
+        }
+        );
     }
 }

+ 3 - 9
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -1,4 +1,4 @@
-// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
 //
 // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
 //
@@ -133,10 +133,7 @@ public static class SqlSugarSetup
             {
                 if (ex.Parametres == null) return;
                 var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
-                var originColor = Console.ForegroundColor;
-                Console.ForegroundColor = ConsoleColor.DarkRed;
-                Console.WriteLine(log);
-                Console.ForegroundColor = originColor;
+                Log.Error(log, ex);
                 App.PrintToMiniProfiler("SqlSugar", "Error", log);
             };
             db.Aop.OnLogExecuted = (sql, pars) =>
@@ -156,10 +153,7 @@ public static class SqlSugarSetup
                     var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
                     var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
                     var log = $"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + $"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}";
-                    var originColor = Console.ForegroundColor;
-                    Console.ForegroundColor = ConsoleColor.DarkYellow;
-                    Console.WriteLine(log);
-                    Console.ForegroundColor = originColor;
+                    Log.Warning(log);
                     App.PrintToMiniProfiler("SqlSugar", "Slow", log);
                 }
             };

+ 17 - 2
Admin.NET/Admin.NET.Web.Core/Startup.cs

@@ -1,4 +1,4 @@
-// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
+// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
 //
 // 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
 //
@@ -8,6 +8,9 @@ using Admin.NET.Core;
 using Admin.NET.Core.Service;
 using AspNetCoreRateLimit;
 using Furion;
+using Furion.EventBus;
+using Furion.Logging;
+using Furion.Logging.Extensions;
 using Furion.SpecificationDocument;
 using Furion.VirtualFileServer;
 using IGeekFan.AspNetCore.Knife4jUI;
@@ -121,9 +124,21 @@ public class Startup : AppStartup
             options.UseUtcTimestamp = false;
             // 不启用事件日志
             options.LogEnabled = false;
-            // 事件执行器(失败重试)
+
+            // 事件执行器(失败重试处理方式)
             options.AddExecutor<RetryEventHandlerExecutor>();
 
+            // 事件执行器,重试后依然处理未处理异常的处理器
+            options.UnobservedTaskExceptionHandler = (obj, args) => {
+                if (args.Exception?.Message != null)
+                {
+                    Log.Error($"EeventBus 有未处理异常 :{args.Exception?.Message} ", args.Exception);
+                }
+            };
+
+            //事件执行器-监视器,每一次处理都会进入
+            options.AddMonitor<EventHandlerMonitor>();
+
             #region Redis消息队列
 
             //// 替换事件源存储器