Beispiel #1
0
        /// <summary>
        ///     Determine whether the feed is public or private.
        /// </summary>
        /// <param name="feedUrl">Feed url to test</param>
        /// <returns>True if the feed is public, false if it is private, and null if it was not possible to determine.</returns>
        /// <remarks>
        /// Do an unauthenticated GET on the feed URL. If it succeeds, the feed is not public.
        /// If it fails with a 4* error, assume it is internal.
        /// </remarks>
        public static async Task <bool?> IsFeedPublicAsync(string feedUrl, HttpClient httpClient, TaskLoggingHelper log)
        {
            bool?isPublic = null;

            ExponentialRetry RetryHandler = new ExponentialRetry
            {
                MaxAttempts = MaxRetries
            };

            bool success = await RetryHandler.RunAsync(async attempt =>
            {
                try
                {
                    HttpResponseMessage response = await httpClient.GetAsync(feedUrl);
                    if (response.IsSuccessStatusCode)
                    {
                        isPublic = true;
                        return(true);
                    }
                    else if (response.StatusCode >= (System.Net.HttpStatusCode) 400 &&
                             response.StatusCode < (System.Net.HttpStatusCode) 500)
                    {
                        isPublic = false;
                        return(true);
                    }
                    else
                    {
                        // Don't know for certain, retry
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    log.LogMessage(MessageImportance.Low, $"Unexpected exception {e.Message} when attempting to determine whether feed is internal.");
                    return(false);
                }
            });

            if (!success)
            {
                // We couldn't determine anything.  We'd be unlikely to be able to push to this feed either,
                // since it's 5xx'ing.
                log.LogError($"Unable to determine whether '{feedUrl}' is public or internal.");
            }

            return(isPublic);
        }
Beispiel #2
0
        public async Task UploadBlockBlobAsync(string filePath, string blobPath)
        {
            BlobClient      blob    = GetBlob(blobPath.Replace("\\", "/"));
            BlobHttpHeaders headers = GetBlobHeadersByExtension(filePath);

            // This function can sporadically throw
            // "System.Net.Http.HttpRequestException: Error while copying content to a stream."
            // Ideally it should retry for itself internally, but the existing retry seems
            // to be intended for throttling only.
            var retryHandler = new ExponentialRetry
            {
                MaxAttempts = 5,
                DelayBase   = 2.5 // 2.5 ^ 5 = ~1.5 minutes max wait between retries
            };

            Exception mostRecentlyCaughtException = null;

            bool success = await retryHandler.RunAsync(async attempt =>
            {
                try
                {
                    await blob.UploadAsync(
                        filePath,
                        headers)
                    .ConfigureAwait(false);
                    return(true);
                }
                catch (System.Net.Http.HttpRequestException toStore)
                {
                    mostRecentlyCaughtException = toStore;
                    return(false);
                }
            }).ConfigureAwait(false);

            // If retry failed print out a nice looking exception
            if (!success)
            {
                throw new Exception($"Failed to upload local file '{filePath}' to '{blobPath} in  {retryHandler.MaxAttempts} attempts.  See inner exception for details.", mostRecentlyCaughtException);
            }
        }