Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor cleanup service #39264

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}