0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-05 05:23:57 +00:00

feat: Improve rabbit connection and channel handling

This commit is contained in:
alex289 2025-02-27 19:57:17 +01:00
parent 0234b28214
commit a780612e9c
No known key found for this signature in database
GPG Key ID: 573F77CD2D87F863

View File

@ -21,6 +21,7 @@ public sealed class RabbitMqHandler : BackgroundService
private readonly ConcurrentQueue<IRabbitMqAction> _pendingActions = new(); private readonly ConcurrentQueue<IRabbitMqAction> _pendingActions = new();
private IChannel? _channel; private IChannel? _channel;
private IConnection? _connection;
public RabbitMqHandler( public RabbitMqHandler(
RabbitMqConfiguration configuration, RabbitMqConfiguration configuration,
@ -49,18 +50,35 @@ public sealed class RabbitMqHandler : BackgroundService
Password = _configuration.Password Password = _configuration.Password
}; };
var connection = await factory.CreateConnectionAsync(cancellationToken); _connection = await factory.CreateConnectionAsync(cancellationToken);
_channel = await connection.CreateChannelAsync(null, cancellationToken); _channel = await _connection.CreateChannelAsync(null, cancellationToken);
await base.StartAsync(cancellationToken); await base.StartAsync(cancellationToken);
} }
public override async Task StopAsync(CancellationToken cancellationToken)
{
if (_channel is not null)
{
await _channel.CloseAsync(cancellationToken);
await _channel.DisposeAsync();
}
if (_connection is not null)
{
await _connection.CloseAsync(cancellationToken);
await _connection.DisposeAsync();
}
await base.StopAsync(cancellationToken);
}
public void InitializeExchange(string exchangeName, string type = ExchangeType.Fanout) public void InitializeExchange(string exchangeName, string type = ExchangeType.Fanout)
{ {
if (!_configuration.Enabled) if (!_configuration.Enabled)
{ {
_logger.LogInformation("RabbitMQ is disabled. Skipping the creation of exchange {exchangeName}.", _logger.LogInformation("RabbitMQ is disabled. Skipping the creation of exchange {ExchangeName}.",
exchangeName); exchangeName);
return; return;
} }
@ -214,21 +232,50 @@ public sealed class RabbitMqHandler : BackgroundService
return; return;
} }
while (true) while (!stoppingToken.IsCancellationRequested)
{ {
await HandleEnqueuedActions(); var channel = await GetOpenChannelAsync(stoppingToken);
if (channel is not null)
{
await HandleEnqueuedActionsAsync(channel);
}
await Task.Delay(1000, stoppingToken); await Task.Delay(1000, stoppingToken);
} }
} }
private async Task HandleEnqueuedActions() private async ValueTask<IChannel?> GetOpenChannelAsync(CancellationToken stoppingToken)
{
var channel = _channel;
if (_connection is null || channel is null || channel.IsClosed)
{
return null;
}
try
{
channel = await _connection.CreateChannelAsync(null, stoppingToken);
_channel = channel;
return channel;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error while trying to create a new channel");
}
return null;
}
private async Task HandleEnqueuedActionsAsync(IChannel channel)
{ {
while (_pendingActions.TryDequeue(out var action)) while (_pendingActions.TryDequeue(out var action))
{ {
try try
{ {
await action.Perform(_channel!); await action.Perform(channel);
} }
catch (Exception ex) catch (Exception ex)
{ {