2

I wanted to create a background job in which I want to use the retry mechanism for that I want to use the Polly. My requirement is like this:

  1. the First retry should be done after 2 hours
  2. the Second retry should be done after 4 (2^2) hours
  3. the Third retry should be done after 8 (2^3) hours.

In short, I want to retry after exponential hours timings. To implement this I have 3 ways:

Create an iHostedService which will do retry

Assume that I want to post orders to Amazone S3. I got a queue of 50 orders, In which some orders (assume 40 orders) are retried the first time, 6 orders are retried 2nd time, 3 orders retried 3rd time and 1 order retried 4 times on the specific period of time. As per my knowledge, it is not possible to run the iHostedService in exponential timings. If any please let me know. Assume that it is possible then orders have many states, like some orders to be retried 1st time, some are 2nd time and so, on. So, in this case, orders which are retried to be the first time should be executed after 2 hours, orders with 2nd retry should be after 4 hours, etc. My question is in case we find a way to do it how can we accomplish that goal?. Assume that I am going to run the iHostedService service after x number of hours using CRON job.

Create a separate windows service

In this approach, everything is the same as the per above approach, but instead of creating iHostedService, I will create a separate windows service. Here, after some time, things that I have to check that the windows service is running in the background.

Integrate Polly in the Startup of the HttpService

With the help of this. And thinking of setup the retry timeout in the hours. But I am afraid with the internal mechanism of the same. Like if I set it like that, will it cause any performance issues on the production server? I am assuming that to achieve the goal, its thread/process continuously running on the server and utilizes the server resources.

I need your help to define the approach which is best for me? which is provide the best performance by utilizing lesser server resources. Above is my research, you can suggest any other brand new ways too. Which can be helpful for me.

2
  • Have you tried running Polly in a hosted service, while having it configured for exponential retry times? This question is heavy on assumption and pre-emptive worry, and light on concrete implementation and concrete issues.
    – Flater
    Commented May 3, 2021 at 12:23
  • Yes, it is because in order to implement all the things will take huge time. Also tried to search for articles regarding the same like the performance of iHostedService vs Windows service but don't find any satisfactory articles. Commented May 3, 2021 at 16:04

2 Answers 2

1

Create a separate windows service (or console app managed by linux systemd)

The problem with IHostedService is that it runs as part of a web app. You don't need that and the problems it brings.

A simple app can run forever, you can add in whatever await Task.Delay() you like and have windows services/systemd handle restarting if it crashes.

Since your retry is relatively long at 8 hours, I would persist this in a simple database or file so that if the app does crash, or the computer needs restarting due to updates or whatever. The app can reload the waited for triggers when it restarts

6
  • 2
    "The problem with IHostedService is that it runs as part of a web app." But that's not true. var host = new HostBuilder().Build().RunAsync();
    – catfood
    Commented May 3, 2021 at 13:02
  • Well yes i guess you can make a background task with no foreground task these days. I would still simply avoid it.
    – Ewan
    Commented May 3, 2021 at 14:13
  • @catfood: Just because it can be done doesn't make it a good idea. A web process could also run a CLI command and start virtually any application on the local machine, but that doesn't make it a good solution.
    – Flater
    Commented May 3, 2021 at 16:07
  • 1
    you are correct. Its just do you want to use something that's been designed to do background tasks in a website (and had issues) then had the underlying webhost generalized out from under it when you can write a simple app with none of the framework that works better
    – Ewan
    Commented May 3, 2021 at 21:52
  • 1
    The Generic Host is literally purpose-built to handle background tasks... Commented May 22, 2021 at 17:38
0

I recommend using IHostedService with Polly in a console app, then scheduling that app to run (once) on system startup, using Windows Task Scheduler. (You are on Windows, right?)

https://nodogmablog.bryanhogan.net/2018/07/polly-httpclientfactory-and-the-policy-registry-in-a-console-application/ has more information about how to do this. One caveat I would add, though: Instead of using exact, exponential retry times, try using Polly.Contrib.WaitAndRetry.Backoff.DecorrelatedJitterBackoffV2 . I expect you could use a medianFirstRetryDelay value of TimeSpan.FromHours(2), and retryCount of 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.