public async Task <bool> CheckIfBlobExistsAsync(string blobPath) { string url = $"{FeedContainerUrl}/{blobPath}?comp=metadata"; using (HttpClient client = new HttpClient()) { const int MaxAttempts = 15; // add a bit of randomness to the retry delay. var rng = new Random(); int retryCount = MaxAttempts; // Used to make sure TaskCancelledException comes from timeouts. CancellationTokenSource cancelTokenSource = new CancellationTokenSource(); while (true) { try { client.DefaultRequestHeaders.Clear(); var request = AzureHelper.RequestMessage("GET", url, AccountName, AccountKey).Invoke(); using (HttpResponseMessage response = await client.SendAsync(request, cancelTokenSource.Token)) { if (response.IsSuccessStatusCode) { Log.LogMessage( MessageImportance.Low, $"Blob {blobPath} exists for {AccountName}: Status Code:{response.StatusCode} Status Desc: {await response.Content.ReadAsStringAsync()}"); } else { Log.LogMessage( MessageImportance.Low, $"Blob {blobPath} does not exist for {AccountName}: Status Code:{response.StatusCode} Status Desc: {await response.Content.ReadAsStringAsync()}"); } return(response.IsSuccessStatusCode); } } catch (HttpRequestException toLog) { if (retryCount <= 0) { Log.LogError($"Unable to check for existence of blob {blobPath} in {AccountName} after {MaxAttempts} retries."); throw; } else { Log.LogWarning("Exception thrown while trying to detect if blob already exists in feed:"); Log.LogWarningFromException(toLog, true); } } catch (TaskCanceledException possibleTimeoutToLog) { // Detect timeout. if (possibleTimeoutToLog.CancellationToken != cancelTokenSource.Token) { if (retryCount <= 0) { Log.LogError($"Unable to check for existence of blob {blobPath} in {AccountName} after {MaxAttempts} retries."); throw; } else { Log.LogWarning("Exception thrown while trying to detect if blob already exists in feed:"); Log.LogWarningFromException(possibleTimeoutToLog, true); } } else { throw; } } --retryCount; Log.LogWarning($"Failed to check for existence of blob {blobPath}. {retryCount} attempts remaining"); int delay = (MaxAttempts - retryCount) * rng.Next(1, 7); await Task.Delay(delay * 1000); } } }
public static async Task<HttpResponseMessage> RequestWithRetry(TaskLoggingHelper loggingHelper, HttpClient client, Func<HttpRequestMessage> createRequest, Func<HttpResponseMessage, bool> validationCallback = null, int retryCount = 5, int retryDelaySeconds = 5) { if (loggingHelper == null) throw new ArgumentNullException(nameof(loggingHelper)); if (client == null) throw new ArgumentNullException(nameof(client)); if (createRequest == null) throw new ArgumentNullException(nameof(createRequest)); if (retryCount < 1) throw new ArgumentException(nameof(retryCount)); if (retryDelaySeconds < 1) throw new ArgumentException(nameof(retryDelaySeconds)); int retries = 0; HttpResponseMessage response = null; // add a bit of randomness to the retry delay var rng = new Random(); while (retries < retryCount) { if (retries > 0) { if (response != null) { response.Dispose(); response = null; } int delay = retryDelaySeconds * retries * rng.Next(1, 5); loggingHelper.LogMessage(MessageImportance.Low, "Waiting {0} seconds before retry", delay); await System.Threading.Tasks.Task.Delay(delay * 1000); } try { using (var request = createRequest()) response = await client.SendAsync(request); } catch (Exception e) { loggingHelper.LogWarningFromException(e, true); // if this is the final iteration let the exception bubble up if (retries + 1 == retryCount) throw; } // response can be null if we fail to send the request if (response != null) { if (validationCallback == null) { // check if the response code is within the range of failures if (IsWithinRetryRange(response.StatusCode)) { loggingHelper.LogWarning("Request failed with status code {0}", response.StatusCode); } else { loggingHelper.LogMessage(MessageImportance.Low, "Response completed with status code {0}", response.StatusCode); return response; } } else { bool isSuccess = validationCallback(response); if (!isSuccess) { loggingHelper.LogMessage("Validation callback returned retry for status code {0}", response.StatusCode); } else { loggingHelper.LogMessage("Validation callback returned success for status code {0}", response.StatusCode); return response; } } } ++retries; } // retry count exceeded loggingHelper.LogWarning("Retry count {0} exceeded", retryCount); // set some default values in case response is null var statusCode = "None"; var contentStr = "Null"; if (response != null) { statusCode = response.StatusCode.ToString(); contentStr = await response.Content.ReadAsStringAsync(); response.Dispose(); } throw new HttpRequestException(string.Format("Request failed with status {0} response {1}", statusCode, contentStr)); }