Skip to content

Commit b6880c0

Browse files
authored
Auto create storage container (Azure#17607)
1 parent 141f578 commit b6880c0

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/src/BlobsCheckpointStore.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using Azure.Storage.Blobs;
68
using Microsoft.Extensions.Logging;
79

@@ -11,6 +13,7 @@ internal partial class BlobsCheckpointStore
1113
{
1214
private readonly string _functionId;
1315
private readonly ILogger _logger;
16+
private BlobContainerClient _client;
1417

1518
/// <summary>
1619
/// The mocking constructor.
@@ -35,6 +38,7 @@ public BlobsCheckpointStore(BlobContainerClient blobContainerClient,
3538
{
3639
_functionId = functionId;
3740
_logger = logger;
41+
_client = blobContainerClient;
3842
}
3943

4044
/// <summary>
@@ -68,5 +72,15 @@ partial void ListCheckpointsError(string fullyQualifiedNamespace, string eventHu
6872
"Function '{functionId}': An exception occurred when listing checkpoints for FullyQualifiedNamespace: '{fullyQualifiedNamespace}'; EventHubName: '{eventHubName}'; ConsumerGroup: '{consumerGroup}'.",
6973
_functionId, fullyQualifiedNamespace, eventHubName, consumerGroup);
7074
}
75+
76+
/// <summary>
77+
/// Attempts to create a storage container if one doesn't exists.
78+
/// </summary>
79+
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
80+
/// <returns>A task to be resolved on when the operation has completed.</returns>
81+
public async Task CreateIfNotExistsAsync(CancellationToken cancellationToken)
82+
{
83+
await _client.CreateIfNotExistsAsync(cancellationToken: cancellationToken).ConfigureAwait(false);
84+
}
7185
}
7286
}

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/src/Listeners/EventHubListener.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void IDisposable.Dispose()
7171

7272
public async Task StartAsync(CancellationToken cancellationToken)
7373
{
74+
await _checkpointStore.CreateIfNotExistsAsync(cancellationToken).ConfigureAwait(false);
7475
await _eventProcessorHost.StartProcessingAsync(this, _checkpointStore, cancellationToken).ConfigureAwait(false);
7576
}
7677

sdk/eventhub/Microsoft.Azure.WebJobs.Extensions.EventHubs/tests/WebJobsEventHubTestBase.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ public class WebJobsEventHubTestBase
2525
/// <summary>The active Event Hub resource scope for the test fixture.</summary>
2626
protected EventHubScope _eventHubScope;
2727

28-
/// <summary>The active Blob storage resource scope for the test fixture.</summary>
29-
protected StorageScope _storageScope;
30-
3128
/// <summary>
3229
/// Performs the tasks needed to initialize the test fixture. This
3330
/// method runs once for the entire fixture, prior to running any tests.
@@ -37,7 +34,6 @@ public class WebJobsEventHubTestBase
3734
public async Task FixtureSetUp()
3835
{
3936
_eventHubScope = await EventHubScope.CreateAsync(2);
40-
_storageScope = await StorageScope.CreateAsync();
4137
_testId = Guid.NewGuid().ToString();
4238
}
4339

@@ -49,11 +45,7 @@ public async Task FixtureSetUp()
4945
[TearDown]
5046
public async Task FixtureTearDown()
5147
{
52-
await Task.WhenAll
53-
(
54-
_eventHubScope.DisposeAsync().AsTask(),
55-
_storageScope.DisposeAsync().AsTask()
56-
);
48+
await _eventHubScope.DisposeAsync();
5749
}
5850

5951
protected void ConfigureTestEventHub(IHostBuilder builder)
@@ -85,7 +77,7 @@ protected void ConfigureTestEventHub(IHostBuilder builder)
8577
{
8678
services.Configure<EventHubOptions>(options =>
8779
{
88-
options.CheckpointContainer = _storageScope.ContainerName;
80+
options.CheckpointContainer = Guid.NewGuid().ToString("D").Substring(0, 13);
8981
// Speedup shutdown
9082
options.EventProcessorOptions.MaximumWaitTime = TimeSpan.FromSeconds(5);
9183
});

0 commit comments

Comments
 (0)