I am trying to use polly to construct a policy that can retry exponentially for n tries and then switch to retrying every 1 hour. Can this be achieved?
I tried policy Wrap but did not get the desired results
I am trying to use polly to construct a policy that can retry exponentially for n tries and then switch to retrying every 1 hour. Can this be achieved?
I tried policy Wrap but did not get the desired results
On
I might be late to the party but let me share my thoughts:
construct a policy that can retry exponentially for n tries Here is an example how you can construct
var retryWithBackoff = Policy
.Handle<Exception>()
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(TimeSpan.FromSeconds(1), n));
DecorrelatedJitterBackoffV2
medianFirstRetryDelay parameter for your needsretrying every 1 hour
var retryForeverWithAnHourDelay = Policy
.Handle<Exception>()
.WaitAndRetryForeverAsync(_ => TimeSpan.FromHours(1));
I tried policy Wrap
var combinedRetryPolicy = Policy.WrapAsync(retryForeverWithAnHourDelay, retryWithBackoff);
retryWithBackoff did not success then try retryForeverWithAnHourDelayHere you can find a simple dotnet fiddle app which demonstrates this at work:
Even though it is feasible to sleep for an hour with Polly, it is not suggested. Polly's retry was primarily designed to overcome transient failures. These failures should vanish/self-heal in minutes (not in hours or days).
If you need to wait that long I would rather suggest to use some sort of job scheduler like Quartz.NET or Hangfire.
This should be possible to achieve with overloads accepting custom
sleepDurationProvider. For example something like the following: