Example #1
0
        async Task WrapJobInRetryingMechanism(
            RetryingThreadLoopJob job)
        {
            var attempts         = 0;
            var exceptionsCaught = new HashSet <Exception>();

            try
            {
                attempts++;
                await job.Action();

                threadLoop.Stop();
            }
            catch (Exception ex)
            {
                exceptionsCaught.Add(ex);
                if ((job.IsExceptionIgnored != null) &&
                    !job.IsExceptionIgnored(ex))
                {
                    throw;
                }

                if (attempts == job.AttemptsBeforeFailing)
                {
                    throw new AggregateException(
                              "The operation timed out while attempting to invoke the given job, and several exceptions were caught within the duration of these attempts.",
                              exceptionsCaught);
                }

                await Task.Delay(job.IntervalInMilliseconds);
            }
        }
        async Task WrapJobInRetryingMechanism(
            RetryingThreadLoopJob job)
        {
            var attempts         = 0;
            var exceptionsCaught = new HashSet <Exception>();

            try
            {
                attempts++;
                await job.Action();

                threadLoop.Stop();
            }
            catch (Exception ex)
            {
                logger.Warning("Attempt #{attempt}. Will try {tries} more times. {message}", attempts, job.AttemptsBeforeFailing - attempts, ex.Message);

                exceptionsCaught.Add(ex);
                if (job.IsExceptionIgnored?.Invoke(ex) != true)
                {
                    throw;
                }

                if (attempts == job.AttemptsBeforeFailing)
                {
                    throw new AggregateException(
                              "The operation timed out while attempting to invoke the given job, and several exceptions were caught within the duration of these attempts.",
                              exceptionsCaught);
                }

                await Task.Delay(job.IntervalInMilliseconds);
            }
        }
        async Task WrapJobInRetryingMechanism(
            RetryingThreadLoopJob job)
        {
            var attempts = 0;
            var exceptionsCaught = new HashSet<Exception>();

            try
            {
                attempts++;
                await job.Action();
                threadLoop.Stop();
            }
            catch (Exception ex)
            {
                exceptionsCaught.Add(ex);
                if ((job.IsExceptionIgnored != null) && 
                    !job.IsExceptionIgnored(ex))
                {
                    throw;
                }
                
                if (attempts == job.AttemptsBeforeFailing)
                {
                    throw new AggregateException(
                        "The operation timed out while attempting to invoke the given job, and several exceptions were caught within the duration of these attempts.",
                        exceptionsCaught);
                }

                await Task.Delay(job.IntervalInMilliseconds);
            }
        }