Skip to content
This repository has been archived by the owner on Jul 19, 2024. It is now read-only.

Commit

Permalink
Stop Listener when disposed (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrod authored Feb 11, 2022
1 parent f4b1757 commit 3a6f3ee
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
8 changes: 6 additions & 2 deletions release_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
#### Version 4.2.0
- User configurable initial offset support [#79](https://github.com/Azure/azure-functions-eventhubs-extension/pull/79)

**Release sprint:** Sprint 87
[ [bugs](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+87%22+label%3Abug+is%3Aclosed) | [features](https://github.com/Azure/azure-functions-host/issues?q=is%3Aissue+milestone%3A%22Functions+Sprint+87%22+label%3Afeature+is%3Aclosed) ]
#### Version 4.3.0
- Adding explicit reference Microsoft.Azure.Amqp 2.4.11 [#99](https://github.com/Azure/azure-functions-eventhubs-extension/pull/99)

#### Version 4.3.1
- Stop Listener when disposed [#105](https://github.com/Azure/azure-functions-eventhubs-extension/pull/105)
- Add listener details [#105](https://github.com/Azure/azure-functions-eventhubs-extension/pull/105)
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ internal sealed class EventHubListener : IListener, IEventProcessorFactory, ISca
private readonly bool _singleDispatch;
private readonly EventHubOptions _options;
private readonly ILogger _logger;
private readonly SemaphoreSlim _stopSemaphoreSlim = new SemaphoreSlim(1, 1);
private readonly string _details;
private bool _started;

private Lazy<EventHubsScaleMonitor> _scaleMonitor;
Expand Down Expand Up @@ -62,6 +64,8 @@ public EventHubListener(
_options = options;
_logger = logger;
_scaleMonitor = new Lazy<EventHubsScaleMonitor>(() => new EventHubsScaleMonitor(_functionId, _eventHubName, _consumerGroup, _connectionString, _storageConnectionString, _logger, blobContainer));
_details = $"'namespace='{eventProcessorHost?.EndpointAddress}', eventHub='{eventProcessorHost?.EventHubPath}', " +
$"consumerGroup='{eventProcessorHost?.ConsumerGroupName}', functionId='{functionId}', singleDispatch='{singleDispatch}'";
}

void IListener.Cancel()
Expand All @@ -71,21 +75,37 @@ void IListener.Cancel()

void IDisposable.Dispose()
{
StopAsync(CancellationToken.None).Wait();
}

public async Task StartAsync(CancellationToken cancellationToken)
{
await _eventProcessorHost.RegisterEventProcessorFactoryAsync(this, _options.EventProcessorOptions);
_started = true;

_logger.LogDebug($"EventHub listener started ({_details})");
}

public async Task StopAsync(CancellationToken cancellationToken)
{
if (_started)
await _stopSemaphoreSlim.WaitAsync();
try
{
if (_started)
{
await _eventProcessorHost.UnregisterEventProcessorAsync();
_logger.LogDebug($"EventHub listener stopped ({_details})");
}
else
{
_logger.LogDebug($"EventHub listener is already stopped ({_details})");
}
_started = false;
}
finally
{
await _eventProcessorHost.UnregisterEventProcessorAsync();
_stopSemaphoreSlim.Release();
}
_started = false;
}

IEventProcessor IEventProcessorFactory.CreateEventProcessor(PartitionContext context)
Expand Down Expand Up @@ -132,7 +152,7 @@ public Task CloseAsync(PartitionContext context, CloseReason reason)
{
// signal cancellation for any in progress executions
_cts.Cancel();

_logger.LogDebug(GetOperationDetails(context, $"CloseAsync, {reason.ToString()}"));
return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<RootNamespace>Microsoft.Azure.WebJobs.EventHubs</RootNamespace>
<PackageId>Microsoft.Azure.WebJobs.Extensions.EventHubs</PackageId>
<Description>Microsoft Azure WebJobs SDK EventHubs Extension</Description>
<Version>4.3.0</Version>
<Version>4.3.1</Version>
<CommitHash Condition="$(CommitHash) == ''">N/A</CommitHash>
<InformationalVersion>$(Version) Commit hash: $(CommitHash)</InformationalVersion>
<Authors>Microsoft</Authors>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Azure.EventHubs.Processor;
using Microsoft.Azure.WebJobs.EventHubs.Listeners;
using Microsoft.Azure.WebJobs.Host.Executors;
using Microsoft.Azure.WebJobs.Host.Listeners;
using Microsoft.Azure.WebJobs.Host.Scale;
using Microsoft.Azure.WebJobs.Host.TestCommon;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -198,7 +199,6 @@ public void GetMonitor_ReturnsExpectedValue()
var functionId = "FunctionId";
var eventHubName = "EventHubName";
var consumerGroup = "ConsumerGroup";
var storageUri = new Uri("https://eventhubsteststorageaccount.blob.core.windows.net/");
var testLogger = new TestLogger("Test");
var listener = new EventHubListener(
functionId,
Expand All @@ -222,5 +222,30 @@ public void GetMonitor_ReturnsExpectedValue()

Assert.Same(scaleMonitor, scaleMonitor2);
}

[Fact]
public void Dispose_Calls_StopAsync()
{
string functionId = "FunctionId";
string eventHubName = "EventHubName";
string consumerGroup = "ConsumerGroup";
var storageUri = new Uri("https://eventhubsteststorageaccount.blob.core.windows.net/");
var testLogger = new TestLogger("Test");
EventHubListener listener = new EventHubListener(
functionId,
eventHubName,
consumerGroup,
"Endpoint=sb://test.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=abc123=",
"DefaultEndpointsProtocol=https;AccountName=EventHubScaleMonitorFakeTestAccount;AccountKey=ABCDEFG;EndpointSuffix=core.windows.net",
new Mock<ITriggeredFunctionExecutor>(MockBehavior.Strict).Object,
null,
false,
new EventHubOptions(),
testLogger,
new Mock<CloudBlobContainer>(MockBehavior.Strict, new Uri("https://eventhubsteststorageaccount.blob.core.windows.net/azure-webjobs-eventhub")).Object);

(listener as IDisposable).Dispose();
Assert.Single(testLogger.GetLogMessages().Where(x => x.FormattedMessage.StartsWith("EventHub listener is already stopped (")));
}
}
}

0 comments on commit 3a6f3ee

Please sign in to comment.