2024-06-05 07:20:00 +00:00
|
|
|
|
namespace Utils
|
2024-06-04 09:06:19 +00:00
|
|
|
|
{
|
2024-06-05 07:20:00 +00:00
|
|
|
|
public class Retry
|
2024-06-04 09:06:19 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly string description;
|
|
|
|
|
private readonly TimeSpan maxTimeout;
|
|
|
|
|
private readonly TimeSpan sleepAfterFail;
|
|
|
|
|
private readonly Action<Failure> onFail;
|
|
|
|
|
|
2024-06-05 07:20:00 +00:00
|
|
|
|
public Retry(string description, TimeSpan maxTimeout, TimeSpan sleepAfterFail, Action<Failure> onFail)
|
2024-06-04 09:06:19 +00:00
|
|
|
|
{
|
|
|
|
|
this.description = description;
|
|
|
|
|
this.maxTimeout = maxTimeout;
|
|
|
|
|
this.sleepAfterFail = sleepAfterFail;
|
|
|
|
|
this.onFail = onFail;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 07:20:00 +00:00
|
|
|
|
public void Run(Action task)
|
2024-06-04 09:06:19 +00:00
|
|
|
|
{
|
2024-06-05 07:20:00 +00:00
|
|
|
|
var run = new RetryRun(description, task, maxTimeout, sleepAfterFail, onFail);
|
|
|
|
|
run.Run();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public T Run<T>(Func<T> task)
|
|
|
|
|
{
|
|
|
|
|
T? result = default;
|
|
|
|
|
|
|
|
|
|
var run = new RetryRun(description, () =>
|
|
|
|
|
{
|
|
|
|
|
result = task();
|
|
|
|
|
}, maxTimeout, sleepAfterFail, onFail);
|
|
|
|
|
run.Run();
|
|
|
|
|
|
|
|
|
|
return result!;
|
2024-06-04 09:06:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class RetryRun
|
|
|
|
|
{
|
|
|
|
|
private readonly string description;
|
2024-06-05 07:20:00 +00:00
|
|
|
|
private readonly Action task;
|
2024-06-04 09:06:19 +00:00
|
|
|
|
private readonly TimeSpan maxTimeout;
|
|
|
|
|
private readonly TimeSpan sleepAfterFail;
|
|
|
|
|
private readonly Action<Failure> onFail;
|
|
|
|
|
private readonly DateTime start = DateTime.UtcNow;
|
|
|
|
|
private readonly List<Failure> failures = new List<Failure>();
|
|
|
|
|
private int tryNumber;
|
|
|
|
|
private DateTime tryStart;
|
|
|
|
|
|
2024-06-05 07:20:00 +00:00
|
|
|
|
public RetryRun(string description, Action task, TimeSpan maxTimeout, TimeSpan sleepAfterFail, Action<Failure> onFail)
|
2024-06-04 09:06:19 +00:00
|
|
|
|
{
|
|
|
|
|
this.description = description;
|
|
|
|
|
this.task = task;
|
|
|
|
|
this.maxTimeout = maxTimeout;
|
|
|
|
|
this.sleepAfterFail = sleepAfterFail;
|
|
|
|
|
this.onFail = onFail;
|
|
|
|
|
|
|
|
|
|
tryNumber = 0;
|
|
|
|
|
tryStart = DateTime.UtcNow;
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-05 07:20:00 +00:00
|
|
|
|
public void Run()
|
2024-06-04 09:06:19 +00:00
|
|
|
|
{
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
CheckMaximums();
|
|
|
|
|
|
|
|
|
|
tryNumber++;
|
|
|
|
|
tryStart = DateTime.UtcNow;
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-06-05 07:20:00 +00:00
|
|
|
|
task();
|
|
|
|
|
return;
|
2024-06-04 09:06:19 +00:00
|
|
|
|
}
|
2024-10-09 07:11:04 +00:00
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-04 09:06:19 +00:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var failure = CaptureFailure(ex);
|
|
|
|
|
onFail(failure);
|
|
|
|
|
Time.Sleep(sleepAfterFail);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Failure CaptureFailure(Exception ex)
|
|
|
|
|
{
|
|
|
|
|
var f = new Failure(ex, DateTime.UtcNow - tryStart, tryNumber);
|
|
|
|
|
failures.Add(f);
|
|
|
|
|
return f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckMaximums()
|
|
|
|
|
{
|
|
|
|
|
if (Duration() > maxTimeout) Fail();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Fail()
|
|
|
|
|
{
|
|
|
|
|
throw new TimeoutException($"Retry '{description}' timed out after {tryNumber} tries over {Time.FormatDuration(Duration())}: {GetFailureReport}",
|
|
|
|
|
new AggregateException(failures.Select(f => f.Exception)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetFailureReport()
|
|
|
|
|
{
|
|
|
|
|
return Environment.NewLine + string.Join(Environment.NewLine, failures.Select(f => f.Describe()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private TimeSpan Duration()
|
|
|
|
|
{
|
|
|
|
|
return DateTime.UtcNow - start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Failure
|
|
|
|
|
{
|
|
|
|
|
public Failure(Exception exception, TimeSpan duration, int tryNumber)
|
|
|
|
|
{
|
|
|
|
|
Exception = exception;
|
|
|
|
|
Duration = duration;
|
|
|
|
|
TryNumber = tryNumber;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Exception Exception { get; }
|
|
|
|
|
public TimeSpan Duration { get; }
|
|
|
|
|
public int TryNumber { get; }
|
|
|
|
|
|
|
|
|
|
public string Describe()
|
|
|
|
|
{
|
|
|
|
|
return $"Try {TryNumber} failed after {Time.FormatDuration(Duration)} with exception '{Exception}'";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|