Skip to content
6 changes: 4 additions & 2 deletions src/StarGate.Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

namespace StarGate.Infrastructure;

public static partial class DependencyInjection

Check warning on line 11 in src/StarGate.Infrastructure/DependencyInjection.cs

View workflow job for this annotation

GitHub Actions / Quality

Missing XML comment for publicly visible type or member 'DependencyInjection'
{
public static IServiceCollection AddMessageBroker(

Check warning on line 13 in src/StarGate.Infrastructure/DependencyInjection.cs

View workflow job for this annotation

GitHub Actions / Quality

Missing XML comment for publicly visible type or member 'DependencyInjection.AddMessageBroker(IServiceCollection, IConfiguration)'
this IServiceCollection services,
IConfiguration configuration)
{
Expand All @@ -34,13 +34,15 @@
// Register RabbitMQ options
services.AddSingleton(options);

// Register RabbitMQ broker
// Register RabbitMQ broker and consumer
services.AddSingleton<IMessageBroker, RabbitMqBroker>();
services.AddSingleton<IMessageConsumer, RabbitMqConsumer>();
}
else
{
// Null object pattern - no-op broker when RabbitMQ is disabled
// Null object pattern - no-op broker and consumer when RabbitMQ is disabled
services.AddSingleton<IMessageBroker, NullMessageBroker>();
services.AddSingleton<IMessageConsumer, NullMessageConsumer>();
}

return services;
Expand Down
55 changes: 55 additions & 0 deletions src/StarGate.Infrastructure/Messaging/NullMessageConsumer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Microsoft.Extensions.Logging;
using StarGate.Core.Abstractions;

namespace StarGate.Infrastructure.Messaging;

/// <summary>
/// Null object implementation of <see cref="IMessageConsumer"/>.
/// Used when RabbitMQ is disabled or unavailable.
/// Logs warnings and ignores all consumption requests.
/// </summary>
public sealed class NullMessageConsumer : IMessageConsumer
{
private readonly ILogger<NullMessageConsumer> _logger;
private bool _disposed;

public NullMessageConsumer(ILogger<NullMessageConsumer> logger)

Check warning on line 16 in src/StarGate.Infrastructure/Messaging/NullMessageConsumer.cs

View workflow job for this annotation

GitHub Actions / Quality

Missing XML comment for publicly visible type or member 'NullMessageConsumer.NullMessageConsumer(ILogger<NullMessageConsumer>)'
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_logger.LogWarning("NullMessageConsumer is active - no messages will be consumed");
}

public Task StartConsumingAsync<T>(

Check warning on line 22 in src/StarGate.Infrastructure/Messaging/NullMessageConsumer.cs

View workflow job for this annotation

GitHub Actions / Quality

Missing XML comment for publicly visible type or member 'NullMessageConsumer.StartConsumingAsync<T>(Func<T, MessageContext, Task>, CancellationToken)'
Func<T, MessageContext, Task> messageHandler,
CancellationToken ct = default)
where T : class
{
ArgumentNullException.ThrowIfNull(messageHandler);
ObjectDisposedException.ThrowIf(_disposed, this);

var queueName = $"stargate.{typeof(T).Name.ToLowerInvariant()}";

_logger.LogWarning(
"NullMessageConsumer: Ignoring consume request for queue {Queue}",
queueName);

return Task.CompletedTask;
}

public Task StopConsumingAsync()
{
ObjectDisposedException.ThrowIf(_disposed, this);
return Task.CompletedTask;
}

public ValueTask DisposeAsync()
{
if (!_disposed)
{
_logger.LogInformation("NullMessageConsumer disposed");
_disposed = true;
}

return ValueTask.CompletedTask;
}
}
Loading
Loading