From 0020680778f4a0ecdd6ebc749c815f0375eef205 Mon Sep 17 00:00:00 2001
From: Khadikov Alan <90841049+alan-kh@users.noreply.github.com>
Date: Thu, 7 Nov 2024 08:26:50 -0300
Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?=
=?UTF-8?q?=D0=BE=D0=BF=D1=86=D0=B8=D1=8E=20EnableConsoleLogger=20=D0=B2?=
=?UTF-8?q?=20EventbusOption=20(#28)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Add option for local testing
---
.../ATI.Services.RabbitMQ.csproj | 4 +--
ATI.Services.RabbitMQ/EventbusManager.cs | 25 +++++++++++++++----
ATI.Services.RabbitMQ/EventbusOptions.cs | 2 ++
3 files changed, 24 insertions(+), 7 deletions(-)
diff --git a/ATI.Services.RabbitMQ/ATI.Services.RabbitMQ.csproj b/ATI.Services.RabbitMQ/ATI.Services.RabbitMQ.csproj
index ba81414..6db3e5d 100644
--- a/ATI.Services.RabbitMQ/ATI.Services.RabbitMQ.csproj
+++ b/ATI.Services.RabbitMQ/ATI.Services.RabbitMQ.csproj
@@ -18,7 +18,7 @@
1701;1702;CS1591;CS1571;CS1573;CS1574
-
-
+
+
\ No newline at end of file
diff --git a/ATI.Services.RabbitMQ/EventbusManager.cs b/ATI.Services.RabbitMQ/EventbusManager.cs
index 0cb8b07..3213c3b 100644
--- a/ATI.Services.RabbitMQ/EventbusManager.cs
+++ b/ATI.Services.RabbitMQ/EventbusManager.cs
@@ -84,7 +84,11 @@ public Task InitializeAsync()
serviceRegister =>
{
serviceRegister.Register(c =>
- new RabbitMqConventions(c.Resolve(), _options));
+ new RabbitMqConventions(c.Resolve(), _options)
+ );
+
+ if (_options.EnableConsoleLogger)
+ serviceRegister.EnableConsoleLogger();
}).Advanced;
_busClient.Connected += (_, _) => ResubscribeOnReconnect();
@@ -173,7 +177,8 @@ await _busClient.PublishAsync(
routingKey,
mandatory,
messageProperties,
- body)
+ body
+ )
);
if (sendingResult.FinalException != null)
@@ -235,10 +240,20 @@ public Task SubscribeAsync(QueueExchangeBinding bindingInfo,
}));
}
- private static AsyncPolicyWrap SetupPolicy(TimeSpan? timeout = null) =>
- Policy.WrapAsync(Policy.TimeoutAsync(timeout ?? TimeSpan.FromSeconds(2)),
+ private AsyncPolicyWrap SetupPolicy(TimeSpan? timeout = null) =>
+ Policy.WrapAsync(
+ Policy.TimeoutAsync(timeout ?? TimeSpan.FromSeconds(3)),
Policy.Handle()
- .WaitAndRetryAsync(3, _ => TimeSpan.FromSeconds(3)));
+ .WaitAndRetryAsync(
+ 3,
+ _ => TimeSpan.FromSeconds(1),
+ (exception, timeSpan, retryCount, _) =>
+ {
+ if(_options.LogInnerExceptionsInRetryPolicy)
+ _logger.ErrorWithObject(exception, new { TimeSpan = timeSpan, RetryCount = retryCount });
+ }
+ )
+ );
private async Task ExecuteWithPolicy(Func> action)
{
diff --git a/ATI.Services.RabbitMQ/EventbusOptions.cs b/ATI.Services.RabbitMQ/EventbusOptions.cs
index 3906e15..044aeaf 100644
--- a/ATI.Services.RabbitMQ/EventbusOptions.cs
+++ b/ATI.Services.RabbitMQ/EventbusOptions.cs
@@ -15,6 +15,8 @@ public class EventbusOptions
public bool AddHostnamePostfixToQueues { get; init; }
public bool DeleteQueuesOnApplicationShutdown { get; init; }
+ public bool EnableConsoleLogger { get; init; }
+ public bool LogInnerExceptionsInRetryPolicy { get; init; }
#endregion
}
\ No newline at end of file