New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BackgroundService on a timer: TimedBackgroundService #36383
Comments
|
/cc @glennc @davidfowl This is something we've been thinking about for the future. If we provided a simple base class that's a singleton service that was called on an interval you choose, is that what you want? I think it would be possible for you to test-drive something like this through a sample until we have it built in. We'd probably also do things like include logging and error handling by default. Are there other features you'd expect this to have? |
|
When you mention logging and error handling, do you mean that the sample will include these or the Assumptions: (Please bear in mind I haven't read much about hosted services yet)
A couple new features I can come up with, are:
Thanks for your quick answer! It's exciting to see you guys already looked into this :) |
|
We'll look at this in 3.0. No guarantees, but it seems like a useful thing to add. |
|
Any updates? |
|
I'm looking for a solution as well. It should be as easy as setting a TimerTrigger in Azure Functions. |
|
You could also look into a WebJob for now if you host it in Azure, if you feel more comfortable with that. Otherwise, look at my own solution for this issue in this StackOverflow issue; please keep the comments in mind that bring good feedback; it's possible that the solution can be optimized or that it might not meet requirements. |
|
Any updates? |
|
@sertunc not at this time. We're doing planning for 5.0 and will consider this during that process, but there's no guarantee as it will be prioritized against all the other work on our queue. |
|
Your solution is here |
|
Someone pointed out in my stackoverflow post that using Your link does have a good point though! In that person's post they lock the jobs so it waits until the current one is finished. That would be useful/mandatory as well if it is included as an extension in the net core repo. @anurse Any updates? I expect it not to launch with .NET 5, but I'm curious to know what you think of adding a monitor/lock system and to know if this will ever be added? |
|
@sander1095 Indeed using Monotor.TryEnter will prevent other threads to enter the sub DoWork(), Maybe instead of using the void DoWork() we may use async void DoWork() and use SemaphoreSlim, Therefore instead of : We will have: SemaphoreSlim _sync = new SemaphoreSlim(1); private async void DoWork(object state) What do you think? |
|
I am not that experienced with Also, your code is not formatted and rather hard to read. I would ask one of the dot net experts to look more into this :) |
|
The next step for this would be to prepare APIs and samples from guideline: https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md |
|
Sinds the ~2+ years that I posted this I have encountered this issue a few times. I believe that my previous suggestion is insufficient, this might need more work:
I sadly don't have the time or knowledge to really help out with this feature, so I'd like someone else to make some proposals. I am also curious what the status of this story is; are there any plans for it? |
Is your feature request related to a problem? Please describe.
I discovered ASP.NET Core 2.1 has a
BackgroundServicethat can be easily used for long running async tasks. It sadly does not have the option to run it on a timer which could be very handy since we are creating an abstraction forIHostedServiceanyway.I could remember the last time the method ran and then, if x time passed, run the method again (thus creating my own timer), but that would have to run in
ExecuteAsync()which feels odd because even thoughExecuteAsyncis called, the method can't guarantee the core functionality is actually executed. I also do not know the pattern ofExecuteAsynccalls which could result in the timer not being useful because it would depend onExecuteAsyncbeing called consistently to make sure there is as little delay as possible when actually executing the task, but this could also be because of my new knowledge of this class.Describe the solution you'd like
I'd like to have a new abstraction for
IHostedServiceorBackgroundServicecalledTimedBackgroundService. This could have a constructor argument/configuration argument that has a cron expression to make it run on a timer. Maybe even an attribute?Describe alternatives you've considered
It could be possible to add a cronjob expression to
services.AddHostedService()to decide there when a backgroundservice will run.Additional context
None.
The text was updated successfully, but these errors were encountered: