2024-01-25 13:51:48 +00:00
|
|
|
using Elsa.Extensions;
|
2023-01-01 17:02:38 +00:00
|
|
|
using Elsa.Features.Abstractions;
|
|
|
|
|
using Elsa.Features.Attributes;
|
|
|
|
|
using Elsa.Features.Services;
|
2024-01-25 13:51:48 +00:00
|
|
|
using Elsa.MassTransit.Consumers;
|
2023-11-15 19:47:11 +00:00
|
|
|
using Elsa.MassTransit.Features;
|
2024-01-25 13:51:48 +00:00
|
|
|
using Elsa.MassTransit.Options;
|
|
|
|
|
using Elsa.Workflows.Contracts;
|
2023-01-01 17:02:38 +00:00
|
|
|
using MassTransit;
|
2023-12-11 18:59:13 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2024-01-25 13:51:48 +00:00
|
|
|
using Microsoft.Extensions.Options;
|
2023-01-01 17:02:38 +00:00
|
|
|
|
2023-11-15 19:47:11 +00:00
|
|
|
namespace Elsa.MassTransit.RabbitMq.Features;
|
2023-01-01 17:02:38 +00:00
|
|
|
|
2023-01-04 14:28:28 +00:00
|
|
|
/// <summary>
|
2023-11-15 19:47:11 +00:00
|
|
|
/// Configures MassTransit to use the RabbitMQ transport.
|
2023-01-04 14:28:28 +00:00
|
|
|
/// </summary>
|
2023-01-01 17:02:38 +00:00
|
|
|
[DependsOn(typeof(MassTransitFeature))]
|
|
|
|
|
public class RabbitMqServiceBusFeature : FeatureBase
|
|
|
|
|
{
|
2023-01-04 14:28:28 +00:00
|
|
|
/// <inheritdoc />
|
2023-01-01 17:02:38 +00:00
|
|
|
public RabbitMqServiceBusFeature(IModule module) : base(module)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 14:28:28 +00:00
|
|
|
/// A RabbitMQ connection string.
|
2023-12-11 18:59:13 +00:00
|
|
|
public string? ConnectionString { get; set; }
|
2023-01-01 17:02:38 +00:00
|
|
|
|
2023-12-11 18:59:13 +00:00
|
|
|
/// Configures the RabbitMQ transport options.
|
|
|
|
|
public Action<RabbitMqTransportOptions>? TransportOptions { get; set; }
|
|
|
|
|
|
2023-01-04 14:28:28 +00:00
|
|
|
/// <summary>
|
2023-12-11 18:59:13 +00:00
|
|
|
/// Configures the RabbitMQ bus.
|
2023-01-04 14:28:28 +00:00
|
|
|
/// </summary>
|
2023-12-11 18:59:13 +00:00
|
|
|
public Action<IRabbitMqBusFactoryConfigurator>? ConfigureServiceBus { get; set; }
|
2023-01-04 14:28:28 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-01-01 17:02:38 +00:00
|
|
|
public override void Configure()
|
|
|
|
|
{
|
2024-01-25 13:51:48 +00:00
|
|
|
Module.Configure<MassTransitFeature>(massTransitFeature =>
|
2023-01-01 17:02:38 +00:00
|
|
|
{
|
2024-01-25 13:51:48 +00:00
|
|
|
massTransitFeature.BusConfigurator = configure =>
|
2023-01-01 17:02:38 +00:00
|
|
|
{
|
2024-01-25 13:51:48 +00:00
|
|
|
var tempConsumers = massTransitFeature.GetConsumers()
|
|
|
|
|
.Where(c => c.IsTemporary)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
configure.AddConsumers(tempConsumers.Select(c => c.ConsumerType).ToArray());
|
|
|
|
|
|
|
|
|
|
configure.UsingRabbitMq((context, serviceBus) =>
|
|
|
|
|
{
|
|
|
|
|
var options = context.GetRequiredService<IOptions<MassTransitWorkflowDispatcherOptions>>().Value;
|
|
|
|
|
var instanceNameRetriever = context.GetRequiredService<IInstanceNameRetriever>();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(ConnectionString))
|
|
|
|
|
serviceBus.Host(ConnectionString);
|
2023-12-11 18:59:13 +00:00
|
|
|
|
2024-01-25 13:51:48 +00:00
|
|
|
ConfigureServiceBus?.Invoke(serviceBus);
|
|
|
|
|
|
|
|
|
|
foreach (var consumer in tempConsumers)
|
|
|
|
|
{
|
|
|
|
|
serviceBus.ReceiveEndpoint($"{instanceNameRetriever.GetName()}-{consumer.Name}", configurator =>
|
|
|
|
|
{
|
|
|
|
|
configurator.QueueExpiration = options.ShortTermQueueLifetime ?? TimeSpan.FromHours(1);
|
|
|
|
|
configurator.ConcurrentMessageLimit = options.ConcurrentMessageLimit;
|
|
|
|
|
configurator.ConfigureConsumer<DispatchCancelWorkflowsRequestConsumer>(context);
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-12-11 18:59:13 +00:00
|
|
|
|
2024-01-25 13:51:48 +00:00
|
|
|
serviceBus.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter("Elsa", false));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
});
|
2023-01-01 17:02:38 +00:00
|
|
|
}
|
2023-12-11 18:59:13 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Apply()
|
|
|
|
|
{
|
|
|
|
|
if (TransportOptions != null) Services.Configure(TransportOptions);
|
|
|
|
|
}
|
2023-01-01 17:02:38 +00:00
|
|
|
}
|