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); } }
public void Stop() { lock (this) { internalLoop.Stop(); countAvailable = 0; } }
static Func<Task> CreateTestAction(IThreadLoop loop, Func<bool> callback) { return async () => { if (callback()) { loop.Stop(); } await Task.Delay(1); }; }
static Func <Task> CreateTestAction(IThreadLoop loop, Func <bool> callback) { return(async() => { if (callback()) { loop.Stop(); } await Task.Delay(1); }); }
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); }); }