104 lines
3.4 KiB
C#
104 lines
3.4 KiB
C#
using FictionArchive.Service.Shared.MassTransit.Configuration;
|
|
using MassTransit;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace FictionArchive.Service.Shared.MassTransit;
|
|
|
|
public static class MassTransitExtensions
|
|
{
|
|
/// <summary>
|
|
/// Adds MassTransit with RabbitMQ and Entity Framework outbox
|
|
/// </summary>
|
|
public static IServiceCollection AddFictionArchiveMassTransit<TDbContext>(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
Action<IBusRegistrationConfigurator>? configureConsumers = null)
|
|
where TDbContext : DbContext
|
|
{
|
|
services.AddMassTransit(x =>
|
|
{
|
|
configureConsumers?.Invoke(x);
|
|
|
|
x.AddEntityFrameworkOutbox<TDbContext>(o =>
|
|
{
|
|
o.UsePostgres();
|
|
o.UseBusOutbox();
|
|
});
|
|
|
|
x.UsingRabbitMq((context, cfg) =>
|
|
{
|
|
var options = configuration.GetSection("RabbitMQ").Get<MassTransitOptions>()
|
|
?? new MassTransitOptions();
|
|
|
|
cfg.Host(options.Host, options.VirtualHost, h =>
|
|
{
|
|
h.Username(options.Username);
|
|
h.Password(options.Password);
|
|
});
|
|
|
|
// Immediate retries for transient failures
|
|
cfg.UseMessageRetry(r => r.Intervals(
|
|
TimeSpan.FromSeconds(1),
|
|
TimeSpan.FromSeconds(1),
|
|
TimeSpan.FromSeconds(1)));
|
|
|
|
// Delayed redelivery for longer outages
|
|
cfg.UseDelayedRedelivery(r => r.Intervals(
|
|
TimeSpan.FromSeconds(5),
|
|
TimeSpan.FromSeconds(30),
|
|
TimeSpan.FromMinutes(2),
|
|
TimeSpan.FromMinutes(10),
|
|
TimeSpan.FromMinutes(30)));
|
|
|
|
cfg.ConfigureEndpoints(context);
|
|
});
|
|
});
|
|
|
|
return services;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds MassTransit with RabbitMQ without outbox (for services without EF)
|
|
/// </summary>
|
|
public static IServiceCollection AddFictionArchiveMassTransit(
|
|
this IServiceCollection services,
|
|
IConfiguration configuration,
|
|
Action<IBusRegistrationConfigurator>? configureConsumers = null)
|
|
{
|
|
services.AddMassTransit(x =>
|
|
{
|
|
configureConsumers?.Invoke(x);
|
|
|
|
x.UsingRabbitMq((context, cfg) =>
|
|
{
|
|
var options = configuration.GetSection("RabbitMQ").Get<MassTransitOptions>()
|
|
?? new MassTransitOptions();
|
|
|
|
cfg.Host(options.Host, options.VirtualHost, h =>
|
|
{
|
|
h.Username(options.Username);
|
|
h.Password(options.Password);
|
|
});
|
|
|
|
cfg.UseMessageRetry(r => r.Intervals(
|
|
TimeSpan.FromSeconds(1),
|
|
TimeSpan.FromSeconds(1),
|
|
TimeSpan.FromSeconds(1)));
|
|
|
|
cfg.UseDelayedRedelivery(r => r.Intervals(
|
|
TimeSpan.FromSeconds(5),
|
|
TimeSpan.FromSeconds(30),
|
|
TimeSpan.FromMinutes(2),
|
|
TimeSpan.FromMinutes(10),
|
|
TimeSpan.FromMinutes(30)));
|
|
|
|
cfg.ConfigureEndpoints(context);
|
|
});
|
|
});
|
|
|
|
return services;
|
|
}
|
|
}
|