2024-03-27 11:07:55 +00:00
|
|
|
using Azure.Messaging.ServiceBus;
|
2024-02-12 14:32:48 +00:00
|
|
|
using Azure.Messaging.ServiceBus.Administration;
|
|
|
|
|
using Elsa.Hosting.Management.Notifications;
|
|
|
|
|
using Elsa.MassTransit.AzureServiceBus.Services;
|
|
|
|
|
using Elsa.Mediator.Contracts;
|
2024-02-20 10:26:52 +00:00
|
|
|
using JetBrains.Annotations;
|
2024-03-27 11:07:55 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2024-02-12 14:32:48 +00:00
|
|
|
|
|
|
|
|
namespace Elsa.MassTransit.AzureServiceBus.Handlers;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-06-20 20:48:03 +00:00
|
|
|
/// A handler for the <see cref="HeartbeatTimedOut"/> notification that removes subscriptions from Azure Service Bus
|
|
|
|
|
/// when there has not been any heartbeat received lately for this instance.
|
2024-02-12 14:32:48 +00:00
|
|
|
/// </summary>
|
2024-02-20 10:26:52 +00:00
|
|
|
[UsedImplicitly]
|
2024-03-27 11:07:55 +00:00
|
|
|
public class RemoveOrphanedSubscriptions(MessageTopologyProvider topologyProvider,
|
|
|
|
|
ServiceBusAdministrationClient client,
|
|
|
|
|
ILogger<RemoveOrphanedSubscriptions> logger)
|
|
|
|
|
: INotificationHandler<HeartbeatTimedOut>
|
2024-02-12 14:32:48 +00:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Removes orphaned subscriptions from Azure Service Bus.
|
|
|
|
|
/// </summary>
|
2024-02-16 13:00:28 +00:00
|
|
|
public async Task HandleAsync(HeartbeatTimedOut notification, CancellationToken cancellationToken)
|
2024-02-12 14:32:48 +00:00
|
|
|
{
|
2024-02-16 13:00:28 +00:00
|
|
|
var subscriptionTopology = topologyProvider.GetTemporarySubscriptions().ToList();
|
2024-02-13 13:35:39 +00:00
|
|
|
|
|
|
|
|
foreach (var subscription in subscriptionTopology)
|
2024-02-12 14:32:48 +00:00
|
|
|
{
|
2024-03-27 11:07:55 +00:00
|
|
|
try
|
2024-02-13 13:35:39 +00:00
|
|
|
{
|
2024-06-20 20:48:03 +00:00
|
|
|
// Get subscriptions based on topics instead of the topology since when names are longer than 50 characters
|
2024-03-27 11:07:55 +00:00
|
|
|
// MassTransit automatically truncates them.
|
|
|
|
|
await foreach (var asbSubscription in client.GetSubscriptionsAsync(subscription.TopicName, cancellationToken))
|
2024-02-13 13:35:39 +00:00
|
|
|
{
|
2024-06-25 07:40:53 +00:00
|
|
|
if (asbSubscription.SubscriptionName.StartsWith($"{notification.InstanceName}-elsa-"))
|
2024-03-27 11:07:55 +00:00
|
|
|
{
|
|
|
|
|
var queueName = asbSubscription.ForwardTo[(asbSubscription.ForwardTo.LastIndexOf('/') + 1)..];
|
2024-02-13 13:35:39 +00:00
|
|
|
|
2024-03-27 11:07:55 +00:00
|
|
|
await Task.WhenAll(
|
|
|
|
|
client.DeleteSubscriptionAsync(subscription.TopicName, asbSubscription.SubscriptionName, cancellationToken),
|
|
|
|
|
client.DeleteQueueAsync(queueName, cancellationToken));
|
|
|
|
|
}
|
2024-02-13 13:35:39 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-25 07:40:53 +00:00
|
|
|
catch (ServiceBusException ex) when (ex.Reason == ServiceBusFailureReason.MessagingEntityNotFound)
|
2024-03-27 11:07:55 +00:00
|
|
|
{
|
2024-06-20 20:48:03 +00:00
|
|
|
logger.LogWarning(ex, "Service bus entity {entityPath} was not found", ex.EntityPath);
|
2024-03-27 11:07:55 +00:00
|
|
|
}
|
2024-02-12 14:32:48 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|