Hi, I've a scenario. Suppose we have created one job that completes execution within 20 minutes and runs on a time interval of 10 minutes, but if job takes more time (10 minutes more) in execution, then time interval should adjust automatically. Ex - Suppose a job starts at 10:00AM and completes execution at 10:20AM. Again job will start @10:30AM with the interval of 10 minutes. If Job takes 10 more minutes in execution and completes execution @10:30AM, then again job should start @10:40AM rather than @10:30AM. Interval time should be increased according to time taken in execution (It should be changed dynamically.)
This background task is something which is running at regular interval but how can we schedule multiple jobs and pause/resume them if needed with their own independent frequency/interval and monitor them through a UI?
When you have such a task running in the background, is there any straightforward way for it to communicate its status so that external processes (eg the one that spawned it) can access that status?
@J M, there are a couple of ways to achieve it. 1. is to create an in-memory queue and communicate through it. 2. is to create an event exchange and communicate through that. One thing to keep in mind is that, since they are in different threads, the code needs to be thread-safe.
@DotNet Core Central, please kindly leave a reply to this question above ... @Geverola, I would appreciate you sharing the answer to this if you have got one ...
Important to note that adding the hosted service for method 1 does not work when adding it from startup configureServices, and requires to be added through program createHostBuilder.
@Kashif Reza, if you need control over how to start and stop your service, you go with IHostedService, otherwise, BackgroundService will be the better approach. Having said that, I personally prefer IHostedService, just my personal preference.
@@DotNetCoreCentral can you please give example of this? Also, can we create dashboard or persist ongoing tasks in database and process them and see their status updated over time just like in hangfire?
@Kmn I, thanks for watching! With respect to running background jobs to access EF calls, I do not see any reason why it would be unsafe. Unless the query ran can cause potential deadlocks, in which case anyway the design has to be rethought. Let me know if it makes sense. Thanks
@noriel ablana, if you are debugging locally yes it will. But not when deployed in dev/qa/prod. Also, you can configure your Visual Studio not to stop the process on browser close.
@@DotNetCoreCentral I was just testing (in development) and i just thought that it is running even the browser closes, luckily you mentioned here about production, can you teach me how to do that in visual studio? thanks again!
Very useful and well explained. One thing I'm unclear on, why do you AddHostedService() inside Program.cs, rather than Startup.cs? As far as I can tell the method ConfigureServices() is the same.
@Akustik, you can use it in Startup, I use it there as I feel since it's a service on its own and running in its own thread, makes it clearer if it is started from the Program class. Again, there is no real technical reason, just my personal coding preference. Hope this answers your question. And thanks for watching!
Great video!! Very precise and easy to understand. Thanks for putting such a short video covering bs😃! Just heads up - Going forward in .net6, you can write whole initialization code in classless main with implied using namespace. It also allows old style of using main/startup combo
but where is code to make it reapeat after specific intervals. i want it to be a scheduled job. task.delay is just to create a delay after performing dowork. but no code to make it scehduled job. please help
@C-Sharp Tricks , if you want scheduled jobs, the better option is to use Hangfire. I have a video here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-sQyY0xvJ4-o.html
@Ashutosh Mishra , for that requirement, you should use Hangfire. I have a video here: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-sQyY0xvJ4-o.html
is it like fire and forget and do we need to do pooling to find whether the task is complete or not, please let me know if there is a way to know the job done to notify user
@@DotNetCoreCentral I'm publishing to a live webserver and as soon as the app pool's "idle timeout" property is fulfilled the background service is killed.
@@parko1965 To be honest I have not been running .NET Core in Windows and IIS for over a year now in Test or Production. I have been using out of the box kestrel web server running in a docker container for Production, and locally using self-hosted in the console. And I have never faced any issue. I think in your case, IIS is shutting down the app. I can try it out on my side and see what can be done with IIS.
@@DotNetCoreCentral I have a shared hosting solution and modification of the app pool time out is not an option. I do have a work around but it's not ideal.
@Micro Tech, I have not been using IIS/Windows for more than a few years, all of my services are now in Docker/Linux. Having said that, I know IIS shuts down appdomain if no external HTTP activity. And for that scenario, the only option is to have a periodic health check or ping to keep the server alive. I know it's not an ideal solution, but as far as I know, this is the solution that might work.
@@DotNetCoreCentral so how do you keep background services (which are hosted with .net core api/mvc) all the time active in docker/linux? Doesn't it stop afyer certain time of inactivity like iis? Thanks!
@@microtech2448 for .NET Core services running in Linux/Docker hosted in AWS they are always pinged for health check by ECS which keeps it running irrespective.