using System;
using Svrnty.CQRS.Events.ConsumerGroups.PostgreSQL;
using Svrnty.CQRS.Events.ConsumerGroups.Monitoring;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Svrnty.CQRS.Events.ConsumerGroups.Abstractions;
namespace Svrnty.CQRS.Events.ConsumerGroups;
///
/// Extension methods for registering consumer group services.
///
public static class ServiceCollectionExtensions
{
///
/// Adds PostgreSQL-based consumer group support with health monitoring.
/// Registers IConsumerOffsetStore, IConsumerGroupReader, and the health monitor background service.
///
/// The service collection.
/// Action to configure PostgreSQL storage options.
/// Optional action to configure health monitor options.
/// The service collection for chaining.
public static IServiceCollection AddPostgresConsumerGroups(
this IServiceCollection services,
Action configureStorage,
Action? configureHealthMonitor = null)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (configureStorage == null)
throw new ArgumentNullException(nameof(configureStorage));
// Configure storage options
services.Configure(configureStorage);
// Configure health monitor options (if provided)
if (configureHealthMonitor != null)
{
services.Configure(configureHealthMonitor);
}
// Register core services
services.TryAddSingleton();
services.TryAddSingleton();
// Register health monitor as hosted service
services.AddHostedService();
return services;
}
///
/// Adds PostgreSQL-based consumer group support with health monitoring.
/// Registers IConsumerOffsetStore, IConsumerGroupReader, and the health monitor background service.
///
/// The service collection.
/// Configuration section for PostgreSQL storage options.
/// Optional configuration section for health monitor options.
/// The service collection for chaining.
public static IServiceCollection AddPostgresConsumerGroups(
this IServiceCollection services,
IConfigurationSection storageConfiguration,
IConfigurationSection? healthMonitorConfiguration = null)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (storageConfiguration == null)
throw new ArgumentNullException(nameof(storageConfiguration));
// Configure storage options from configuration
services.Configure(options => storageConfiguration.Bind(options));
// Configure health monitor options from configuration (if provided)
if (healthMonitorConfiguration != null)
{
services.Configure(options => healthMonitorConfiguration.Bind(options));
}
// Register core services
services.TryAddSingleton();
services.TryAddSingleton();
// Register health monitor as hosted service
services.AddHostedService();
return services;
}
///
/// Adds consumer offset store implementation.
/// Use this if you only need offset tracking without the full consumer group reader.
///
/// The service collection.
/// Action to configure PostgreSQL storage options.
/// The service collection for chaining.
public static IServiceCollection AddPostgresConsumerOffsetStore(
this IServiceCollection services,
Action configureStorage)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (configureStorage == null)
throw new ArgumentNullException(nameof(configureStorage));
services.Configure(configureStorage);
services.TryAddSingleton();
return services;
}
///
/// Adds consumer offset store implementation.
/// Use this if you only need offset tracking without the full consumer group reader.
///
/// The service collection.
/// Configuration section for PostgreSQL storage options.
/// The service collection for chaining.
public static IServiceCollection AddPostgresConsumerOffsetStore(
this IServiceCollection services,
IConfigurationSection storageConfiguration)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (storageConfiguration == null)
throw new ArgumentNullException(nameof(storageConfiguration));
services.Configure(options => storageConfiguration.Bind(options));
services.TryAddSingleton();
return services;
}
///
/// Adds the consumer health monitor background service.
/// Requires IConsumerOffsetStore to be registered.
///
/// The service collection.
/// Optional action to configure health monitor options.
/// The service collection for chaining.
public static IServiceCollection AddConsumerHealthMonitor(
this IServiceCollection services,
Action? configure = null)
{
if (services == null)
throw new ArgumentNullException(nameof(services));
if (configure != null)
{
services.Configure(configure);
}
services.AddHostedService();
return services;
}
}