Skip to content

Instantly share code, notes, and snippets.

@ealsur ealsur/Estimator.cs
Created Feb 15, 2019

Embed
What would you like to do?
Azure Cosmos DB + Functions Cookbook - monitoring - estimator
private static Lazy<IRemainingWorkEstimator> remainingWorkEstimator;
public static IRemainingWorkEstimator GetRemainingWorkEstimator(
DocumentClient monitoredCollectionClient,
DocumentClient leaseCollectionClient)
{
if (remainingWorkEstimator == null)
{
// Pull the Connection string from the environment, Environment.GetEnvironmentVariable will read the local.settings.json file or the deployed Function App configuration
CosmosDBConnectionString cosmosDBConnectionString = new CosmosDBConnectionString(Environment.GetEnvironmentVariable("CosmosDB"));
remainingWorkEstimator = new Lazy<IRemainingWorkEstimator>(() =>
{
var builder = new ChangeFeedProcessorBuilder()
.WithHostName("monitor") // Can be a random name
.WithProcessorOptions(new ChangeFeedProcessorOptions()
{
LeasePrefix = Environment.GetEnvironmentVariable("MonitoredDatabaseLeasePrefix")
})
.WithFeedCollection(new DocumentCollectionInfo()
{
Uri = cosmosDBConnectionString.ServiceEndpoint,
MasterKey = cosmosDBConnectionString.AuthKey,
CollectionName = Environment.GetEnvironmentVariable("MonitoredCollection"),
DatabaseName = Environment.GetEnvironmentVariable("MonitoredDatabase")
})
.WithLeaseCollection(new DocumentCollectionInfo()
{
Uri = cosmosDBConnectionString.ServiceEndpoint,
MasterKey = cosmosDBConnectionString.AuthKey,
CollectionName = "leases",
DatabaseName = Environment.GetEnvironmentVariable("MonitoredDatabase")
})
.WithFeedDocumentClient(monitoredCollectionClient)
.WithLeaseDocumentClient(leaseCollectionClient);
return builder.BuildEstimatorAsync().Result;
});
}
return remainingWorkEstimator.Value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.