I have a call that uses a Policy.Handle<PollingResult>(...).WaitAndRetry(...).ExecuteAsync(...)
call to wrap a polling operation.
Having spent the better part of half an hour reading the Polly documentation, I cannot seem to find any way for this call to tell me "Whoops, seems we used all the retries to no avail!"
Like, how do I do this:
var retryResultWithAllDetails = Policy
.Handle<PollingResult>(poll => poll.IsReady)
.WaitAndRetry(maxRetries, n => RetryIntervalCalculation(n))
.ExecuteAsyncWithAllTheNecessaryNittyGrittyDetailsAndFootnotes(() => SomePollingOperation());
if (retryResultWithAllDetails.RanOutOfRetryAttempts)
throw new WeRanOutOfRetryAttemptsException("Oops!")
It seems strange that this isn't just readily avialable as a boolean flag somewhere in the documentation.
I am not interested in the exceptions or whether it failed or whatnot — I know how to handle exceptions thrown from within Polly. I am interested in whether it ran out of retry attempts so I can log an appropriate error. I hope that is clear.
There is no built-in support for this. But you can do something like this:
var retry = Policy
.Handle<PollingResult>(poll => poll.IsReady)
.WaitAndRetry(maxRetries, n => RetryIntervalCalculation(n));
int retryAttempts = 0;
result = retry.ExecuteAsyncWithAllTheNecessaryNittyGrittyDetailsAndFootnotes(
() => { retryAttempts++; SomePollingOperation() });
if (retryAttempts == maxRetries + 1)
throw new WeRanOutOfRetryAttemptsException("Oops!")
retryAttempts
is incremented at every retrymaxRetries + 1
the plus one is needed because the 0th retry attempt is the original execution.Dotnet fiddle: https://dotnetfiddle.net/HYyxLs