From 12b0133a4e8d63bac361209bf401b70b672de1ae Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 17 Oct 2023 10:53:40 -0700 Subject: [PATCH 1/3] Use singleton rather than hosted service for cleanup --- .../ServiceBusExtensionConfigProvider.cs | 3 +- .../Config/ServiceBusHostBuilderExtensions.cs | 2 +- .../Primitives/CachedClientCleanupService.cs | 46 ------------------- .../src/Primitives/CleanupService.cs | 25 ++++++++++ .../src/Primitives/MessagingProvider.cs | 25 ++++++++++ .../tests/ServiceBusEndToEndTests.cs | 6 +++ .../tests/WebJobsServiceBusTestBase.cs | 6 --- 7 files changed, 59 insertions(+), 54 deletions(-) delete mode 100644 sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CachedClientCleanupService.cs create mode 100644 sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CleanupService.cs diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusExtensionConfigProvider.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusExtensionConfigProvider.cs index e1a66314b88c4..aae0944a48b03 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusExtensionConfigProvider.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusExtensionConfigProvider.cs @@ -48,7 +48,8 @@ public ServiceBusExtensionConfigProvider( IConverterManager converterManager, ServiceBusClientFactory clientFactory, ConcurrencyManager concurrencyManager, - IDrainModeManager drainModeManager) + IDrainModeManager drainModeManager, + CleanupService cleanupService) { _options = options.Value; _messagingProvider = messagingProvider; diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusHostBuilderExtensions.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusHostBuilderExtensions.cs index 7131723924936..5c67e2dc787ad 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusHostBuilderExtensions.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Config/ServiceBusHostBuilderExtensions.cs @@ -110,7 +110,7 @@ public static IWebJobsBuilder AddServiceBus(this IWebJobsBuilder builder, Action builder.Services.AddAzureClientsCore(); builder.Services.TryAddSingleton(); - builder.Services.AddHostedService(); + builder.Services.AddSingleton(); builder.Services.AddSingleton(); #if NET6_0_OR_GREATER builder.Services.AddSingleton(); diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CachedClientCleanupService.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CachedClientCleanupService.cs deleted file mode 100644 index 635d2e390e7bb..0000000000000 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CachedClientCleanupService.cs +++ /dev/null @@ -1,46 +0,0 @@ -// 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 CachedClientCleanupService : IHostedService - { - private readonly MessagingProvider _provider; - - public CachedClientCleanupService(MessagingProvider provider) - { - _provider = provider; - } - - public Task StartAsync(CancellationToken cancellationToken) - { - return Task.CompletedTask; - } - - public async Task StopAsync(CancellationToken cancellationToken) - { - foreach (var receiver in _provider.MessageReceiverCache.Values) - { - await receiver.DisposeAsync().ConfigureAwait(false); - } - _provider.MessageReceiverCache.Clear(); - - foreach (var sender in _provider.MessageSenderCache.Values) - { - await sender.DisposeAsync().ConfigureAwait(false); - } - _provider.MessageSenderCache.Clear(); - - foreach (var client in _provider.ClientCache.Values) - { - await client.DisposeAsync().ConfigureAwait(false); - } - _provider.ClientCache.Clear(); - _provider.ActionsCache.Clear(); - } - } -} \ No newline at end of file diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CleanupService.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CleanupService.cs new file mode 100644 index 0000000000000..a868754d2ceee --- /dev/null +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/CleanupService.cs @@ -0,0 +1,25 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.Extensions.Hosting; + +namespace Microsoft.Azure.WebJobs.ServiceBus +{ + internal class CleanupService : IAsyncDisposable + { + private readonly MessagingProvider _provider; + + public CleanupService(MessagingProvider provider) + { + _provider = provider; + } + + public async ValueTask DisposeAsync() + { + await _provider.DisposeAsync().ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/MessagingProvider.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/MessagingProvider.cs index 84af7e4585741..c3ecd8fe12d15 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/MessagingProvider.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Primitives/MessagingProvider.cs @@ -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; @@ -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(); + } } } diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/ServiceBusEndToEndTests.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/ServiceBusEndToEndTests.cs index 2049f7b2897ad..11c02a266ead1 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/ServiceBusEndToEndTests.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/ServiceBusEndToEndTests.cs @@ -619,6 +619,8 @@ public async Task TestBatch_DataContractPoco() public async Task BindToPoco() { var host = BuildHost(); + var provider = host.Services.GetService(); + using (host) { await WriteQueueMessage("{ Name: 'foo', Value: 'bar' }"); @@ -630,6 +632,10 @@ public async Task BindToPoco() Assert.Contains("PocoValues(foo,bar)", logs); await host.StopAsync(); } + Assert.AreEqual(0, provider.ClientCache.Count); + Assert.AreEqual(0, provider.MessageReceiverCache.Count); + Assert.AreEqual(0, provider.MessageSenderCache.Count); + Assert.AreEqual(0, provider.ActionsCache.Count); } [Test] diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs index 5353e6ef5cd1a..9d5775e618faa 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/tests/WebJobsServiceBusTestBase.cs @@ -336,12 +336,6 @@ public async Task StopAsync(CancellationToken cancellationToken) QueueRuntimeProperties properties = await client.GetQueueRuntimePropertiesAsync(FirstQueueScope.QueueName, CancellationToken.None); Assert.AreEqual(ExpectedRemainingMessages, properties.ActiveMessageCount); - - var provider = _host.Services.GetService(); - Assert.AreEqual(0, provider.ClientCache.Count); - Assert.AreEqual(0, provider.MessageReceiverCache.Count); - Assert.AreEqual(0, provider.MessageSenderCache.Count); - Assert.AreEqual(0, provider.ActionsCache.Count); } private static bool IsError(LogMessage logMessage) From 0b66a0f993f439bd30e6007559683bdf8e36957f Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 17 Oct 2023 11:31:43 -0700 Subject: [PATCH 2/3] prepare for release --- .../CHANGELOG.md | 11 ++++++----- ...crosoft.Azure.WebJobs.Extensions.ServiceBus.csproj | 2 +- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md index 29a120c5f6947..315f21f73ca34 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md @@ -1,15 +1,16 @@ # Release History -## 5.14.0-beta.1 (Unreleased) - -### Features Added - -### Breaking Changes +## 5.13.1 ### Bugs Fixed +- Fixed the disposal pattern for cached Service Bus clients so that they are disposed only on + host shutdown. + ### Other Changes +- Updated the proto service definition to use bytes for application properties. + ## 5.13.0 (2023-10-11) ### Features Added diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj index ba0898e39f32a..2d26bfcb81c15 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/src/Microsoft.Azure.WebJobs.Extensions.ServiceBus.csproj @@ -3,7 +3,7 @@ netstandard2.0;net6.0 Microsoft Azure WebJobs SDK ServiceBus Extension - 5.14.0-beta.1 + 5.13.1 5.13.0 From 6814a0904e68a08359aa46004cd849a18cd696c4 Mon Sep 17 00:00:00 2001 From: JoshLove-msft <54595583+JoshLove-msft@users.noreply.github.com> Date: Tue, 17 Oct 2023 12:10:09 -0700 Subject: [PATCH 3/3] add date --- .../Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md index 315f21f73ca34..6c6d496a1a92c 100644 --- a/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md +++ b/sdk/servicebus/Microsoft.Azure.WebJobs.Extensions.ServiceBus/CHANGELOG.md @@ -1,6 +1,6 @@ # Release History -## 5.13.1 +## 5.13.1 (2023-10-17) ### Bugs Fixed