Тёмный

Scheduling recurring jobs with Hangfire (In ASP.Net Core 3.1) 

DotNet Core Central
Подписаться 27 тыс.
Просмотров 54 тыс.
50% 1

Hangfire provides a way to create and manage scheduled jobs. Creating scheduled jobs to run at a particular interval is always tedious. And also error-prone. Hangfire encapsulates all these and provides very easy to use API for configuring jobs.
In the video, I will walk through creating a ASP.Net application, which will use Hangfire to manage recurring jobs.
The three main Nuget packages needed for hangfire are:
1. Hangfire.Core
2. Hangfire.AspNetCore
3. Hangfire.MemoryStorage
Hangfire has three main components:
1. Hangfire Server - The server is responsible for fetching the job data from the job storage and execute them. The server is also responsible for keeping the job storage clean and removing old jobs.
2. Hangfire Client - The client is responsible for creating jobs and saving them in the job storage.
3. Job Storage - Hangfire keeps the jobs and related information stored in persistent storage. Persistent storage helps survive a job in case the process is restarted. In this video, I am going to use MemoryStorage, which of course will its data once the process is restarted. But til will work for the example to show how Hangfire is used.
Github location: github.com/choudhurynirjhar/h...

Наука

Опубликовано:

 

21 июл 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 100   
@kunalkaran9655
@kunalkaran9655 3 года назад
Extremely helpful video. To the point and most importantly easy to understand and implement. Thanks man.
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Kunal Karan, thank you for watching the video!
@themt.oceans
@themt.oceans Год назад
Great explanation in a very short time, thanks!
@DotNetCoreCentral
@DotNetCoreCentral Год назад
Thanks for watching!
@samuelkoroh963
@samuelkoroh963 4 года назад
Nice one, I would love you to do a video on running recurring jobs for each tenant in a multitenant based aspnet core API application
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
Samuel Koroh thanks for your valuable feedback. I’ll do that in my next video.
@rohitjagannath5331
@rohitjagannath5331 Год назад
So easy to follow and understand over the simple explaination provided on this video. Thanks for this video content.
@DotNetCoreCentral
@DotNetCoreCentral Год назад
You're very welcome!
@robinjohansson6604
@robinjohansson6604 3 года назад
Really good video, worked like charm!
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Robin Johansson, thanks for watching!
@Sygmond
@Sygmond 2 года назад
Great content and nicely done! I wanted to find out what Hangfire is and you helped me understand that. Thanks!
@DotNetCoreCentral
@DotNetCoreCentral 2 года назад
@Sygmond, thanks for watching!
@AjithChanaka
@AjithChanaka 3 года назад
The best teaching. Thank you.
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Ajith Chanaka, thanks for watching!
@migueljimenez970
@migueljimenez970 2 года назад
You are simply the best!
@DotNetCoreCentral
@DotNetCoreCentral 2 года назад
Thanks!
@bharatbj5042
@bharatbj5042 3 года назад
Thanks buddy you have done great job keep doing god bless you
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@bharat bj, thanks for watching!
@karimmessaoud8223
@karimmessaoud8223 4 года назад
Thank you! I found it useful.
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
@Karim Messaoud Thanks for watching!
@varahalababu6057
@varahalababu6057 2 года назад
Nice Content, could you please do the video for hangfire running recurring jobs for each tenant with multi tenant in .Net Framework
@RayCarneiro
@RayCarneiro 4 года назад
I have used Hangfire with SQL Server as RDBMS but now I need to configure it to work with Oracle. I'm doing some researches about it. Did anybody successfully used Hangfire with Oracle before?
@nahidroozbeh5948
@nahidroozbeh5948 4 года назад
Thanks, it was really useful
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
@Nahid Roozbeh thanks for watching!
@LuigiZambetti
@LuigiZambetti 2 года назад
Could you please add a video also for the case of a ASP.NET Web Forms application?
@arsman_ahmad
@arsman_ahmad 3 года назад
thank you for this useful video.
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Arsman Ahmad, thanks for watching!
@chanakyasidharthan4478
@chanakyasidharthan4478 Год назад
Nice information by The Way
@DotNetCoreCentral
@DotNetCoreCentral Год назад
Thanks for watching!
@clint2627
@clint2627 2 года назад
Can you set a different cron schedule per environment (dev,uat,prod)? Can hang fire make it so the task only runs on one pod in kubernetes ?
@cybernarokable
@cybernarokable 3 года назад
You're my hero!
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Supakit Inkeaw, thanks for watching!
@michaelcandler3821
@michaelcandler3821 3 года назад
Thanks, this really helped me a lot! One question - my recurring job is a daily POST request to a HTTPClient. Should that be done with AddScoped, AddTransient or AddSingleton?
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Michael Candler, the job can be Transient in that case.
@michaelcandler3821
@michaelcandler3821 3 года назад
@@DotNetCoreCentral thanks mate!
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@@michaelcandler3821 Welcome!
@mrwhammer
@mrwhammer 2 года назад
how do you make it reocurring at a specific time of day like 2:00 pm?
@lakshminaidu3123
@lakshminaidu3123 Год назад
How to dynamic the cornexpression from DB, please help to resolve that?
@nicholash8021
@nicholash8021 4 года назад
Thanks for the tutorial, but I found a few things very confusing: Where exactly is the job running from? It seems that your application is only scheduling the job but somehow magically Hanfire is executing it, but from where? Or maybe Hanfgire is only calling back into my program to trigger my local function to execute--which would be basically useless if I want to schedule jobs to be run without my program itself waiting around. At first I thought Hangire was something that could schedule a job to run on a remote machine and my program would exit immediately after scheduling the job. However, it seems you are showing that your program which scheduled the job must stay running. If so, I can't rely on Hangfire to start the job if my program has already exited. I would have to restart my program and check to make sure I'm not re-scheduling something that is already scheduled, then wait around for Hangfire to finally trigger the logic so that my program can receive the request. That seems overly complicated and easy to cause accidental dupicate jobs being run. So, again, my point is that some of these fundamental concepts should be discussed because to someone new hangfire, these concepts are not clear.
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
Nicholas H as you have pointed out, hangfire is used for scheduling jobs inside of an application which is also hosting hangfire. It is not an alternative for distributed job scheduling engine like Tivoli, Tidal or even Microsoft task scheduler. It is mainly for scheduling tasks inside of an application by an in memory scheduler. Which we usually implement using custom code using Timer or threads. Hangfire simplifies this and provides a very nice dashboard to monitor and adhoc trigger those jobs itself. I will provide comments in my video to clarify this. Thanks for your question, it’s extremely valuable. I’ll also try to create a video comparing hangfire with one of the enterprise scheduler. Maybe that will help as well. Thanks again for watching the video and providing your feedback.
@soft.developer
@soft.developer 7 месяцев назад
Bro...Very good video. How can i get NextExecution from a schedule job? I have tried, but from what i find on internet i need the Cron property but in my shedules object i dont have this property
@nowzarifarhad
@nowzarifarhad 3 года назад
This is the easy part my friend, what is your solution for deployment on IIS. IIS has a default idle timeout which I prefer not to touch because the application needs to free the resources one in a while. I know that you can make IIS to start it automatically but Does IIS kill the app when the job is running I think so. Because it does not care about the background tasks as procedures to keep the app alive.
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Farhad Nowzari, I have not used IIS for more than 3 years now, the economy does not work out using Windows with IIS in the cloud. I use Docker container with ECS/Linux and Kestral as Web Server.
@im-nazmul
@im-nazmul 2 года назад
Hi, I'm using dot net 6. I don't Startup class and I'm doing everything on Program class. So how can I use IBackgroundJobClient on there?
@mouayadkhashfeh4967
@mouayadkhashfeh4967 3 года назад
Thanks, it is really useful, I have just one doubt, please correct me if I am wrong Looks like HangFire doesn't consume PrintJob from the container, because every time the recurring job run, it initializes a new instance from PrintJob even though it has been injected as a singleton
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@mouayad khashfeh, it should be injected from the container, if not I might have a bug somewhere in the Startup class. I will take a look into it.
@mouayadkhashfeh4967
@mouayadkhashfeh4967 3 года назад
@@DotNetCoreCentral I used the same steps in one of my applications, and added a breakpoint in the service class constructor which I injected as singleton, but and I noticed that in every hangfire round it calls the constructor again, I tried the solutions I found online, but non of them were using Singleton service, they were talking about something called ASPnetcoreJobActivator Thanks for your help 😊
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@@mouayadkhashfeh4967 thanks for bringing this up. I have multiple production application running with Hangfire with the assumption that singleton works :( I will have to get to the bottom of this.
@vijayande2009
@vijayande2009 3 года назад
That was a great introduction to Hangfire. I have a scenario where I want to schedule a job at specific Time set by different Users. Is it possible to schedule a job for each user as per the time set by the user using Hangfire and then leave it to Hanfire to trigger the job as per the user's set time for each user. Please advice. Thanks.
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Vijay Ande, so far what I have done, I do not see that is doable. I will research, and if I find something I will let you know. But as far as what I know right now, I do not think it is possible out of box.
@vijayande2009
@vijayande2009 3 года назад
@@DotNetCoreCentral thanks, I got it you can do it using IBackgroundJobClient.Schedule() method. Now my question is can I use "Open plan or Subscription" of Hangfire to Schedule 100+ or even more jobs in a day.
@morsheddld
@morsheddld 2 года назад
Where does this IPrintJob interface come from?
@kd150381
@kd150381 Год назад
I am facing issue with Recurring Job. I have schedule to run my job at 1 am PST. It executed at specified time but some time it runs at odd times. I am not able to figure out the reason
@raj60921
@raj60921 4 года назад
Hi, i am using mongodb for storage of hangfire jobs. everything works fine on local but when deployed on a container hangfire dashboard got broken it seems css got broken, do you any idea about that ?
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
@raj yadav I will try with my implementation on sql server to run on a container as see. To be honest I’ve multiple apps on hangfire which uses in-memory storage in production, running in docker on Linux server. Have not seen this issue before.
@dintots
@dintots 3 года назад
Hi, how do you prevent hangfire to trigger multiple times? I have recurring jobs but I notice it triggers twice sometimes which is wrong. Thanks!
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@dintots, thanks for watching! I have been using Hangfire in production for more than a couple of years for multiple applications, never seen this issue before. Can you share more details around how you implemented this?
@coregerror4519
@coregerror4519 3 года назад
Nice article
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@CoReg Error, thanks for watching!
@bucketnick
@bucketnick 4 года назад
love it
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
@bucket lee thanks!
@SaiKumar-gp3kb
@SaiKumar-gp3kb Год назад
Does hangfire support kerberos authentication?
@nasirhussain1982
@nasirhussain1982 5 месяцев назад
but this job is stop at mid night next day we have to re-run
@ajayverma-cv8pj
@ajayverma-cv8pj 3 года назад
can we do this using by creating class and implementing it to IHostedService ? instead of hangfire?
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@ajay verma, yes you can, but in that case, you have to manage all the jobs and also will not get the handy UI that comes out of the box.
@bharatbj5042
@bharatbj5042 3 года назад
[AutomaticRetry(Attempts = 0, OnAttemptsExceeded = AttemptsExceededAction.Delete)] I am manually throw the error and I am trying to stop atomatic retry but its not working can you help me on this
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@bharat bj, can you share the code you are trying to do, maybe as a gist in GitHub? I can take a look.
@abhaysrivastava3642
@abhaysrivastava3642 9 месяцев назад
How to avoid jobs that are missed while server is not up. Actually when server is waking up , it is running all missed jobs , that we would not want. Please provide solution to that also.
@praadiiit
@praadiiit 3 года назад
Can you use single hangfire server with multiple projects/codebase? Is hangfire only support for monolitical structure?
@praadiiit
@praadiiit 3 года назад
Hope someone can reply this 🙏
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Aditya Pratama, you can have a single hangfire for multiple projects/applications. But in that case, if the jobs are in other processes, you will need inter-process communication to trigger jobs in other processes. You can do that using REST API's, but this is something to consiider.
@praadiiit
@praadiiit 3 года назад
@@DotNetCoreCentral thank you fo replying, i need a cron for microservice structure with netcore as a base language. Can you tell me what best practices on that case? Or maybe an alternative for hangfire? Sorry if my english is bad 😬
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@@praadiiit are you using any cloud infra? if you are using something like AWS you can use EventBridge and trigger cron jobs in your web API from there.
@gustavodossantos6397
@gustavodossantos6397 3 года назад
I am having some inssue, just the con expression "* * * * *" is working, any other one is ignorated, do you know why?
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Gustavo dos Santos, I have used this in so many projects, have never faced it. Can you share the CRON expression, I can try it out in my Github repo and let you know.
@ankitshah2270
@ankitshah2270 3 года назад
Is it possible to verify if selected job is still running or not when user click on "Trigger Now" button?
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Ankit Shah , yes the UI gives this capability.
@wisnu7734
@wisnu7734 2 года назад
Why creating job must placed in middleware ???...
@Tymonello
@Tymonello Год назад
Can you create jobs from the HangfireDashboard, or only from code?
@DotNetCoreCentral
@DotNetCoreCentral Год назад
You can create from the dashboard
@miguelgonzalezlugo165
@miguelgonzalezlugo165 2 года назад
how do you deal with a class like your PrintJob that has dependencies? I am having issues with hangfire 1.7.24 :( any help?
@DotNetCoreCentral
@DotNetCoreCentral 2 года назад
@Miguel Gonzalez Lugo, what error you are getting?
@miguelgonzalezlugo165
@miguelgonzalezlugo165 2 года назад
@@DotNetCoreCentral System.TypeLoadException, when recurrent job tries to execute method from the type of service declared in AddOrUpdate
@MiceDevelopment
@MiceDevelopment 2 года назад
hai may i know how do you setup the application to always running when using hangfire in the web app?
@DotNetCoreCentral
@DotNetCoreCentral 2 года назад
@ Iqmal Miz, the hangfire jobs are used for scheduling a recurring CRON job, if you need something to run continuously, than Hangfire is not the right tool for that. You should use IHostedService/BackgroundService for that. ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-1Fe7QD7Ovi8.html
@MiceDevelopment
@MiceDevelopment 2 года назад
@@DotNetCoreCentral im using it to schedule some job that based on users setting when they want it to run but the iis kills the web app if got no request for period of time So the jobs won't execute. I saw the article for production deployment but got no idea how to implement it in .net since the doc is for asp.net framework i guess
@DotNetCoreCentral
@DotNetCoreCentral 2 года назад
@@MiceDevelopment I have not used IIS in over 3 years. But if you need to keep IIS alive one option is to make a ping every 60 seconds or so.
@rulonoboyev2939
@rulonoboyev2939 4 года назад
Where is job interval a minute set?
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
Job interval is a CRON expression in the Startup class, you can set it to anything you want.
@ju456one4
@ju456one4 4 года назад
Are you Indian? Your english is perfect dude.
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
@Ju456One If it’s a complement, then thanks!
@andersborum9267
@andersborum9267 4 года назад
Thanks for the video, but you need to work around breathing down the microphone; it's really not beneficial!
@DotNetCoreCentral
@DotNetCoreCentral 4 года назад
@Anders Borum thanks for the feedback, I’ll work on it.
@tapstach2872
@tapstach2872 3 года назад
You sound hungry
@DotNetCoreCentral
@DotNetCoreCentral 3 года назад
@Taps Tach, I was probably, don't remember :) Thanks for watching!
Далее
Scaling HANGFIRE: Processing More Jobs Concurrently
6:52
Hangfire + MediatR = Message Dispatcher
10:56
Просмотров 21 тыс.
10 C# Libraries To Save You Time And Energy
33:59
Просмотров 205 тыс.