connection retry added
All checks were successful
Build and publish package / Build (push) Successful in 26s
Build and publish package / Determine version (push) Successful in 20s
Build and publish package / Pack (push) Successful in 26s
Build and publish package / Publish (push) Successful in 13s

This commit is contained in:
2026-03-02 22:44:11 +01:00
Unverified
parent 3cc13eef1f
commit f0fda3d7c6

View File

@@ -1,5 +1,6 @@
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using RabbitMQ.Client; using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;
namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ; namespace TimetableDesigner.Backend.Events.Providers.RabbitMQ;
@@ -20,6 +21,16 @@ public class RabbitMQEventQueue : EventQueue<RabbitMQEventQueue>
string password = connectionParameters["Password"]; string password = connectionParameters["Password"];
string exchangeName = connectionParameters["ExchangeName"]; string exchangeName = connectionParameters["ExchangeName"];
string queuePrefix = connectionParameters["QueuePrefix"]; string queuePrefix = connectionParameters["QueuePrefix"];
if (!connectionParameters.TryGetValue("Retries", out string retriesStr))
{
retriesStr = "0";
}
int retries = int.Parse(retriesStr);
if (!connectionParameters.TryGetValue("RetryCooldown", out string retryCooldownStr))
{
retryCooldownStr = "1000";
}
int retryCooldown = int.Parse(retryCooldownStr);
ConnectionFactory factory = new ConnectionFactory ConnectionFactory factory = new ConnectionFactory
{ {
@@ -29,9 +40,33 @@ public class RabbitMQEventQueue : EventQueue<RabbitMQEventQueue>
Password = password, Password = password,
}; };
Task<IConnection> createConnectionTask = factory.CreateConnectionAsync(); IConnection? connection = null;
int retryCount = 0;
Exception lastException = new Exception("Cannot connect to RabbitMQ");
while (connection is null && (retries < 0 || retryCount < retries))
{
try
{
using (Task<IConnection> createConnectionTask = factory.CreateConnectionAsync())
{
createConnectionTask.Wait(); createConnectionTask.Wait();
services.AddSingleton(createConnectionTask.Result); connection = createConnectionTask.Result;
}
}
catch (Exception ex)
{
Thread.Sleep(retryCooldown);
retryCount++;
lastException = ex;
}
}
if (connection is null)
{
throw lastException;
}
services.AddSingleton(connection);
services.AddSingleton<IEventQueuePublisher, RabbitMQEventQueuePublisher>(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService<IConnection>(), exchangeName)); services.AddSingleton<IEventQueuePublisher, RabbitMQEventQueuePublisher>(sp => new RabbitMQEventQueuePublisher(sp.GetRequiredService<IConnection>(), exchangeName));
services.AddSingleton<IEventQueueSubscriber, RabbitMQEventQueueSubscriber>(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService<IConnection>(), exchangeName, queuePrefix)); services.AddSingleton<IEventQueueSubscriber, RabbitMQEventQueueSubscriber>(sp => new RabbitMQEventQueueSubscriber(sp.GetRequiredService<IConnection>(), exchangeName, queuePrefix));
} }