2024-02-12 14:32:48 +00:00
|
|
|
using Azure.Messaging.ServiceBus.Administration;
|
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-02-16 13:00:28 +00:00
|
|
|
using Elsa.Hosting.Management.Contracts;
|
2024-02-12 14:32:48 +00:00
|
|
|
using Elsa.MassTransit.AzureServiceBus.Handlers;
|
|
|
|
|
using Elsa.MassTransit.AzureServiceBus.Models;
|
|
|
|
|
using Elsa.MassTransit.AzureServiceBus.Options;
|
|
|
|
|
using Elsa.MassTransit.AzureServiceBus.Services;
|
2023-11-15 19:47:11 +00:00
|
|
|
using Elsa.MassTransit.Features;
|
2024-02-12 14:32:48 +00:00
|
|
|
using Elsa.MassTransit.Models;
|
2024-01-25 13:51:48 +00:00
|
|
|
using Elsa.MassTransit.Options;
|
2023-11-15 19:47:11 +00:00
|
|
|
using MassTransit;
|
2024-02-12 14:32:48 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
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)
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-02-20 10:26:52 +00:00
|
|
|
|
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; }
|
2024-02-20 10:26:52 +00:00
|
|
|
|
2024-02-13 13:35:39 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to configure <see cref="AzureServiceBusOptions"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Action<AzureServiceBusOptions> AzureServiceBusOptions { get; set; } = _ => { };
|
2024-02-20 10:26:52 +00:00
|
|
|
|
2024-02-12 14:32:48 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// A delegate to create a <see cref="ServiceBusAdministrationClient"/> instance.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<IServiceProvider, ServiceBusAdministrationClient> ServiceBusAdministrationClientFactory { get; set; } = sp => new(GetConnectionString(sp));
|
2023-11-15 19:47:11 +00:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Configure()
|
|
|
|
|
{
|
|
|
|
|
Module.Configure<MassTransitFeature>(massTransitFeature =>
|
|
|
|
|
{
|
|
|
|
|
massTransitFeature.BusConfigurator = configure =>
|
|
|
|
|
{
|
2024-02-12 14:30:19 +00:00
|
|
|
var consumers = massTransitFeature.GetConsumers().ToList();
|
2024-02-16 13:00:28 +00:00
|
|
|
var temporaryConsumers = consumers
|
|
|
|
|
.Where(c => c.IsTemporary)
|
2024-01-25 13:51:48 +00:00
|
|
|
.ToList();
|
2024-02-12 14:32:48 +00:00
|
|
|
RegisterConsumers(consumers);
|
2024-02-20 10:26:52 +00:00
|
|
|
|
2023-11-15 19:47:11 +00:00
|
|
|
configure.AddServiceBusMessageScheduler();
|
2024-02-16 13:00:28 +00:00
|
|
|
configure.AddConsumers(temporaryConsumers.Select(c => c.ConsumerType).ToArray());
|
2024-02-20 10:26:52 +00:00
|
|
|
|
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;
|
2024-02-16 13:00:28 +00:00
|
|
|
var instanceNameProvider = context.GetRequiredService<IApplicationInstanceNameProvider>();
|
2024-01-25 13:51:48 +00:00
|
|
|
|
2024-02-20 10:26:52 +00:00
|
|
|
if (ConnectionString != null)
|
2023-11-15 19:47:11 +00:00
|
|
|
serviceBus.Host(ConnectionString);
|
|
|
|
|
serviceBus.UseServiceBusMessageScheduler();
|
2023-12-11 18:59:13 +00:00
|
|
|
ConfigureServiceBus?.Invoke(serviceBus);
|
2024-01-25 13:51:48 +00:00
|
|
|
|
2024-02-16 13:00:28 +00:00
|
|
|
foreach (var consumer in temporaryConsumers)
|
2024-01-25 13:51:48 +00:00
|
|
|
{
|
2024-02-16 13:00:28 +00:00
|
|
|
serviceBus.ReceiveEndpoint($"Elsa-{instanceNameProvider.GetName()}-{consumer.Name}", configurator =>
|
2024-01-25 13:51:48 +00:00
|
|
|
{
|
2024-02-16 13:00:28 +00:00
|
|
|
configurator.AutoDeleteOnIdle = options.TemporaryQueueTtl ?? TimeSpan.FromHours(1);
|
2024-01-25 13:51:48 +00:00
|
|
|
configurator.ConcurrentMessageLimit = options.ConcurrentMessageLimit;
|
2024-02-09 14:05:51 +00:00
|
|
|
configurator.ConfigureConsumer(context, consumer.ConsumerType);
|
2024-01-25 13:51:48 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 19:47:11 +00:00
|
|
|
serviceBus.ConfigureEndpoints(context, new KebabCaseEndpointNameFormatter("Elsa", false));
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-02-12 14:32:48 +00:00
|
|
|
|
|
|
|
|
private void RegisterConsumers(List<ConsumerTypeDefinition> consumers)
|
|
|
|
|
{
|
2024-02-20 10:26:52 +00:00
|
|
|
var subscriptionTopology = (
|
|
|
|
|
from consumer in consumers
|
|
|
|
|
from consumerInterface in consumer.ConsumerType.GetInterfaces()
|
|
|
|
|
where consumerInterface.IsGenericType && consumerInterface.GetGenericTypeDefinition() == typeof(IConsumer<>)
|
|
|
|
|
let genericType = consumerInterface.GetGenericArguments()[0]
|
|
|
|
|
let topicName = $"{genericType.Namespace.ToLower()}/{genericType.Name.ToLower()}"
|
|
|
|
|
select new MessageSubscriptionTopology(topicName, consumer.Name ?? genericType.Name.ToLower(), consumer.IsTemporary)
|
|
|
|
|
).ToList();
|
2024-02-12 14:32:48 +00:00
|
|
|
|
|
|
|
|
Services.AddSingleton(new MessageTopologyProvider(subscriptionTopology));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Apply()
|
|
|
|
|
{
|
2024-02-13 13:35:39 +00:00
|
|
|
Services.Configure(AzureServiceBusOptions);
|
2024-02-12 14:32:48 +00:00
|
|
|
Services.AddScoped(ServiceBusAdministrationClientFactory);
|
2024-02-20 10:26:52 +00:00
|
|
|
Services.AddNotificationHandler<RemoveOrphanedSubscriptions>();
|
2024-02-12 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetConnectionString(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
var options = serviceProvider.GetRequiredService<IOptions<AzureServiceBusOptions>>().Value;
|
|
|
|
|
var configuration = serviceProvider.GetRequiredService<IConfiguration>();
|
|
|
|
|
return configuration.GetConnectionString(options.ConnectionStringOrName) ?? options.ConnectionStringOrName;
|
|
|
|
|
}
|
2023-11-15 19:47:11 +00:00
|
|
|
}
|