66using System . ComponentModel . Design ;
77using System . Globalization ;
88using System . Threading . Tasks ;
9+ using Autofac ;
10+ using Microsoft . Extensions . Configuration ;
11+ using Microsoft . Extensions . DependencyInjection ;
12+ using Microsoft . Extensions . Options ;
913using Microsoft . WindowsAzure . Storage ;
1014using Microsoft . WindowsAzure . Storage . Blob ;
1115using Microsoft . WindowsAzure . Storage . RetryPolicies ;
1216using NuGet . Jobs ;
13- using NuGet . Services . KeyVault ;
14- using NuGet . Services . Sql ;
17+ using NuGet . Jobs . Configuration ;
1518using Stats . AzureCdnLogs . Common ;
1619
1720namespace Stats . ImportAzureCdnStatistics
1821{
19- public class Job
20- : JobBase
22+ public class ImportAzureCdnStatisticsJob : JsonConfigurationJob
2123 {
22- private bool _aggregatesOnly ;
23- private string _azureCdnAccountNumber ;
24- private string _cloudStorageContainerName ;
24+ private ImportAzureCdnStatisticsConfiguration _configuration ;
2525 private AzureCdnPlatform _azureCdnPlatform ;
26- private ISqlConnectionFactory _statisticsDbConnectionFactory ;
27- private CloudStorageAccount _cloudStorageAccount ;
2826 private CloudBlobClient _cloudBlobClient ;
2927 private LogFileProvider _blobLeaseManager ;
3028
3129 public override void Init ( IServiceContainer serviceContainer , IDictionary < string , string > jobArgsDictionary )
3230 {
33- var secretInjector = ( ISecretInjector ) serviceContainer . GetService ( typeof ( ISecretInjector ) ) ;
34- var statisticsDbConnectionString = JobConfigurationManager . GetArgument ( jobArgsDictionary , JobArgumentNames . StatisticsDatabase ) ;
35- _statisticsDbConnectionFactory = new AzureSqlConnectionFactory ( statisticsDbConnectionString , secretInjector ) ;
31+ base . Init ( serviceContainer , jobArgsDictionary ) ;
3632
37- var azureCdnPlatform = JobConfigurationManager . GetArgument ( jobArgsDictionary , JobArgumentNames . AzureCdnPlatform ) ;
38- var cloudStorageAccountConnectionString = JobConfigurationManager . GetArgument ( jobArgsDictionary , JobArgumentNames . AzureCdnCloudStorageAccount ) ;
39- _cloudStorageAccount = ValidateAzureCloudStorageAccount ( cloudStorageAccountConnectionString ) ;
33+ _configuration = _serviceProvider . GetRequiredService < IOptionsSnapshot < ImportAzureCdnStatisticsConfiguration > > ( ) . Value ;
4034
41- _azureCdnAccountNumber = JobConfigurationManager . GetArgument ( jobArgsDictionary , JobArgumentNames . AzureCdnAccountNumber ) ;
42- _azureCdnPlatform = ValidateAzureCdnPlatform ( azureCdnPlatform ) ;
43- _cloudStorageContainerName = ValidateAzureContainerName ( JobConfigurationManager . GetArgument ( jobArgsDictionary , JobArgumentNames . AzureCdnCloudStorageContainerName ) ) ;
35+ _azureCdnPlatform = ValidateAzureCdnPlatform ( _configuration . AzureCdnPlatform ) ;
4436
45- _aggregatesOnly = JobConfigurationManager . TryGetBoolArgument ( jobArgsDictionary , JobArgumentNames . AggregatesOnly ) ;
46-
47- // construct a cloud blob client for the configured storage account
48- _cloudBlobClient = _cloudStorageAccount . CreateCloudBlobClient ( ) ;
37+ var cloudStorageAccount = ValidateAzureCloudStorageAccount ( _configuration . AzureCdnCloudStorageAccount ) ;
38+ _cloudBlobClient = cloudStorageAccount . CreateCloudBlobClient ( ) ;
4939 _cloudBlobClient . DefaultRequestOptions . RetryPolicy = new ExponentialRetry ( TimeSpan . FromSeconds ( 10 ) , 5 ) ;
5040
51- // Get the source blob container (containing compressed log files)
52- // and construct a log source (fetching raw logs from the source blob container)
53- var sourceBlobContainer = _cloudBlobClient . GetContainerReference ( _cloudStorageContainerName ) ;
54- _blobLeaseManager = new LogFileProvider ( sourceBlobContainer , LoggerFactory ) ;
41+ _blobLeaseManager = new LogFileProvider (
42+ _cloudBlobClient . GetContainerReference ( _configuration . AzureCdnCloudStorageContainerName ) ,
43+ LoggerFactory ) ;
5544 }
5645
5746 public override async Task Run ( )
5847 {
5948 // Get the target blob container (for archiving decompressed log files)
60- var targetBlobContainer = _cloudBlobClient . GetContainerReference ( _cloudStorageContainerName + "-archive" ) ;
49+ var targetBlobContainer = _cloudBlobClient . GetContainerReference (
50+ _configuration . AzureCdnCloudStorageContainerName + "-archive" ) ;
6151 await targetBlobContainer . CreateIfNotExistsAsync ( ) ;
6252
6353 // Get the dead-letter table (corrupted or failed blobs will end up there)
64- var deadLetterBlobContainer = _cloudBlobClient . GetContainerReference ( _cloudStorageContainerName + "-deadletter" ) ;
54+ var deadLetterBlobContainer = _cloudBlobClient . GetContainerReference (
55+ _configuration . AzureCdnCloudStorageContainerName + "-deadletter" ) ;
6556 await deadLetterBlobContainer . CreateIfNotExistsAsync ( ) ;
6657
6758 // Create a parser
68- var warehouse = new Warehouse ( LoggerFactory , _statisticsDbConnectionFactory ) ;
59+ var warehouse = new Warehouse ( LoggerFactory , OpenSqlConnectionAsync < StatisticsDbConfiguration > ) ;
6960 var statisticsBlobContainerUtility = new StatisticsBlobContainerUtility (
7061 targetBlobContainer ,
7162 deadLetterBlobContainer ,
@@ -74,11 +65,13 @@ public override async Task Run()
7465 var logProcessor = new LogFileProcessor ( statisticsBlobContainerUtility , LoggerFactory , warehouse ) ;
7566
7667 // Get the next to-be-processed raw log file using the cdn raw log file name prefix
77- var prefix = string . Format ( CultureInfo . InvariantCulture , "{0}_{1}_" , _azureCdnPlatform . GetRawLogFilePrefix ( ) , _azureCdnAccountNumber ) ;
68+ var prefix = string . Format ( CultureInfo . InvariantCulture , "{0}_{1}_" ,
69+ _azureCdnPlatform . GetRawLogFilePrefix ( ) ,
70+ _configuration . AzureCdnAccountNumber ) ;
7871
7972 // Get next raw log file to be processed
8073 IReadOnlyCollection < string > alreadyAggregatedLogFiles = null ;
81- if ( _aggregatesOnly )
74+ if ( _configuration . AggregatesOnly )
8275 {
8376 // We only want to process aggregates for the log files.
8477 // Get the list of files we already processed so we can skip them.
@@ -90,9 +83,9 @@ public override async Task Run()
9083 {
9184 var packageTranslator = new PackageTranslator ( "packagetranslations.json" ) ;
9285 var packageStatisticsParser = new PackageStatisticsParser ( packageTranslator , LoggerFactory ) ;
93- await logProcessor . ProcessLogFileAsync ( leasedLogFile , packageStatisticsParser , _aggregatesOnly ) ;
86+ await logProcessor . ProcessLogFileAsync ( leasedLogFile , packageStatisticsParser , _configuration . AggregatesOnly ) ;
9487
95- if ( _aggregatesOnly )
88+ if ( _configuration . AggregatesOnly )
9689 {
9790 _blobLeaseManager . TrackLastProcessedBlobUri ( leasedLogFile . Uri ) ;
9891 }
@@ -139,5 +132,14 @@ private static string ValidateAzureContainerName(string containerName)
139132 }
140133 return containerName ;
141134 }
135+
136+ protected override void ConfigureAutofacServices ( ContainerBuilder containerBuilder )
137+ {
138+ }
139+
140+ protected override void ConfigureJobServices ( IServiceCollection services , IConfigurationRoot configurationRoot )
141+ {
142+ ConfigureInitializationSection < ImportAzureCdnStatisticsConfiguration > ( services , configurationRoot ) ;
143+ }
142144 }
143145}
0 commit comments