コード例 #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)
            {
                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);
            }
        }
コード例 #2
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);
            }
        }
コード例 #3
0
 public void Stop()
 {
     lock (this)
     {
         internalLoop.Stop();
         countAvailable = 0;
     }
 }
コード例 #4
0
 static Func<Task> CreateTestAction(IThreadLoop loop, Func<bool> callback)
 {
     return async () => {
         if (callback())
         {
             loop.Stop();
         }
         await Task.Delay(1);
     };
 }
コード例 #5
0
 static Func <Task> CreateTestAction(IThreadLoop loop, Func <bool> callback)
 {
     return(async() => {
         if (callback())
         {
             loop.Stop();
         }
         await Task.Delay(1);
     });
 }
コード例 #6
0
        public async Task DeferAsync(
            int milliseconds,
            Action action)
        {
            currentDefer = DateTime.UtcNow.Add(TimeSpan.FromMilliseconds(milliseconds));
            this.action  = action;

            if (threadLoop.IsRunning)
            {
                return;
            }

            await threadLoop.StartAsync(async() => {
                if (DateTime.UtcNow >= currentDefer)
                {
                    action();
                    threadLoop.Stop();
                    return;
                }

                var delta = currentDefer - DateTime.UtcNow;
                await threadDelay.ExecuteAsync((int)delta.TotalMilliseconds);
            });
        }