2024-01-25 13:51:48 +00:00
|
|
|
using Elsa.Extensions;
|
2023-11-15 19:47:11 +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-11-15 19:47:11 +00:00
|
|
|
using MassTransit;
|
2024-01-25 13:51:48 +00:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2023-11-15 19:47:11 +00:00
|
|
|
|
|
|
|
|
namespace Elsa.MassTransit.AzureServiceBus.Features;
|
|
|
|
|
|
|
|
|
|
/// Configures MassTransit to use the Azure Service Bus transport.
|
2023-12-11 18:59:13 +00:00
|
|
|
/// See https://masstransit.io/documentation/configuration/transports/azure-service-bus
|
2023-11-15 19:47:11 +00:00
|
|
|
[DependsOn(typeof(MassTransitFeature))]
|
|
|
|
|
public class AzureServiceBusFeature : FeatureBase
|
|
|
|
|
{
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public AzureServiceBusFeature(IModule module) : base(module)
|
|
|
|
|
{
|
|
|
|
|
}
|
2023-12-11 18:59:13 +00:00
|
|
|
|
|
|
|
|
/// An Azure Service Bus connection string.
|
|
|
|
|
public string? ConnectionString { get; set; }
|
2023-11-15 19:47:11 +00:00
|
|
|
|
|
|
|
|
/// <summary>
|
2023-12-11 18:59:13 +00:00
|
|
|
/// A delegate that configures the Azure Service Bus transport options.
|
2023-11-15 19:47:11 +00:00
|
|
|
/// </summary>
|
2023-12-11 18:59:13 +00:00
|
|
|
public Action<IServiceBusBusFactoryConfigurator>? ConfigureServiceBus { get; set; }
|
2023-11-15 19:47:11 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Configure()
|
|
|
|
|
{
|
|
|
|
|
Module.Configure<MassTransitFeature>(massTransitFeature =>
|
|
|
|
|
{
|
|
|
|
|
massTransitFeature.BusConfigurator = configure =>
|
|
|
|
|
{
|
2024-01-25 13:51:48 +00:00
|
|
|
var tempConsumers = massTransitFeature.GetConsumers()
|
|
|
|
|
.Where(c => c.IsTemporary)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
2023-11-15 19:47:11 +00:00
|
|
|
configure.AddServiceBusMessageScheduler();
|
2024-01-25 13:51:48 +00:00
|
|
|
configure.AddConsumers(tempConsumers.Select(c => c.ConsumerType).ToArray());
|
2023-11-15 19:47:11 +00:00
|
|
|
|
|
|
|
|
configure.UsingAzureServiceBus((context, serviceBus) =>
|
|
|
|
|
{
|
2024-01-25 13:51:48 +00:00
|
|
|
var options = context.GetRequiredService<IOptions<MassTransitWorkflowDispatcherOptions>>().Value;
|
|
|
|
|
var instanceNameRetriever = context.GetRequiredService<IInstanceNameRetriever>();
|
|
|
|
|
|
2023-11-15 19:47:11 +00:00
|
|
|
if (ConnectionString != null)
|
|
|
|
|
serviceBus.Host(ConnectionString);
|
|
|
|
|
serviceBus.UseServiceBusMessageScheduler();
|
2023-12-11 18:59:13 +00:00
|
|
|
ConfigureServiceBus?.Invoke(serviceBus);
|
2024-01-25 13:51:48 +00:00
|
|
|
|
|
|
|
|
foreach (var consumer in tempConsumers)
|
|
|
|
|
{
|
|
|
|
|
//Throw on no known name?
|
|
|
|
|
serviceBus.ReceiveEndpoint($"{instanceNameRetriever.GetName()}-{consumer.Name}", configurator =>
|
|
|
|
|
{
|
|
|
|
|
configurator.AutoDeleteOnIdle = options.ShortTermQueueLifetime ?? TimeSpan.FromHours(1);
|
|
|
|
|
configurator.ConcurrentMessageLimit = options.ConcurrentMessageLimit;
|
|
|
|
|
configurator.ConfigureConsumer<DispatchCancelWorkflowsRequestConsumer>(context);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 19:47:11 +00:00
|
|
|
serviceBus.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter("Elsa", false));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|