-
Couldn't load subscription status.
- Fork 867
Add DownloadInitiated, Failed and Completed events #4079
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
base: GarrettBeatty/stacked/4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "services": [ | ||
| { | ||
| "serviceName": "S3", | ||
| "type": "minor", | ||
| "changeLogMessages": [ | ||
| "Added DownloadInitiatedEvent, DownloadCompletedEvent, and DownloadFailedEvent for downloads." | ||
| ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,34 @@ static Logger Logger | |
|
|
||
| IAmazonS3 _s3Client; | ||
| TransferUtilityDownloadRequest _request; | ||
| long _totalTransferredBytes; | ||
|
|
||
| #region Event Firing Methods | ||
|
|
||
| private void FireTransferInitiatedEvent() | ||
| { | ||
| var transferInitiatedEventArgs = new DownloadInitiatedEventArgs(_request, _request.FilePath); | ||
| _request.OnRaiseTransferInitiatedEvent(transferInitiatedEventArgs); | ||
| } | ||
|
|
||
| private void FireTransferCompletedEvent(TransferUtilityDownloadResponse response, string filePath, long transferredBytes, long totalBytes) | ||
| { | ||
| var transferCompletedEventArgs = new DownloadCompletedEventArgs( | ||
| _request, | ||
| response, | ||
| filePath, | ||
| transferredBytes, | ||
| totalBytes); | ||
| _request.OnRaiseTransferCompletedEvent(transferCompletedEventArgs); | ||
| } | ||
|
|
||
| private void FireTransferFailedEvent(string filePath, long transferredBytes, long totalBytes = -1) | ||
| { | ||
| var eventArgs = new DownloadFailedEventArgs(this._request, filePath, transferredBytes, totalBytes); | ||
| this._request.OnRaiseTransferFailedEvent(eventArgs); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| internal DownloadCommand(IAmazonS3 s3Client, TransferUtilityDownloadRequest request) | ||
| { | ||
|
|
@@ -89,6 +117,12 @@ private void ValidateRequest() | |
|
|
||
| void OnWriteObjectProgressEvent(object sender, WriteObjectProgressArgs e) | ||
| { | ||
| // Keep track of the total transferred bytes so that we can also return this value in case of failure | ||
| Interlocked.Add(ref _totalTransferredBytes, e.IncrementTransferred); | ||
|
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. not actually sure if interlocked is needed here. but added to be consistent with upload |
||
|
|
||
| // Set the Request property to enable access to the original download request | ||
| e.Request = this._request; | ||
|
|
||
| this._request.OnRaiseProgressEvent(e); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,12 +33,17 @@ internal partial class DownloadCommand : BaseCommand<TransferUtilityDownloadResp | |
| public override async Task<TransferUtilityDownloadResponse> ExecuteAsync(CancellationToken cancellationToken) | ||
| { | ||
| ValidateRequest(); | ||
|
|
||
| FireTransferInitiatedEvent(); | ||
|
|
||
| GetObjectRequest getRequest = ConvertToGetObjectRequest(this._request); | ||
|
|
||
| var maxRetries = _s3Client.Config.MaxErrorRetry; | ||
| var retries = 0; | ||
| bool shouldRetry = false; | ||
| string mostRecentETag = null; | ||
| TransferUtilityDownloadResponse lastSuccessfulMappedResponse = null; | ||
| long? totalBytesFromResponse = null; // Track total bytes once we have response headers | ||
| do | ||
| { | ||
| shouldRetry = false; | ||
|
|
@@ -54,12 +59,16 @@ public override async Task<TransferUtilityDownloadResponse> ExecuteAsync(Cancell | |
| using (var response = await this._s3Client.GetObjectAsync(getRequest, cancellationToken) | ||
| .ConfigureAwait(continueOnCapturedContext: false)) | ||
| { | ||
| // Capture total bytes from response headers as soon as we get them | ||
| totalBytesFromResponse = response.ContentLength; | ||
|
|
||
| if (!string.IsNullOrEmpty(mostRecentETag) && !string.Equals(mostRecentETag, response.ETag)) | ||
| { | ||
| //if the eTag changed, we need to retry from the start of the file | ||
| mostRecentETag = response.ETag; | ||
| getRequest.ByteRange = null; | ||
| retries = 0; | ||
| Interlocked.Exchange(ref _totalTransferredBytes, 0); | ||
GarrettBeatty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| shouldRetry = true; | ||
| WaitBeforeRetry(retries); | ||
| continue; | ||
|
|
@@ -101,6 +110,8 @@ await response.WriteResponseStreamToFileAsync(this._request.FilePath, false, can | |
| await response.WriteResponseStreamToFileAsync(this._request.FilePath, true, cancellationToken) | ||
| .ConfigureAwait(continueOnCapturedContext: false); | ||
| } | ||
|
|
||
| lastSuccessfulMappedResponse = ResponseMapper.MapGetObjectResponse(response); | ||
| } | ||
| } | ||
| catch (Exception exception) | ||
|
|
@@ -109,6 +120,9 @@ await response.WriteResponseStreamToFileAsync(this._request.FilePath, true, canc | |
| shouldRetry = HandleExceptionForHttpClient(exception, retries, maxRetries); | ||
| if (!shouldRetry) | ||
| { | ||
| // Pass total bytes if we have them from response headers, otherwise -1 for unknown | ||
| FireTransferFailedEvent(this._request.FilePath, Interlocked.Read(ref _totalTransferredBytes), totalBytesFromResponse ?? -1); | ||
|
|
||
| if (exception is IOException) | ||
| { | ||
| throw; | ||
|
|
@@ -131,8 +145,9 @@ await response.WriteResponseStreamToFileAsync(this._request.FilePath, true, canc | |
| WaitBeforeRetry(retries); | ||
| } while (shouldRetry); | ||
|
|
||
| // TODO map and return response | ||
| return new TransferUtilityDownloadResponse(); | ||
| FireTransferCompletedEvent(lastSuccessfulMappedResponse, this._request.FilePath, Interlocked.Read(ref _totalTransferredBytes), totalBytesFromResponse ?? -1); | ||
|
|
||
| return lastSuccessfulMappedResponse; | ||
|
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. copilot is saying that this could potentially be null but based on my reading of the code, if there is any failure we will throw exception early on, so it can never be null. thoughts? |
||
| } | ||
|
|
||
| private static bool HandleExceptionForHttpClient(Exception exception, int retries, int maxRetries) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.