|
| 1 | +// Copyright (c) .NET Foundation. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.ComponentModel.Design; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Autofac; |
| 10 | +using Azure.Core; |
| 11 | +using Azure.Storage.Blobs; |
| 12 | +using Microsoft.Extensions.Configuration; |
| 13 | +using Microsoft.Extensions.DependencyInjection; |
| 14 | +using Microsoft.Extensions.Logging; |
| 15 | +using Microsoft.Extensions.Options; |
| 16 | +using NuGet.Jobs; |
| 17 | +using NuGet.Jobs.Configuration; |
| 18 | + |
| 19 | +namespace Stats.CreateAzureCdnWarehouseReports |
| 20 | +{ |
| 21 | + public class CreateAzureCdnWarehouseReportsJob : JsonConfigurationJob |
| 22 | + { |
| 23 | + private const int DefaultSqlCommandTimeoutSeconds = 1800; // 30 minute SQL command timeout by default |
| 24 | + |
| 25 | + private BlobServiceClient _cloudStorageAccount; |
| 26 | + private BlobServiceClient _additionalGalleryTotalsAccount; |
| 27 | + private string _statisticsContainerName; |
| 28 | + private string _additionalGalleryTotalsContainerName; |
| 29 | + private int _sqlCommandTimeoutSeconds = DefaultSqlCommandTimeoutSeconds; |
| 30 | + private ApplicationInsightsHelper _applicationInsightsHelper; |
| 31 | + |
| 32 | + public override void Init(IServiceContainer serviceContainer, IDictionary<string, string> jobArgsDictionary) |
| 33 | + { |
| 34 | + base.Init(serviceContainer, jobArgsDictionary); |
| 35 | + |
| 36 | + var configuration = _serviceProvider.GetRequiredService<IOptionsSnapshot<CreateAzureCdnWarehouseReportsConfiguration>>().Value; |
| 37 | + |
| 38 | + _sqlCommandTimeoutSeconds = configuration.CommandTimeOut ?? DefaultSqlCommandTimeoutSeconds; |
| 39 | + |
| 40 | + _cloudStorageAccount = ValidateAzureCloudStorageAccount( |
| 41 | + configuration.AzureCdnCloudStorageAccount, |
| 42 | + nameof(configuration.AzureCdnCloudStorageAccount), |
| 43 | + _serviceProvider); |
| 44 | + |
| 45 | + _statisticsContainerName = ValidateAzureContainerName( |
| 46 | + configuration.AzureCdnCloudStorageContainerName, |
| 47 | + nameof(configuration.AzureCdnCloudStorageContainerName)); |
| 48 | + |
| 49 | + if (!string.IsNullOrWhiteSpace(configuration.AdditionalGalleryTotalsStorageAccount)) |
| 50 | + { |
| 51 | + _additionalGalleryTotalsAccount = ValidateAzureCloudStorageAccount( |
| 52 | + configuration.AdditionalGalleryTotalsStorageAccount, |
| 53 | + nameof(configuration.AdditionalGalleryTotalsStorageAccount), |
| 54 | + _serviceProvider); |
| 55 | + Logger.LogInformation("Additional totals account found {BlobEndpoint}", _additionalGalleryTotalsAccount.Uri.GetLeftPart(UriPartial.Path)); |
| 56 | + |
| 57 | + _additionalGalleryTotalsContainerName = configuration.AdditionalGalleryTotalsStorageContainerName; |
| 58 | + } |
| 59 | + |
| 60 | + _applicationInsightsHelper = new ApplicationInsightsHelper(ApplicationInsightsConfiguration.TelemetryConfiguration); |
| 61 | + } |
| 62 | + |
| 63 | + public override async Task Run() |
| 64 | + { |
| 65 | + var reportGenerationTime = DateTime.UtcNow; |
| 66 | + var destinationContainer = _cloudStorageAccount.GetBlobContainerClient(_statisticsContainerName); |
| 67 | + |
| 68 | + Logger.LogInformation("Generating reports and saving to {AccountName}/{Container}", |
| 69 | + _cloudStorageAccount.AccountName, destinationContainer.Name); |
| 70 | + |
| 71 | + // build stats-totals.json |
| 72 | + var stopwatch = Stopwatch.StartNew(); |
| 73 | + |
| 74 | + var targets = new List<StorageContainerTarget> |
| 75 | + { |
| 76 | + new(_cloudStorageAccount, _statisticsContainerName) |
| 77 | + }; |
| 78 | + if (_additionalGalleryTotalsAccount != null && !string.IsNullOrWhiteSpace(_additionalGalleryTotalsContainerName)) |
| 79 | + { |
| 80 | + targets.Add(new StorageContainerTarget(_additionalGalleryTotalsAccount, _additionalGalleryTotalsContainerName)); |
| 81 | + Logger.LogInformation("Added additional target for stats totals report {BlobEndpoint}/{Container}", |
| 82 | + _additionalGalleryTotalsAccount.Uri.GetLeftPart(UriPartial.Path), |
| 83 | + _additionalGalleryTotalsContainerName); |
| 84 | + } |
| 85 | + var galleryTotalsReport = new GalleryTotalsReport( |
| 86 | + LoggerFactory.CreateLogger<GalleryTotalsReport>(), |
| 87 | + targets, |
| 88 | + OpenSqlConnectionAsync<GalleryDbConfiguration>, |
| 89 | + commandTimeoutSeconds: _sqlCommandTimeoutSeconds); |
| 90 | + await galleryTotalsReport.Run(); |
| 91 | + |
| 92 | + stopwatch.Stop(); |
| 93 | + var reportMetricName = ReportNames.GalleryTotals + ReportNames.Extension; |
| 94 | + _applicationInsightsHelper.TrackMetric(reportMetricName + " Generation Time (ms)", stopwatch.ElapsedMilliseconds); |
| 95 | + _applicationInsightsHelper.TrackReportProcessed(reportMetricName); |
| 96 | + } |
| 97 | + |
| 98 | + private static BlobServiceClient ValidateAzureCloudStorageAccount( |
| 99 | + string cloudStorageAccount, |
| 100 | + string configurationName, |
| 101 | + IServiceProvider serviceProvider) |
| 102 | + { |
| 103 | + if (string.IsNullOrEmpty(cloudStorageAccount)) |
| 104 | + { |
| 105 | + throw new ArgumentException($"Job configuration {configurationName} is not defined."); |
| 106 | + } |
| 107 | + |
| 108 | + var storageMsiConfiguration = serviceProvider.GetRequiredService<IOptions<StorageMsiConfiguration>>().Value; |
| 109 | + |
| 110 | + var blobClientOptions = new BlobClientOptions() |
| 111 | + { |
| 112 | + Retry = |
| 113 | + { |
| 114 | + Delay = TimeSpan.FromSeconds(10), |
| 115 | + MaxRetries = 5, |
| 116 | + Mode = RetryMode.Exponential, |
| 117 | + NetworkTimeout = TimeSpan.FromSeconds(30) |
| 118 | + } |
| 119 | + }; |
| 120 | + |
| 121 | + if (storageMsiConfiguration is null || !storageMsiConfiguration.UseManagedIdentity) |
| 122 | + { |
| 123 | + // using connection string |
| 124 | + return new BlobServiceClient(cloudStorageAccount, blobClientOptions); |
| 125 | + } |
| 126 | + |
| 127 | + // using token credential with blob endpoint |
| 128 | + var tempClient = new BlobServiceClient(cloudStorageAccount); |
| 129 | + var blobEndpoint = new Uri(tempClient.Uri.GetLeftPart(UriPartial.Path)); |
| 130 | + |
| 131 | + var tokenCredential = serviceProvider.GetRequiredService<TokenCredential>(); |
| 132 | + return new BlobServiceClient(blobEndpoint, tokenCredential, blobClientOptions); |
| 133 | + } |
| 134 | + |
| 135 | + private static string ValidateAzureContainerName(string containerName, string configurationName) |
| 136 | + { |
| 137 | + if (string.IsNullOrWhiteSpace(containerName)) |
| 138 | + { |
| 139 | + throw new ArgumentException($"Job configuration {configurationName} is not defined."); |
| 140 | + } |
| 141 | + |
| 142 | + return containerName; |
| 143 | + } |
| 144 | + |
| 145 | + protected override void ConfigureAutofacServices(ContainerBuilder containerBuilder, IConfigurationRoot configurationRoot) |
| 146 | + { |
| 147 | + } |
| 148 | + |
| 149 | + protected override void ConfigureJobServices(IServiceCollection services, IConfigurationRoot configurationRoot) |
| 150 | + { |
| 151 | + ConfigureInitializationSection<CreateAzureCdnWarehouseReportsConfiguration>(services, configurationRoot); |
| 152 | + services.ConfigureStorageMsi(configurationRoot); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments