public static async Task Run( [TimerTrigger("0 0 0/1 * * *")] TimerInfo timerInfo, [CosmosDB( databaseName: Webhook.DatabaseName, collectionName: Webhook.CollectionName, ConnectionStringSetting = "CosmosDBConnectionString")] DocumentClient documentClient, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var dimensionConfigs = JsonConvert.DeserializeObject <DimensionConfig[]>(config["DIMENSION_CONFIG"]); log.LogTrace($"Dimension configs: {JsonConvert.SerializeObject(dimensionConfigs)}"); using (var httpClient = HttpClientFactory.Create()) { var token = await CronJob.GetToken(httpClient, config, log).ConfigureAwait(continueOnCapturedContext: false); httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); string sqlExpression = "Select * from c where c.processStatus=false"; using (var queryable = documentClient.CreateDocumentQuery <BillingEntry>(UriFactory.CreateDocumentCollectionUri(Webhook.DatabaseName, Webhook.CollectionName), sqlExpression).AsDocumentQuery()) { while (queryable.HasMoreResults) { foreach (var billingEntry in await queryable.ExecuteNextAsync <BillingEntry>().ConfigureAwait(continueOnCapturedContext: false)) { foreach (var dimensionConfig in dimensionConfigs) { var response = await CronJob.EmitUsageEvents(config, httpClient, dimensionConfig, billingEntry).ConfigureAwait(continueOnCapturedContext: false); var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); if (response.IsSuccessStatusCode) { log.LogTrace($"Successfully emitted a usage event. Reponse body: {responseBody}"); // update cosmosdb document billingEntry.processStatus = true; await documentClient.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(Webhook.DatabaseName, Webhook.CollectionName), billingEntry).ConfigureAwait(continueOnCapturedContext: false); } else { log.LogError($"Failed to emit a usage event. Error code: {response.StatusCode}. Failure cause: {response.ReasonPhrase}. Response body: {responseBody}"); } } } } } } }
public static async Task Run( [TimerTrigger("0 0 0/1 * * *")] TimerInfo timerInfo, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var dimensionConfigs = JsonConvert.DeserializeObject <DimensionConfig[]>(config["DIMENSION_CONFIG"]); log.LogTrace($"Dimension configs: {JsonConvert.SerializeObject(dimensionConfigs)}"); using (var armHttpClient = HttpClientFactory.Create()) { var armToken = await CronJob.GetToken(config, armHttpClient, log, "https://management.core.windows.net/").ConfigureAwait(continueOnCapturedContext: false); armHttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {armToken}"); var applicationResourceId = await CronJob.GetResourceGroupManagedBy(config, armHttpClient, log).ConfigureAwait(continueOnCapturedContext: false); var application = await CronJob.GetApplication(applicationResourceId, config, armHttpClient, log).ConfigureAwait(continueOnCapturedContext: false); if (application != null) { log.LogInformation($"Authorization bearer token: {armToken}"); log.LogInformation($"Resource usage id: {application.Properties.BillingDetails?.ResourceUsageId}"); log.LogInformation($"Plan name: {application.Plan.Name}"); foreach (var dimensionConfig in dimensionConfigs) { var response = await CronJob.EmitUsageEvents(config, armHttpClient, dimensionConfig, application.Properties.BillingDetails?.ResourceUsageId, application.Plan.Name).ConfigureAwait(continueOnCapturedContext: false); var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(continueOnCapturedContext: false); if (response.IsSuccessStatusCode) { log.LogTrace($"Successfully emitted a usage event. Reponse body: {responseBody}"); } else { log.LogError($"Failed to emit a usage event. Error code: {response.StatusCode}. Failure cause: {response.ReasonPhrase}. Response body: {responseBody}"); } } } } }