This repository has been archived by the owner on Apr 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pulling logic to make sure it is uploaded to the feed. Add retry logic for the whole upload process Remove the old upload script
- Loading branch information
William Li
committed
Aug 30, 2017
1 parent
8de61cd
commit 5baf66d
Showing
19 changed files
with
647 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
build_projects/dotnet-cli-build.Tests/GivenActionAndRetryTimes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.DotNet.Cli.Build.UploadToLinuxPackageRepository; | ||
using Xunit; | ||
|
||
namespace dotnet_cli_build.Tests | ||
{ | ||
public class GivenActionAndRetryTimes | ||
{ | ||
public static IEnumerable<Task> NoWaitTimer() | ||
{ | ||
while (true) | ||
{ | ||
yield return Task.CompletedTask; | ||
} | ||
} | ||
|
||
[Fact] | ||
public void ExponentialRetryShouldProvideIntervalSequence() | ||
{ | ||
ExponentialRetry.Intervals.First().Should().Be(TimeSpan.FromSeconds(5)); | ||
ExponentialRetry.Intervals.Skip(1).First().Should().Be(TimeSpan.FromSeconds(10)); | ||
ExponentialRetry.Intervals.Skip(2).First().Should().Be(TimeSpan.FromSeconds(20)); | ||
ExponentialRetry.Intervals.Skip(3).First().Should().Be(TimeSpan.FromSeconds(40)); | ||
ExponentialRetry.Intervals.Skip(4).First().Should().Be(TimeSpan.FromSeconds(80)); | ||
} | ||
|
||
[Fact] | ||
public void ExponentialShouldNotRetryAfterFirstSucceess() | ||
{ | ||
var fakeAction = new FakeAction(0); | ||
ExponentialRetry.ExecuteWithRetry( | ||
fakeAction.Run, | ||
s => s == "success", | ||
10, | ||
NoWaitTimer).Wait(); | ||
fakeAction.Count.Should().Be(0); | ||
} | ||
|
||
[Fact] | ||
public void ExponentialShouldRetryUntilSuccess() | ||
{ | ||
var fakeAction = new FakeAction(5); | ||
ExponentialRetry.ExecuteWithRetry( | ||
fakeAction.Run, | ||
s => s == "success", | ||
10, | ||
NoWaitTimer).Wait(); | ||
fakeAction.Count.Should().Be(5); | ||
} | ||
|
||
[Fact] | ||
public void ExponentialShouldThrowAfterMaximumAmountReached() | ||
{ | ||
var fakeAction = new FakeAction(10); | ||
Action a = () => ExponentialRetry.ExecuteWithRetry( | ||
fakeAction.Run, | ||
s => s == "success", | ||
5, | ||
NoWaitTimer, | ||
"testing retry").Wait(); | ||
a.ShouldThrow<RetryFailedException>() | ||
.WithMessage("Retry failed for testing retry after 5 times with result: fail"); | ||
} | ||
} | ||
|
||
public class FakeAction | ||
{ | ||
private readonly int _successAfter; | ||
|
||
public FakeAction(int successAfter) | ||
{ | ||
_successAfter = successAfter; | ||
} | ||
|
||
public int Count { get; private set; } | ||
|
||
public Task<string> Run() | ||
{ | ||
if (_successAfter == Count) | ||
{ | ||
return Task.FromResult("success"); | ||
} | ||
|
||
Count++; | ||
return Task.FromResult("fail"); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
build_projects/dotnet-cli-build.Tests/dotnet-cli-build.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>$(CliTargetFramework)</TargetFramework> | ||
<VersionPrefix>1.0.0</VersionPrefix> | ||
<AssetTargetFallback>$(AssetTargetFallback);portable-net45+win8+wp8+wpa81</AssetTargetFallback> | ||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(CLI_TestPlatform_Version)" /> | ||
<PackageReference Include="xunit" Version="2.2.0" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" /> | ||
<PackageReference Include="FluentAssertions" Version="4.18.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\dotnet-cli-build\dotnet-cli-build.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
62 changes: 62 additions & 0 deletions
62
build_projects/dotnet-cli-build/UploadToLinuxPackageRepository/AddPackageStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using NuGet.Protocol; | ||
|
||
namespace Microsoft.DotNet.Cli.Build.UploadToLinuxPackageRepository | ||
{ | ||
internal class AddPackageStrategy : IAzurelinuxRepositoryServiceHttpStrategy | ||
{ | ||
private readonly IdInRepositoryService _idInRepositoryService; | ||
private readonly string _packageName; | ||
private readonly string _packageVersion; | ||
private readonly string _repositoryId; | ||
|
||
public AddPackageStrategy( | ||
IdInRepositoryService idInRepositoryService, | ||
string packageName, | ||
string packageVersion, | ||
string repositoryId) | ||
{ | ||
_idInRepositoryService = idInRepositoryService | ||
?? throw new ArgumentNullException(nameof(idInRepositoryService)); | ||
_packageName = packageName; | ||
_packageVersion = packageVersion; | ||
_repositoryId = repositoryId; | ||
} | ||
|
||
public async Task<string> Execute(HttpClient client, Uri baseAddress) | ||
{ | ||
var debianUploadJsonContent = new Dictionary<string, string> | ||
{ | ||
["name"] = _packageName, | ||
["version"] = AppendDebianRevisionNumber(_packageVersion), | ||
["fileId"] = _idInRepositoryService.Id, | ||
["repositoryId"] = _repositoryId | ||
}.ToJson(); | ||
var content = new StringContent(debianUploadJsonContent, | ||
Encoding.UTF8, | ||
"application/json"); | ||
|
||
using (var response = await client.PostAsync(new Uri(baseAddress, "/v1/packages"), content)) | ||
{ | ||
if (!response.IsSuccessStatusCode) | ||
throw new FailedToAddPackageToPackageRepositoryException( | ||
$"request:{debianUploadJsonContent} response:{response.ToJson()}"); | ||
return response.Headers.GetValues("Location").Single(); | ||
} | ||
} | ||
|
||
private static string AppendDebianRevisionNumber(string packageVersion) | ||
{ | ||
return packageVersion + "-1"; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
build_projects/dotnet-cli-build/UploadToLinuxPackageRepository/ExponentialRetry.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.DotNet.Cli.Build.UploadToLinuxPackageRepository | ||
{ | ||
public static class ExponentialRetry | ||
{ | ||
public static IEnumerable<TimeSpan> Intervals | ||
{ | ||
get | ||
{ | ||
var seconds = 5; | ||
while (true) | ||
{ | ||
yield return TimeSpan.FromSeconds(seconds); | ||
seconds *= 2; | ||
} | ||
} | ||
} | ||
|
||
public static async Task ExecuteWithRetry(Func<Task<string>> action, | ||
Func<string, bool> isSuccess, | ||
int maxRetryCount, | ||
Func<IEnumerable<Task>> timer, | ||
string taskDescription = "") | ||
{ | ||
var count = 0; | ||
foreach (var t in timer()) | ||
{ | ||
await t; | ||
var result = await action(); | ||
if (isSuccess(result)) | ||
return; | ||
count++; | ||
if (count == maxRetryCount) | ||
throw new RetryFailedException( | ||
$"Retry failed for {taskDescription} after {count} times with result: {result}"); | ||
} | ||
throw new Exception("Timer should not be exhausted"); | ||
} | ||
|
||
public static IEnumerable<Task> Timer(IEnumerable<TimeSpan> interval) | ||
{ | ||
return interval.Select(Task.Delay); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...li-build/UploadToLinuxPackageRepository/FailedToAddPackageToPackageRepositoryException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.DotNet.Cli.Build.UploadToLinuxPackageRepository | ||
{ | ||
public class FailedToAddPackageToPackageRepositoryException : Exception | ||
{ | ||
public FailedToAddPackageToPackageRepositoryException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public FailedToAddPackageToPackageRepositoryException() | ||
{ | ||
} | ||
|
||
public FailedToAddPackageToPackageRepositoryException(string message, Exception innerException) : base(message, | ||
innerException) | ||
{ | ||
} | ||
} | ||
} |
Oops, something went wrong.