Refactor email queue interface and RabbitMQ handling
Removed `DequeueAsync` from `IOutgoingEmailQueue` and added `GetQueueDepthAsync` to query the queue's message count. Updated RabbitMQ connection and channel creation methods to support `CancellationToken`. Removed `DequeueAsync` implementation from `OutgoingEmailQueue`, signaling a shift away from direct message consumption. These changes improve cancellation handling and simplify the queue's responsibilities.
This commit is contained in:
@@ -9,7 +9,7 @@ namespace DigitalData.EmailProfiler.Application.Common.Interfaces;
|
|||||||
public interface IOutgoingEmailQueue
|
public interface IOutgoingEmailQueue
|
||||||
{
|
{
|
||||||
Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default);
|
Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default);
|
||||||
Task<OutgoingEmailEvent?> DequeueAsync(CancellationToken cancellationToken = default);
|
|
||||||
Task<int> GetQueueDepthAsync(CancellationToken cancellationToken = default);
|
Task<int> GetQueueDepthAsync(CancellationToken cancellationToken = default);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -52,8 +52,8 @@ public class OutgoingEmailQueue : IOutgoingEmailQueue, IDisposable
|
|||||||
NetworkRecoveryInterval = TimeSpan.FromSeconds(_config.NetworkRecoveryIntervalSeconds)
|
NetworkRecoveryInterval = TimeSpan.FromSeconds(_config.NetworkRecoveryIntervalSeconds)
|
||||||
};
|
};
|
||||||
|
|
||||||
_connection = await factory.CreateConnectionAsync();
|
_connection = await factory.CreateConnectionAsync(cancellationToken);
|
||||||
_channel = await _connection.CreateChannelAsync();
|
_channel = await _connection.CreateChannelAsync(cancellationToken: cancellationToken);
|
||||||
|
|
||||||
// Declare Dead Letter Queue (DLQ) exchange
|
// Declare Dead Letter Queue (DLQ) exchange
|
||||||
await _channel.ExchangeDeclareAsync(
|
await _channel.ExchangeDeclareAsync(
|
||||||
@@ -108,7 +108,6 @@ public class OutgoingEmailQueue : IOutgoingEmailQueue, IDisposable
|
|||||||
_logger.LogInformation("RabbitMQ initialized successfully: Queue={QueueName}, DLQ={DlqQueueName}", _config.QueueName, _config.DlqQueueName);
|
_logger.LogInformation("RabbitMQ initialized successfully: Queue={QueueName}, DLQ={DlqQueueName}", _config.QueueName, _config.DlqQueueName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default)
|
public async Task EnqueueAsync(OutgoingEmailEvent outgoingEmailEvent, CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var json = JsonSerializer.Serialize(outgoingEmailEvent);
|
var json = JsonSerializer.Serialize(outgoingEmailEvent);
|
||||||
@@ -130,31 +129,6 @@ public class OutgoingEmailQueue : IOutgoingEmailQueue, IDisposable
|
|||||||
cancellationToken: cancellationToken);
|
cancellationToken: cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<OutgoingEmailEvent?> DequeueAsync(CancellationToken cancellationToken = default)
|
|
||||||
{
|
|
||||||
var result = await _channel.BasicGetAsync(_config.QueueName, false, cancellationToken);
|
|
||||||
|
|
||||||
if (result == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var json = Encoding.UTF8.GetString(result.Body.ToArray());
|
|
||||||
var email = JsonSerializer.Deserialize<OutgoingEmailEvent>(json);
|
|
||||||
|
|
||||||
// Acknowledge message after successful deserialization
|
|
||||||
await _channel.BasicAckAsync(result.DeliveryTag, false, cancellationToken);
|
|
||||||
|
|
||||||
return email;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// Reject and requeue message on error
|
|
||||||
await _channel.BasicNackAsync(result.DeliveryTag, false, true, cancellationToken);
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<int> GetQueueDepthAsync(CancellationToken cancellationToken = default)
|
public async Task<int> GetQueueDepthAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
var queueInfo = await _channel.QueueDeclarePassiveAsync(_config.QueueName, cancellationToken);
|
var queueInfo = await _channel.QueueDeclarePassiveAsync(_config.QueueName, cancellationToken);
|
||||||
|
|||||||
Reference in New Issue
Block a user