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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dotnet core central in today's video I am going to discuss about scheduling job in asp.net core using hangfire now for scheduling a job let's say if you want to schedule a job which will run every hour on every 5 minutes on 9 p.m. every day it's really tedious to do it using threats or timers hangfire is a framework which abstracts all this implementation and gives a real easy to use fluent api for configuring this kind of job using cron expression so to start that first i am going to create a new project in asp.net core and i'm going to name it as hang for demo and i'm just going to go ahead and create empty project going to keep it as dotnet core and s better of course route 1 so once the project is created first I'm going to go and add the new get packages necessary for hangfire so I'm going to add Hanford core which is the main hand for NuGet package we need after that I'm going to use the Hanford or asp.net core which supports integration with asp.net code and next thing I need is a storage provider so hangfire internally uses three main component one is a Hanford server which is responsible for going through the schedule and run one is a hang fire client which scheduled job and then the hang for storage where jobs are saved so for this example I'm just going to go with memory storage and that's probably the Hanford storage we are going to use in most of the scenarios unless we want to share a job across different processes in which case we might choose to use sequel server storage or some other storage providers so once the namespaces are added what I'm going to do is first I'm going to add the hang fire service so I'm going to do dot and hang fire and in the config I'm going to first set the compatibility mode and I'm going to set it at 170 and for serialization I'm going to have you simple assembly name type serializer and then also and I'm going to also use the default type serializer and finally I'm going to use the memory storage provider for that I am just going to add the namespace so once I do that I can use the memory storage okay once that is done then I'm going to also do and Hanks fire server this will add the server once these two is done what I'm going to do is now it's time to configure a hang fire service but before that what I'm going to do is the other feature that hang fire supports is a full-fledged administrative UI where we can monitor off the jobs and for that what I'm going to do is I'm going to use hanford dashboard once I do that the dashboard will be enabled and for scheduling a job I will add the background job client and here I can say background job client dot NQ and it takes a delegate and here I can just have simple console dot write line hello hang for a job so this is the first job that I'm creating this is not too fancy I'll get into creating recurring job but let's just start with the simple job created through hangfire background job client so once I do that once I have everything set up I'm going to run this application and once the application is started in the console you can see that it is printing out all the information about the hand file so starting in first server it has a worker count of 20 these are the workers which are responsible for scheduling and running jobs and then here you can see that it has printed out the one time job that I created which is hello hang fire job it's just doing a printout now if I go to the web URL and if I type in hand here this is the Hanford dashboard and he's showing the real-time graph and if I go to the jobs I can see that there was one job which was succeeded and this job is the console dot write line statement that I printed out and currently there is no recurring job so once that is done now I'm going to create a recurring job because that's more interesting and that's the case where you know we'll be using hand for most of the time creating just an Q&A job I mean we can just do a staska for achieving the same thing so let's show I recurring job manager and in the instance of recurring job manager what we can do is we can do an add or update and we can give the job a name so let's say run every minute and then the actual job and the job can be again a console dot write line and just test recurring job and then finally the cron expression and for that I can just give the five stars which means it is going to run every minute now let me start the demo once again now this time you can see it is started as expected and we can see the scheduled job the normal scheduled job is printing out hello hang for job and in a minute we are going to see the recurring job showing up but in the meantime we can go back to the hang fire and here now we can see that in recurring job we have our ID the name that we passed that is run every minute the cron expression and then console dot right and next execution in a few seconds so let's go back to the console and in a few seconds we should see the console dot right showing up and now if you go back to jobs we can see that there is one succeeded and we can see two right now immediately just few seconds before the test recurring job has occurred so we see one execution of the job and our recurring job is still running and it should now it is showing the last execution time so so this is a very basic example of how to use hangfire all we had to do is add the new get package for hang fake or hang fire dot ASP not code and hang fire dot memory storage and after that it just literally few lines of code to set up the hang fire Enron job every minute and now it has executed the second time if we go back here we can see it has executed three times and in the recurring job also it is running as expected and the next obvious question is can we use it as part of the dependency injection as well yes we can since the anchor itself is added into the service collection which means both the recurring job manager and background job client are available from the DI and this is the this is the reason why as soon as I added these two to the configure method the objects were passed along into this function by the SPF nerdcore framework because it could pick it up from the services collection but to demonstrate that what I'm going to do is instead of doing a console dot ride I'm going to just create a new class and an interface and I'm going to name it as print job it's a simple class it'll just have a public method print and in the print it will do the same thing it will just do console dot write line it'll say and for recurring job and here I'm going to extract an interface I print job and once I do that I'm going to go into the startup in here I'm going to say services dot add at singleton and here I'll say I print job and print job is the instance and here what I'm going to do is instead of console dot write line so here another thing I'll do is I'll also add the service provider and instead of doing a console dot write line I'm going to do service provider dot get service I print job dot print okay so now you can see that once I run this I should be able to access the I print job so essentially I can completely now the business logic can decide inside the print job class and it can be completely tested out whereas the schedule can be completely isolated from the actual logic of printing because we are not going to test if hang Faris running every minute because it is supposed to we just want to make sure a logic is testable and it can be achieved through this mechanism so I'm just going to run it again you can see the default message is printed out and now if I go to the hangfire dashboard now I can see the jobs the recurring job is set up to run print job dot print method and if I go to the jobs I can see one succeeded which is the default one and next in few more seconds we should see that the we should see that the recurring job is printing out and here we see that the hangfire recurring job print out method came from the print and here we can see it is a success now and this is a recurring job last execution few seconds back it shows what time it got executed so this is all I had to cover today with respect to hang fer there are definitely the lot of other features of hangfire which are pretty interesting but this is something which I found to be extremely useful especially when we want to create recurring jobs it's super simple as you can see it's just a matter of four or five lines of code to set it up and the dashboard is really really powerful and if we have multiple we can do things like trigger now if we do that it will trigger the job we can delete a job we can do a lot of things building job is going to delete from in memory storage for the time being and it's not going to trigger anymore so let me just go ahead and that okay there is no reckoning jobs and we have three total printed out and now even if we continue running this application we're not going to see any more printer because the job is been deleted from the UI so this is very handy if we have to delete a job and someone did them so this all I had to cover today if you like this video please give me a thumbs up and if you have not subscribed to my channel and you have been getting real value out of my channel please subscribe to my channel thank you so much
Info
Channel: DotNet Core Central
Views: 53,618
Rating: undefined out of 5
Keywords: hangfire, hangfire.io, .net core job scheduling, asp.net core job scheduling, schedule job asp.net core, schedule job .net core, c# job schedule, background job schedule .net core
Id: sQyY0xvJ4-o
Channel Id: undefined
Length: 14min 16sec (856 seconds)
Published: Mon Mar 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.