Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Commit 4837967

Browse files
authored
Merge pull request #491 from NuGet/dev
[ReleasePrep][2018.07.25]RI of dev into master
2 parents 9d423ad + 5b766ba commit 4837967

82 files changed

Lines changed: 1472 additions & 502 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/ArchivePackages/ArchivePackages.Job.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Design;
7+
using System.Data.SqlClient;
78
using System.Diagnostics.Tracing;
89
using System.Linq;
910
using System.Threading.Tasks;
@@ -12,8 +13,6 @@
1213
using Microsoft.WindowsAzure.Storage.Blob;
1314
using Newtonsoft.Json.Linq;
1415
using NuGet.Jobs;
15-
using NuGet.Services.KeyVault;
16-
using NuGet.Services.Sql;
1716

1817
namespace ArchivePackages
1918
{
@@ -53,22 +52,20 @@ public class Job : JobBase
5352
/// Blob containing the cursor data. Cursor data comprises of cursorDateTime
5453
/// </summary>
5554
public string CursorBlobName { get; set; }
56-
57-
private ISqlConnectionFactory _packageDbConnectionFactory;
5855

5956
protected CloudBlobContainer SourceContainer { get; private set; }
6057

6158
protected CloudBlobContainer PrimaryDestinationContainer { get; private set; }
6259

6360
protected CloudBlobContainer SecondaryDestinationContainer { get; private set; }
6461

62+
private SqlConnectionStringBuilder PackageDatabase { get; set; }
63+
6564
public Job() : base(JobEventSource.Log) { }
6665

6766
public override void Init(IServiceContainer serviceContainer, IDictionary<string, string> jobArgsDictionary)
6867
{
69-
var secretInjector = (ISecretInjector)serviceContainer.GetService(typeof(ISecretInjector));
70-
var packageDbConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.PackageDatabase);
71-
_packageDbConnectionFactory = new AzureSqlConnectionFactory(packageDbConnectionString, secretInjector);
68+
PackageDatabase = RegisterDatabase(serviceContainer, jobArgsDictionary, JobArgumentNames.PackageDatabase);
7269

7370
Source = CloudStorageAccount.Parse(
7471
JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.Source));
@@ -92,13 +89,18 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
9289

9390
public override async Task Run()
9491
{
95-
JobEventSourceLog.PreparingToArchive(Source.Credentials.AccountName, SourceContainer.Name, PrimaryDestination.Credentials.AccountName, PrimaryDestinationContainer.Name, _packageDbConnectionFactory.DataSource, _packageDbConnectionFactory.InitialCatalog);
92+
JobEventSourceLog.PreparingToArchive(
93+
Source.Credentials.AccountName, SourceContainer.Name,
94+
PrimaryDestination.Credentials.AccountName, PrimaryDestinationContainer.Name,
95+
PackageDatabase.DataSource, PackageDatabase.InitialCatalog);
96+
9697
await Archive(PrimaryDestinationContainer);
9798

9899
// todo: consider reusing package query for primary and secondary archives
99100
if (SecondaryDestinationContainer != null)
100101
{
101102
JobEventSourceLog.PreparingToArchive2(SecondaryDestination.Credentials.AccountName, SecondaryDestinationContainer.Name);
103+
102104
await Archive(SecondaryDestinationContainer);
103105
}
104106
}
@@ -124,9 +126,10 @@ private async Task Archive(CloudBlobContainer destinationContainer)
124126

125127
JobEventSourceLog.CursorData(cursorDateTime.ToString(DateTimeFormatSpecifier));
126128

127-
JobEventSourceLog.GatheringPackagesToArchiveFromDb(_packageDbConnectionFactory.DataSource, _packageDbConnectionFactory.InitialCatalog);
129+
JobEventSourceLog.GatheringPackagesToArchiveFromDb(PackageDatabase.DataSource, PackageDatabase.InitialCatalog);
130+
128131
List<PackageRef> packages;
129-
using (var connection = await _packageDbConnectionFactory.CreateAsync())
132+
using (var connection = await OpenSqlConnectionAsync(JobArgumentNames.PackageDatabase))
130133
{
131134
packages = (await connection.QueryAsync<PackageRef>(@"
132135
SELECT pr.Id, p.NormalizedVersion AS Version, p.Hash, p.LastEdited, p.Published
@@ -135,7 +138,8 @@ FROM Packages p
135138
WHERE Published > @cursorDateTime OR LastEdited > @cursorDateTime", new { cursorDateTime = cursorDateTime }))
136139
.ToList();
137140
}
138-
JobEventSourceLog.GatheredPackagesToArchiveFromDb(packages.Count, _packageDbConnectionFactory.DataSource, _packageDbConnectionFactory.InitialCatalog);
141+
142+
JobEventSourceLog.GatheredPackagesToArchiveFromDb(packages.Count, PackageDatabase.DataSource, PackageDatabase.InitialCatalog);
139143

140144
var archiveSet = packages
141145
.AsParallel()

src/ArchivePackages/ArchivePackages.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,6 @@
7575
<PackageReference Include="Newtonsoft.Json">
7676
<Version>9.0.1</Version>
7777
</PackageReference>
78-
<PackageReference Include="NuGet.Services.Sql">
79-
<Version>2.25.0-master-30453</Version>
80-
</PackageReference>
8178
<PackageReference Include="System.Net.Http">
8279
<Version>4.3.3</Version>
8380
</PackageReference>

src/Gallery.CredentialExpiration/Gallery.CredentialExpiration.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@
9292
<PackageReference Include="Newtonsoft.Json">
9393
<Version>9.0.1</Version>
9494
</PackageReference>
95-
<PackageReference Include="NuGet.Services.Sql">
96-
<Version>2.25.0-master-30263</Version>
97-
</PackageReference>
9895
<PackageReference Include="NuGet.Services.Storage">
9996
<Version>2.1.3</Version>
10097
</PackageReference>

src/Gallery.CredentialExpiration/GalleryCredentialExpiration.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
using System.Data.SqlClient;
77
using System.Linq;
88
using System.Threading.Tasks;
9-
using NuGet.Services.Sql;
109
using Gallery.CredentialExpiration.Models;
1110

1211
namespace Gallery.CredentialExpiration
1312
{
1413
public class GalleryCredentialExpiration : ICredentialExpirationExporter
1514
{
1615
private readonly CredentialExpirationJobMetadata _jobMetadata;
17-
private readonly ISqlConnectionFactory _galleryDatabase;
1816

19-
public GalleryCredentialExpiration(CredentialExpirationJobMetadata jobMetadata, ISqlConnectionFactory galleryDatabase)
17+
private Func<Task<SqlConnection>> OpenGallerySqlConnectionAsync { get; }
18+
19+
public GalleryCredentialExpiration(
20+
CredentialExpirationJobMetadata jobMetadata,
21+
Func<Task<SqlConnection>> openGallerySqlConnectionAsync)
2022
{
2123
_jobMetadata = jobMetadata;
22-
_galleryDatabase = galleryDatabase;
24+
OpenGallerySqlConnectionAsync = openGallerySqlConnectionAsync;
2325
}
2426

2527
/// <summary>
@@ -51,7 +53,7 @@ public async Task<List<ExpiredCredentialData>> GetCredentialsAsync(TimeSpan time
5153
var minNotificationDate = ConvertToString(GetMinNotificationDate());
5254

5355
// Connect to database
54-
using (var galleryConnection = await _galleryDatabase.CreateAsync())
56+
using (var galleryConnection = await OpenGallerySqlConnectionAsync())
5557
{
5658
// Fetch credentials that expire in _warnDaysBeforeExpiration days
5759
// + the user's e-mail address

src/Gallery.CredentialExpiration/Job.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Concurrent;
65
using System.Collections.Generic;
76
using System.ComponentModel.Design;
87
using System.Data.SqlClient;
@@ -15,10 +14,7 @@
1514
using Microsoft.Extensions.Logging;
1615
using Microsoft.WindowsAzure.Storage;
1716
using Newtonsoft.Json;
18-
using Newtonsoft.Json.Linq;
1917
using NuGet.Jobs;
20-
using NuGet.Services.KeyVault;
21-
using NuGet.Services.Sql;
2218
using NuGet.Services.Storage;
2319

2420
namespace Gallery.CredentialExpiration
@@ -34,8 +30,6 @@ public class Job : JobBase
3430
private string _galleryBrand;
3531
private string _galleryAccountUrl;
3632

37-
private ISqlConnectionFactory _galleryDatabase;
38-
3933
private string _mailFrom;
4034
private SmtpClient _smtpClient;
4135

@@ -47,9 +41,7 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
4741
{
4842
_whatIf = JobConfigurationManager.TryGetBoolArgument(jobArgsDictionary, JobArgumentNames.WhatIf);
4943

50-
var secretInjector = (ISecretInjector)serviceContainer.GetService(typeof(ISecretInjector));
51-
var databaseConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.GalleryDatabase);
52-
_galleryDatabase = new AzureSqlConnectionFactory(databaseConnectionString, secretInjector);
44+
RegisterDatabase(serviceContainer, jobArgsDictionary, JobArgumentNames.GalleryDatabase);
5345

5446
_galleryBrand = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryBrand);
5547
_galleryAccountUrl = JobConfigurationManager.GetArgument(jobArgsDictionary, MyJobArgumentNames.GalleryAccountUrl);
@@ -71,12 +63,17 @@ public override void Init(IServiceContainer serviceContainer, IDictionary<string
7163
_storage = storageFactory.Create();
7264
}
7365

66+
public Task<SqlConnection> OpenGallerySqlConnectionAsync()
67+
{
68+
return OpenSqlConnectionAsync(JobArgumentNames.GalleryDatabase);
69+
}
70+
7471
public override async Task Run()
7572
{
7673
var jobRunTime = DateTimeOffset.UtcNow;
7774
// Default values
7875
var jobCursor = new JobRunTimeCursor( jobCursorTime: jobRunTime, maxProcessedCredentialsTime: jobRunTime );
79-
var galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), _galleryDatabase);
76+
var galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), OpenGallerySqlConnectionAsync);
8077

8178
try
8279
{
@@ -89,7 +86,7 @@ public override async Task Run()
8986
// Load from cursor
9087
// Throw if the schema is not correct to ensure that not-intended emails are sent.
9188
jobCursor = JsonConvert.DeserializeObject<JobRunTimeCursor>(content, new JsonSerializerSettings() { MissingMemberHandling = MissingMemberHandling.Error });
92-
galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), _galleryDatabase);
89+
galleryCredentialExpiration = new GalleryCredentialExpiration(new CredentialExpirationJobMetadata(jobRunTime, _warnDaysBeforeExpiration, jobCursor), OpenGallerySqlConnectionAsync);
9390
}
9491

9592
// Connect to database

src/Gallery.Maintenance/DeleteExpiredVerificationKeysTask.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Threading.Tasks;
1010
using Gallery.Maintenance.Models;
1111
using Microsoft.Extensions.Logging;
12+
using NuGet.Jobs;
1213

1314
namespace Gallery.Maintenance
1415
{
@@ -37,7 +38,7 @@ public override async Task RunAsync(Job job)
3738
{
3839
IEnumerable<PackageVerificationKey> expiredKeys;
3940

40-
using (var connection = await job.GalleryDatabase.CreateAsync())
41+
using (var connection = await job.OpenSqlConnectionAsync(JobArgumentNames.GalleryDatabase))
4142
{
4243
expiredKeys = await connection.QueryWithRetryAsync<PackageVerificationKey>(
4344
SelectQuery,
@@ -59,7 +60,7 @@ public override async Task RunAsync(Job job)
5960

6061
if (expectedRowCount > 0)
6162
{
62-
using (var connection = await job.GalleryDatabase.CreateAsync())
63+
using (var connection = await job.OpenSqlConnectionAsync(JobArgumentNames.GalleryDatabase))
6364
{
6465
using (var transaction = connection.BeginTransaction())
6566
{

src/Gallery.Maintenance/Gallery.Maintenance.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@
6767
<PackageReference Include="Newtonsoft.Json">
6868
<Version>9.0.1</Version>
6969
</PackageReference>
70-
<PackageReference Include="NuGet.Services.Sql">
71-
<Version>2.25.0-master-30263</Version>
72-
</PackageReference>
7370
<PackageReference Include="System.Net.Http">
7471
<Version>4.3.3</Version>
7572
</PackageReference>

src/Gallery.Maintenance/Job.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
using System;
55
using System.Collections.Generic;
66
using System.ComponentModel.Design;
7-
using System.Data;
87
using System.Linq;
98
using System.Threading.Tasks;
109
using Microsoft.Extensions.Logging;
1110
using NuGet.Jobs;
1211
using NuGet.Services.KeyVault;
13-
using NuGet.Services.Sql;
1412

1513
namespace Gallery.Maintenance
1614
{
@@ -19,15 +17,9 @@ namespace Gallery.Maintenance
1917
/// </summary>
2018
public class Job : JobBase
2119
{
22-
23-
public ISqlConnectionFactory GalleryDatabase { get; private set; }
24-
2520
public override void Init(IServiceContainer serviceContainer, IDictionary<string, string> jobArgsDictionary)
2621
{
27-
var secretInjector = (ISecretInjector)serviceContainer.GetService(typeof(ISecretInjector));
28-
var databaseConnectionString = JobConfigurationManager.GetArgument(jobArgsDictionary, JobArgumentNames.GalleryDatabase);
29-
30-
GalleryDatabase = new AzureSqlConnectionFactory(databaseConnectionString, secretInjector);
22+
RegisterDatabase(serviceContainer, jobArgsDictionary, JobArgumentNames.GalleryDatabase);
3123
}
3224

3325
public override async Task Run()

src/NuGet.Jobs.Common/Extensions/SqlConnectionStringBuilderExtensions.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)