Skip to content

Commit

Permalink
Create DefaultCosmosDBServiceFactory with AzureComponentFactory (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
chiangvincent committed Aug 29, 2023
1 parent 75919fd commit 5b7367e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Host.Scale;
using Microsoft.Extensions.Azure;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
Expand All @@ -31,11 +32,12 @@ public CosmosDbScalerProvider(IServiceProvider serviceProvider, TriggerMetadata
azureComponentFactory = serviceProvider.GetService<AzureComponentFactory>();
}

IConfiguration config = serviceProvider.GetService<IConfiguration>();
ILoggerFactory loggerFactory = serviceProvider.GetService<ILoggerFactory>();
CosmosDbMetadata cosmosDbMetadata = JsonConvert.DeserializeObject<CosmosDbMetadata>(triggerMetadata.Metadata.ToString());
cosmosDbMetadata.ResolveProperties(serviceProvider.GetService<INameResolver>());
IOptions<CosmosClientOptions> options = serviceProvider.GetService<IOptions<CosmosClientOptions>>();
ICosmosDBServiceFactory serviceFactory = serviceProvider.GetService<ICosmosDBServiceFactory>();
ICosmosDBServiceFactory serviceFactory = new DefaultCosmosDBServiceFactory(config, azureComponentFactory);
CosmosClient cosmosClient = serviceFactory.CreateService(cosmosDbMetadata.Connection, options.Value);
var monitoredContainer = cosmosClient.GetContainer(cosmosDbMetadata.DatabaseName, cosmosDbMetadata.ContainerName);
var leaseContainer = cosmosClient.GetContainer(string.IsNullOrEmpty(cosmosDbMetadata.LeaseDatabaseName) ? cosmosDbMetadata.DatabaseName : cosmosDbMetadata.LeaseDatabaseName, string.IsNullOrEmpty(cosmosDbMetadata.LeaseContainerName) ? CosmosDBTriggerConstants.DefaultLeaseCollectionName : cosmosDbMetadata.LeaseContainerName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static async Task<CosmosClient> InitializeDocumentClientAsync(IConfigurat
}
catch (CosmosException cosmosException) when (cosmosException.StatusCode == System.Net.HttpStatusCode.NotFound)
{
await database.CreateContainerAsync(CollectionName, "/id");
await database.CreateContainerAsync(collectionName, "/id");
}

return client;
Expand Down
27 changes: 26 additions & 1 deletion test/WebJobs.Extensions.CosmosDB.Tests/ScaleHostEndToEndTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Castle.Core.Configuration;
Expand All @@ -34,7 +35,7 @@ public class ScaleHostEndToEndTests
{
private const string FunctionName = "Function1";
private const string DatabaseName = "E2EDb";
private const string CollectionName = "E2ECollection";
private const string CollectionName = "E2EScaleCollection";
private const string Connection = "CosmosDbConnection";
private readonly TestLoggerProvider _loggerProvider = new TestLoggerProvider();

Expand Down Expand Up @@ -132,6 +133,8 @@ public async Task ScaleHostEndToEndTest(bool tbsEnabled)
PartitionKey pk = new PartitionKey(item.Id);
await container.UpsertItemAsync<Item>(item, pk);
}

await StartProcessor(client);
}

IHost scaleHost = hostBuilder.Build();
Expand Down Expand Up @@ -180,5 +183,27 @@ await Host.TestCommon.TestHelpers.Await(async () =>
return scaledOut;
}, pollingInterval: 2000, timeout: 180000, throwWhenDebugging: true);
}

private async Task StartProcessor(CosmosClient cosmosClient)
{
var leaseContainer = cosmosClient.GetContainer(DatabaseName, "leases");
var monitoredContainer = cosmosClient.GetContainer(DatabaseName, CollectionName);

var builder = monitoredContainer.GetChangeFeedProcessorBuilder<Item>(string.Empty, HandleChangesAsync)
.WithInstanceName("MyInstance")
.WithLeaseContainer(leaseContainer)
.WithStartTime(DateTime.UtcNow)
.WithPollInterval(TimeSpan.FromSeconds(10));

var processor = builder.Build();
await processor.StartAsync();
}

private static async Task HandleChangesAsync(
ChangeFeedProcessorContext context,
IReadOnlyCollection<Item> changes,
CancellationToken cancellationToken)
{
}
}
}

0 comments on commit 5b7367e

Please sign in to comment.