/// <summary> /// Invokes lazy attempts until success or all attempts fail. /// </summary> /// <param name="maxReturnAttempts"> /// The maximum number of attempts to return. When specified only the most recent attempts /// will be returned. /// </param> /// <returns>An array of the attempts.</returns> public static Attempts <T> Execute <T>(this IEnumerable <Lazy <T> > lazyAttempts, int?maxReturnAttempts = null) where T : Attempt { var attempts = new Attempts <T> { Capacity = maxReturnAttempts }; foreach (var attempt in lazyAttempts.Select(x => x.Value)) { attempts.Add(attempt); if (attempt.Succeeded) { break; } } attempts.EndDateTime = DateTimeOffset.Now; return(attempts); }
/// <summary> /// Invokes lazy attempts until success or all attempts fail. /// </summary> /// <param name="maxReturnAttempts"> /// The maximum number of attempts to return. When specified only the most recent attempts /// will be returned. /// </param> /// <returns>An array of the attempts.</returns> public static async Task <Attempts <T> > ExecuteAsync <T>(this IEnumerable <Lazy <Task <T> > > lazyAttempts, int?maxReturnAttempts = null) where T : Attempt { var attempts = new Attempts <T> { Capacity = maxReturnAttempts }; foreach (var lazyAttempt in lazyAttempts) { var attempt = await lazyAttempt.Value.ConfigureAwait(false); attempts.Add(attempt); if (attempt.Succeeded) { break; } } attempts.EndDateTime = DateTimeOffset.Now; return(attempts); }