Ejemplo n.º 1
0
        /// <inheritdoc />
        public byte[] GetPackageFile(
            PackageDescription packageDescription)
        {
            new { packageDescription }.Must().NotBeNull();
            var ret =
                Using
                .LinearBackOff(TimeSpan.FromSeconds(5))
                .Run(() =>
            {
                var workingDirectory = Path.Combine(
                    this.defaultWorkingDirectory,
                    "Down-" + DateTime.Now.ToString(
                        DirectoryDateTimeToStringFormat,
                        CultureInfo.InvariantCulture));

                var packageFilePath  = this.DownloadPackages(new[] { packageDescription }, workingDirectory).Single();
                var packageFileBytes = File.ReadAllBytes(packageFilePath);

                // clean up temp files
                Directory.Delete(workingDirectory, true);
                return(packageFileBytes);
            })
                .Now();

            return(ret);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Runs a function and retries if any exception is thrown, using a linear backoff strategy.
        /// </summary>
        /// <param name="operation">The operation to execute.</param>
        /// <param name="retryCount">Optional number of retries; DEFAULT is <see cref="DefaultRetryCount" />.</param>
        /// <param name="backOffDelay">Optional backoff delay; DEFAULT is <see cref="DefaultLinearBackoffDelay" />.</param>
        /// <returns>
        /// A task.
        /// </returns>
        public static void WithRetry(this Action operation, int retryCount = DefaultRetryCount, TimeSpan backOffDelay = default(TimeSpan))
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            var localBackOff = backOffDelay == default(TimeSpan) ? DefaultLinearBackoffDelay : backOffDelay;

            Using
            .LinearBackOff(localBackOff)
            .WithMaxRetries(retryCount)
            .Run(operation)
            .Now();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Runs a function and retries if any exception is thrown, using a linear backoff strategy.
        /// </summary>
        /// <param name="operation">The operation to execute.</param>
        /// <param name="retryCount">Optional number of retries; DEFAULT is <see cref="DefaultRetryCount" />.</param>
        /// <param name="backOffDelay">Optional backoff delay; DEFAULT is <see cref="DefaultLinearBackoffDelay" />.</param>
        /// <returns>
        /// A task.
        /// </returns>
        public static async Task WithRetryAsync(this Func <Task> operation, int retryCount = DefaultRetryCount, TimeSpan backOffDelay = default(TimeSpan))
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            var localBackOff = backOffDelay == default(TimeSpan) ? DefaultLinearBackoffDelay : backOffDelay;

            await Using
            .LinearBackOff(localBackOff)
            .WithMaxRetries(retryCount)
            .RunAsync(operation)
            .Now();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Runs a function and retries if any exception is thrown, using a linear backoff strategy.
        /// </summary>
        /// <typeparam name="T">The type of task returned by the operation.</typeparam>
        /// <param name="operation">The operation to execute.</param>
        /// <param name="retryCount">Optional number of retries; DEFAULT is <see cref="DefaultRetryCount" />.</param>
        /// <param name="backOffDelay">Optional backoff delay; DEFAULT is <see cref="DefaultLinearBackoffDelay" />.</param>
        /// <returns>
        /// A task of type returned by the operation.
        /// </returns>
        public static T WithRetry <T>(this Func <T> operation, int retryCount = DefaultRetryCount, TimeSpan backOffDelay = default(TimeSpan))
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            var localBackOff = backOffDelay == default(TimeSpan) ? DefaultLinearBackoffDelay : backOffDelay;

            var result = Using
                         .LinearBackOff(localBackOff)
                         .WithMaxRetries(retryCount)
                         .Run(operation)
                         .Now();

            return(result);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Runs a function and retries if any exception is thrown, using a linear backoff strategy.
        /// </summary>
        /// <param name="operation">The operation to execute.</param>
        /// <param name="reporter">Action to call to report exceptions as they occur.</param>
        /// <param name="messageBuilder">
        /// Optional.  Transforms the exception message and uses that as the Message property of the
        /// anonymous object that's sent to the <paramref name="reporter"/>.  If null, then the exception's
        /// Message is used.
        /// </param>
        /// <param name="retryCount">Optional number of retries; DEFAULT is <see cref="DefaultRetryCount" />.</param>
        /// <param name="backOffDelay">Optional backoff delay; DEFAULT is <see cref="DefaultLinearBackoffDelay" />.</param>
        /// <returns>
        /// A task.
        /// </returns>
        public static async Task WithRetryAsync(this Func <Task> operation, Action <object> reporter, Func <Exception, string> messageBuilder = null, int retryCount = DefaultRetryCount, TimeSpan backOffDelay = default(TimeSpan))
        {
            if (operation == null)
            {
                throw new ArgumentNullException(nameof(operation));
            }

            if (reporter == null)
            {
                throw new ArgumentNullException(nameof(reporter));
            }

            var localBackOff = backOffDelay == default(TimeSpan) ? DefaultLinearBackoffDelay : backOffDelay;

            await Using
            .LinearBackOff(localBackOff)
            .WithReporter(_ => reporter(new { Message = messageBuilder == null ? _.Message : messageBuilder(_), Exception = _ }))
            .WithMaxRetries(retryCount)
            .RunAsync(operation)
            .Now();
        }