Skip to content

Commit

Permalink
Refactor cleanup (Azure#39264)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshLove-msft authored and matthohn-msft committed Oct 27, 2023
1 parent d103748 commit b44c3c7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static IWebJobsBuilder AddServiceBus(this IWebJobsBuilder builder, Action

builder.Services.AddAzureClientsCore();
builder.Services.TryAddSingleton<MessagingProvider>();
builder.Services.AddHostedService<CachedClientCleanupService>();
builder.Services.AddHostedService<CleanupService>();
builder.Services.AddSingleton<ServiceBusClientFactory>();
#if NET6_0_OR_GREATER
builder.Services.AddSingleton<SettlementService>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace Microsoft.Azure.WebJobs.ServiceBus
{
internal class CleanupService : IHostedService
{
private readonly MessagingProvider _provider;

public CleanupService(MessagingProvider provider)
{
_provider = provider;
}

public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}

public async Task StopAsync(CancellationToken cancellationToken)
{
await _provider.DisposeAsync().ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Messaging.ServiceBus;
using Microsoft.Azure.WebJobs.ServiceBus.Listeners;
Expand Down Expand Up @@ -218,5 +219,29 @@ private static string GenerateCacheKey(string fullyQualifiedNamespace, string en
{
return $"{fullyQualifiedNamespace}/{entityPath}";
}

// This class does not implement IAsyncDisposable as doing so could break existing user code. We can consider making this break
// on the next major version upgrade.
internal async Task DisposeAsync()
{
foreach (var receiver in MessageReceiverCache.Values)
{
await receiver.DisposeAsync().ConfigureAwait(false);
}
MessageReceiverCache.Clear();

foreach (var sender in MessageSenderCache.Values)
{
await sender.DisposeAsync().ConfigureAwait(false);
}
MessageSenderCache.Clear();

foreach (var client in ClientCache.Values)
{
await client.DisposeAsync().ConfigureAwait(false);
}
ClientCache.Clear();
ActionsCache.Clear();
}
}
}

0 comments on commit b44c3c7

Please sign in to comment.