-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attempt to download tentacles multiple times #703
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
|
@@ -17,56 +18,99 @@ public class OctopusPackageDownloader | |
{ | ||
public static async Task DownloadPackage(string downloadUrl, string filePath, ILogger logger) | ||
{ | ||
var exceptions = new List<Exception>(); | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
try | ||
{ | ||
await AttemptToDownloadPackage(downloadUrl, filePath, logger); | ||
return; | ||
} | ||
catch (Exception e) | ||
{ | ||
exceptions.Add(e); | ||
} | ||
} | ||
|
||
throw new AggregateException(exceptions); | ||
} | ||
static async Task AttemptToDownloadPackage(string downloadUrl, string filePath, ILogger logger) | ||
{ | ||
var totalTime = Stopwatch.StartNew(); | ||
var totalRead = 0L; | ||
string expectedHash = null; | ||
using (var client = new HttpClient()) | ||
try | ||
{ | ||
client.Timeout = TimeSpan.FromSeconds(150); | ||
using (var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)) | ||
using (var client = new HttpClient()) | ||
{ | ||
response.EnsureSuccessStatusCode(); | ||
var totalLength = response.Content.Headers.ContentLength; | ||
if (response.Headers.TryGetValues("x-amz-meta-sha256", out var expectedHashs)) | ||
// This appears to be the time it takes to do a single read/write, not the entire download. | ||
client.Timeout = TimeSpan.FromSeconds(20); | ||
using (var response = await client.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)) | ||
{ | ||
expectedHash = expectedHashs.FirstOrDefault(); | ||
} | ||
response.EnsureSuccessStatusCode(); | ||
var totalLength = response.Content.Headers.ContentLength; | ||
expectedHash = TryGetExpectedHashFromHeaders(response, expectedHash); | ||
|
||
logger.Information($"Downloading {downloadUrl} ({totalLength} bytes)"); | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
using (Stream contentStream = await response.Content.ReadAsStreamAsync(), | ||
fileStream = new FileStream( | ||
filePath, | ||
FileMode.Create, | ||
FileAccess.Write, | ||
FileShare.None, | ||
8192, | ||
true)) | ||
{ | ||
var totalRead = 0L; | ||
var buffer = new byte[8192]; | ||
logger.Information($"Downloading {downloadUrl} ({totalLength} bytes)"); | ||
|
||
var read = await contentStream.ReadAsync(buffer, 0, buffer.Length); | ||
while (read != 0) | ||
var sw = new Stopwatch(); | ||
sw.Start(); | ||
using (Stream contentStream = await response.Content.ReadAsStreamAsync(), | ||
fileStream = new FileStream( | ||
filePath, | ||
FileMode.Create, | ||
FileAccess.Write, | ||
FileShare.None, | ||
8192, | ||
true)) | ||
{ | ||
await fileStream.WriteAsync(buffer, 0, read); | ||
|
||
if (totalLength.HasValue && sw.ElapsedMilliseconds >= TimeSpan.FromSeconds(7).TotalMilliseconds) | ||
var buffer = new byte[8192]; | ||
|
||
var read = await contentStream.ReadAsync(buffer, 0, buffer.Length); | ||
while (read != 0) | ||
{ | ||
var percentRead = totalRead * 1.0 / totalLength.Value * 100; | ||
logger.Information($"Downloading Completed {percentRead}%"); | ||
sw.Reset(); | ||
sw.Start(); | ||
await fileStream.WriteAsync(buffer, 0, read); | ||
|
||
if (totalLength.HasValue && sw.ElapsedMilliseconds >= TimeSpan.FromSeconds(7).TotalMilliseconds) | ||
{ | ||
var percentRead = totalRead * 1.0 / totalLength.Value * 100; | ||
logger.Information($"Downloading Completed {percentRead}%"); | ||
sw.Reset(); | ||
sw.Start(); | ||
} | ||
|
||
read = await contentStream.ReadAsync(buffer, 0, buffer.Length); | ||
totalRead += read; | ||
} | ||
|
||
totalTime.Stop(); | ||
|
||
read = await contentStream.ReadAsync(buffer, 0, buffer.Length); | ||
totalRead += read; | ||
logger.Information("Download Finished in {totalTime}ms", totalTime.ElapsedMilliseconds); | ||
} | ||
|
||
logger.Information("Download Finished"); | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new Exception($"Failure to download: {downloadUrl}. After {totalTime.Elapsed.TotalSeconds} seconds we only downloaded, {totalRead}", e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These logs don't make it to the build server :(, so instead make the exception contain information already in the logs. |
||
} | ||
|
||
ValidateDownload(filePath, expectedHash); | ||
} | ||
|
||
static string TryGetExpectedHashFromHeaders(HttpResponseMessage response, string expectedHash) | ||
{ | ||
if (response.Headers.TryGetValues("x-amz-meta-sha256", out var expectedHashs)) | ||
{ | ||
expectedHash = expectedHashs.FirstOrDefault(); | ||
} | ||
|
||
return expectedHash; | ||
} | ||
|
||
static void ValidateDownload(string filePath, string expectedHash) | ||
{ | ||
if (!expectedHash.IsNullOrEmpty()) | ||
{ | ||
using (var sha256 = SHA256.Create()) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simple for loop is simple