Introduction to Hangfire - Easy Background Jobs | ASP.NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is felipe avilan and in this video we are going to give an introduction to home fire hand fire is a tool that allows us to work easily with background jobs for free hanfire allows us to fire jobs in the background fire delay job in the background which means that those jobs will not execute immediately but only after some time like after 5 minutes after an hour after a day or something like that fire recurring job a recurring job is a job that will execute several times periodically like for example every five minutes every hour daily weekly every wednesday or whatever another thing that it allows us is to use continuations with continuations we can have a job and then after that job's finished then we can execute another job so we can have some sort of chain of jobs that executes one after the other and also for free again it offers us a dashboard in which we can see the jobs we can cancel the jobs delete the jobs and etc and i say for free because there is a pro version for hang fire which allows you to for example do batches of jobs but we are not going to see that in this video we are only going to focus on the three fishers of home fire so why is hand fire so cool well in order to understand that we have to learn about the architecture of home fire so let's take a look so this is the basic architecture of home fire we have a hand fire client which is the one that creates the jobs we have a jobs repository which is the place in which that jobs get stored the idea is that then we have some sort of repository or database or whatever what you want to call it we have this place in which we are going to a store that jobs so that later they can be retrieved by a home fire server so we have the client that is the one that creates the job and after the client creates the job then it immediately returns to the client so that the client doesn't have to wait for the job to be executed then we have a home fire server a hamfire server is the application that actually executes the jobs so the hamfire server is going to connect to the database or repository and then the repository is going to return the pending jobs and then the server can run those jobs this is very powerful because it allows us to have the coupled the client that is the one that creates the jobs and the server which is the one that executes the jobs so in real life this hang fire client could be something like an sp.net code application maybe a blazer application a web api or maybe a wpf or winfors application it doesn't matter as long as it is an application that can communicate with the jobs repository and then we have a server which can be again another application that is going to connect to the database or to the repository in general and it is going to retrieve those jobs and it is going to execute them so this home fire server could be something like an spd.net application but also could be a worker service a windows service a console application or something like that something else that we can do is that we can have both the client and the server in the same application which is what we are going to do in this video in this video we are going to create an expert network application that is going to be both the client and the server which means that it is going to create the jobs but also using a background process it is going to execute those jobs and the jobs repository is going to be a sql server database though we are not limited to using this technology because for example have fire can also work with redis for example but in our case we're going to use sql server so let's see our application alright so here we are in visual studio we have an espira net core web api application this is a really simple application which has a single entity i have configured entity framework core as you can see here and we have our people db set and we have a simple people controller which has this operation so again a really simple application by the way if you want to follow along in the description of this video you will find a link to github that has two folders a before and after and in the before folder we have this application like this which is the one that we're going to work on so let's get started so first what we're going to do is that we're going to install home fire so in order to install home fire i will go to the solution explorer i will right click on my project and i will say manage nuget packages i will go to browse and i will write here hand fire and by using this hand fire package i get access to the ham fire core hand fire for sequel server and hand fire for a spiral net core so let me install this package by the way this is version 1.7.28 so this is the version that we're going to use in this tutorial now after that we're going to get this readme but i don't care about it so i will just close it and also close the nougat page now i will go to my upselling.development.json file because it is advised for us to come here to login and include a hand fire filter so let me say here information so that we get in the console or in whatever place we lock our messages the information about home fire also if i want to i can have a connection stream dedicated to a hand fire db i can do that if i want to i won't but i can certainly do that in my case i will use the same database that i have here for my application of course in your case if you want to follow along you have to maybe update this in order to have a valid server but in my case this is enough now that we have installed home fire we have to configure it so let me go to the solution explorer i want to go to the program class and in here we have to say builder services at hang fire control dot using hand fire and this is for adding the ham fire client now there is some default configuration that is advice for us to use that comes from the documentation of hum fire but it is a little bit long so what i will do is that i will just copy and paste this code so let me copy and paste this again this comes from the official home fire documentation this is the default configuration that they advise us to use you can see here that we have these sql server storage options which i can bring the name space off and we also need the connection string so let me get that right away so connection string builder configuration get connection string and here i will put the name of my connection string in my case it is default connection semicolon here and now this compiles now this is for configuring the home fire client the one that is going to create the jobs now as i mentioned before we are also going to use this application as a home fire server therefore i will come here and i will say builder services at hand fire server and i will use a default configuration for this one also now finally i have to configure the dashboard because through the dashboard we are going to be able to see the jobs cancel them and so on so let me say here after use authorization i will say app dot use hand fire dashboard and that's it now if we execute our application we're going to see that in our database we are going to get the home fire tables let's go to sql server management studio here we have our intro to home fire database i can refresh here and you're going to see that now we have home fire counter hand fire job and so on in this hamfire job table we can see all of the jobs that we have let's see that first let me come here because i want to show you something i want to show you that dashboard that i just mentioned let me say duplicate let's come here and let me write hang fire and you're going to see that we have this dashboard in which we can visualize our jobs as you can see here we have jobs we have enqueued jobs scheduled jobs processing succeeded failed deleted and awaiting we also have retries we have recurring jobs and we have servers so this is great now obviously there is nothing here because we haven't fired our first job so let's get to work on that let me come back to visual studio and let's do our first example let's come to people controller and let's see that here we have this operation that it is kind of a slow it is a slow because it has this task delay five seconds but the point is that we have a slow operation and maybe we want to run it in the background so that the user doesn't have to wait for it so we can do that with hand fire very easily so what we're going to do first is that we are going to come here and we are going to inject i background job client control dot to import the home fire namespace background job client and i will assign it as a field let me put this here so that you can visualize everything and before doing this example i will do a really simple example so that we can get used to using hand fire so let me say here background job client dot nq this enqueue function is the one that allows me to fire something in the background and basically forget about it so this will run a job in the background and i will be able to continue the execution of this method let's see that let me come here we have to pass a lambda expression so i can say something like this and here i can do whatever i want for example i can say console write line and i will just pass the person name so person name i wanted to say person name alright semicolon here and that's it with this we have our first example let me save let me click on always rebuild and rebuild and apply changes so that every time we save our application it reveals so let me come here now let's come here now see something see that here in succeeded we don't have anything right so now let me go to swagger let me click on here try it out and i will write a name here let me just write my name felipe execute and we're going to see that here in succeeded we have a1 if we click on here we're going to see that now we have the job that we just fired using the enqueue function and we can click on it and we're going to see something very interesting and it is that we're going to see the code that was executed inside of the job here we have console.writeline felipe which is exactly what we have here console.writeline personname but personname has a value felipe and therefore that is why we have felipe here and of course if we go to the console let me come to the console you are going to see that we have felipe here and not only that see that we have added the person felipe here right so if we go back here and we're going to see that we have this requeue button this requeue button will allow me to re run this method that we have here let's see that let me put this here i want to do this just so that you can see the console while i click on this requeue button so let me click and as you can see we have felipe here i can click on it several times and then that will rerun the job and therefore rerun this functionality this function this code that we have here which is awesome because it means that from this dashboard we can rerun jobs at our wheel but how does this works well remember that i said that here in this job table we have our jobs so let's take a look select top 1000 and we're going to see that indeed what we have here is a invocation data field and in this invocation data field we have system console rightline and then in arguments we have felipe so what is going on here is that hamfire saves the information of the job serialized into json in our job table and then from the server i am able to load this information and then run it using simple c sharp code so that is why i am able to rerun this function from the ui because this is just a record in a database and therefore i can just load it and run it and of course if we make another job we're going to get another row in this table let's see that let me come here let me come to swagger ui let me say claudia execute let's come back here in succeeded now we have another line which is console by line for claudia and see that we have job id number two and indeed if we come back here and i press f5 we're going to see that we have job id equal to 2 and the argument is claudia this time therefore we can see that we have all of the jobs here in this job table now that was a basic example of enqueue now what we're going to do is that we're going to do a real example which is the one that involves entity framework core so let's go back to visual studio and let me comment this out because this was just an example what i want to do is to enqueue this code that we have here so let me try this this is not going to work but i want you to see it let me try this let's say that i want to put a body here so that i can cut all of these inside of this will it work no it won't work why it won't work well because this is an expression this is a tree expression and we can't have a body like this here so what can we do well the first solution that we're going to try is to simply move this into a method and call the method from here let's see that let me come here let's say public async task create person and i will say person name and then i will cut all of this and i will paste that here and then i will eliminate this body from here and i will just say create person and i will pass the person name which is a parameter that we're getting from here now this actually works as you can see this compiles let me save this will compile the application and let me come here so that you can see that this indeed works let me say here for example robert let me execute and we're going to see that immediately we got back a response and if we go back here we're going to see that after a few seconds you can see that we have added the person robert and we have the query that inserts robert into the person table and if we go back to sql server management studio and we go to the people table you can see that indeed we have robert here therefore this is working now this may be fine for a small application but i really don't like it i don't like it because you are tightly coupling this execution to this implementation i would prefer to use a service behind an interface so that we have some flexibility in our application so let's see how to do that let me first come here i will create a new folder i'll call it services and then in services i am going to create a class which is going to be called people repository and inside of people repository we are going to take this code from here and i will paste it here and then i need to bring some namespaces like person and then after that i need to bring the application db contest i need to inject it so let me do that context let me say context control dot as nsa field and also i will use i logger just so that you can see that we can inject as many services here as we want so let me say here people repository logger control dot assign as a field and then in here i will substitute this console by line for the logger so log information this is way better than using console.writeline and let me put this here also now again i want to have this behind an interface so just to save some time i will just put the interface here so i people repository and then i will say that we will implement that interface and here in create person i will say control dot pull up and then that will put this code here all right so now we have to configure the services in the dependency injection system so for that we're going to go to the program class and we're going to come here and we're going to say builder services are scoped are scoped then i people repository let me bring the namespace and then people repository all right so now we can use this ipo repository service for our enqueue function let's go to the peoplecontroller class and instead of doing this i will delete this and i will say here i will pass the i people repository ctrl dot to import the services namespace and then here in the lambda expression i can say repository and then repository dot and this is an instance by the way this is an instance of the class that implements the service i people's repository so in our case because we configured it here this is an instance of the people repository class so here we have the people's repository class i mean in execution time of course so let me say here create person and here i will pass the person name parameter so let me put this in another line so that you can visualize everything better all right so we should be good to go let me comment this out because we don't really need it anymore now as you can see we are hiding the implementation of the createperson functionality behind this interface which is more flexible because now in unit tests we could mock this if we wanted to so let me save let's see that everything works let me come back to google chrome instead of robert i will say melissa and i will say execute again we get back immediately a response and if we come here you can see that after a few seconds we're going to get the insert into people query so that we have added the person melissa and of course if we come back here you can see that we have melissa here excellent so we have seen how to use the enqueue method now let's see how to use the schedule method the schedule method is the one that allows you to create a job that is going to be delayed that means that instead of being fired immediately it is going to be fire perhaps after a few seconds or after a few minutes or after a few hours depending on how you configure it so let's see that let me come here i will come here to visual studio and let me say here http post we're going to create another endpoint i'll say a schedule so let me say public async task action result let me say action result i'll say here a schedule again a string person name and then i will say background job client dot schedule and it works the same way as in queue so that i can pass for example a land expression here or if i wanted to i could pass a service like the i people repository service and then use that service here but in my case i will make a simple example so i don't really need a service i just need a lambda expression and i need to say console writeline and then i will say the name is and then i will say person name but we're not finished yet because now i need to pass when do i want this to be run when do i want this expression to be run this job to be wrong so i can use a timespan for that i can say timespan i can say from seconds i can say for example in 10 seconds and put a semicolon here or i can say for example from minutes from minutes and say after 15 minutes or i can say from hours and then after let's say 13 hours or something like that i can define after how much time i want this job to run now of course because we are making a tutorial i'll just say from seconds and i will say something like three seconds something simple something short so let's say return okay now i am going to save here and this should refresh my application but sometimes i need to help it so i will just refresh manually so we have a schedule here let me say try it out we are going to say felipe and let me put this like this because i want to have the console open so let me come here let me say felipe and then 1 2 3 4 5 6 7 8 9 10 11. okay so it took about 11 seconds for felipe to appear here why is that didn't we say that we wanted to have it so that this job will execute after three seconds well yeah but there is something that i need to explain to you remember that i said that the server that the home fire server is the one that has the task to query the jobs repository the place where we store the jobs it has to then get the pending jobs and execute those jobs right that is the role of the server in a home fire solution but the server does not repeatedly make requests to the database it does it on a 15 second interval so that is why we didn't get this immediately because sometimes it may have to wait 15 seconds for the server to try to fetch the jobs from the database and then be able to run them so how can we modify that 15 seconds interval well we can do that in the program class we can come here and remember that we have this at hanfire server which means that here i can configure the parameters of our home fire server the settings so i can say here options and then here i will say options dot schedule polling interval and in here i can say time span dot from seconds and let's say one so this means that we are polling for new jobs once a second now this may not be that smart to do because it could be a bottleneck for your database because once a second you would be making a query to it so this may not be what you want especially in normal scenarios but let me just test this so that you can see that indeed we're going to get the scheduled job faster in the console so let me i already built the application so let me put this back as it was let me come here let me put this like this let me come here a schedule and let me say here felipe to execute so one two three four five okay so more or less four seconds which is fine and it is definitely better than the 11 seconds that we took in the previous example so as you can see what we have is that we have this schedule polling interval that defines how frequently we're going to query the database query that jobs repository to see if there are pending jobs now it may not be that smart to say that we're going to do that every second so i will delete this so that we have the default configuration of 15 seconds so let me save let me control f5 just to make sure that we have this applied in the settings of our spiro net core application and let's continue now let's see something let's come here to the people controller and instead of from seconds i will say from minutes i want to say five minutes because i want us to have more time so let me say save let me come here let me maximize this because what i want to do is that again i want to write here felipe or let me just say claudia just to say something different let me execute of course it will take five minutes for claudia to appear here in the console but what is cool is that i can come here to the home fire dashboard and i can go to a schedule and i can see that job that has been a schedule as you can see here it says that this job that we have here will be executed in five minutes and if i click on it you can see that we have the name is claudia which is this code that we have here the name is claudia so i can visualize this but what can i do with it well i can delete it let's say that i don't want this to be run so i can just delete it okay and now we don't have the job here anymore and therefore i have been able to stop the job from executing which is great because maybe sometimes you will have a job that you don't want to be executed so you can just stop it with a few clicks you don't even have to write code for it something else that i want to show you is that i can come here to swagger let me say that now i will say robert again execute again we have robert here we have robert here it will get it will get executed in five minutes that is great but let me say that i close this let me say that i come to the console and i shut down the application pressing ctrl c so i just shut down the application the whole application the server and the client so i close everything i close the client and i close the server but if i re-run the application let me press ctrl f5 to run the application now we have swagger here let me duplicate this because i want to open the hanfire dashboard you are going to see that now here in jobs in a schedule we still have our job why is that well remember that we have our jobs here in the database in the job table which means that indeed since we have this job here that says scheduled then it means that even if we shut down our application when we load back the application up it means that the job will be there and then on fire will be able to take it and then run it after the three minutes passes so let me come here this is great we are done with this now let's talk about recurring jobs now the difference between a delayed job and a recurring job is that a delayed job runs after for example a few minutes and a recurring job runs periodically for example in intervals of 5 minutes or in intervals of 1 hour which hourly or daily or weekly or monthly so the late job executes once but the recurring job executes several times until we stop it of course so let's learn how to do a recurring job in hand fire so what we're going to do is that we're going to make again another simple example writing in the console but i will use a service so let me come here to services at class i'll call it time service and i will come here this is really simple cause i will just copy and paste it alright so as you can see here we have this code we have this print now method and we have logger log information daytime now and we're just printing today's date and time again i will use an interface so i will say public interface i time service and then i will come here and put this like this and pull this up all right so now i have to configure this i-time service in the program class so let me come here and let's say builder services at transient i time service time service and now i can use it i can use it for creating a recurring job based on this time service because i want to let's say execute this function once a minute so let me go to the program class one place in which i like to put this is after this use hanfire dashboard so here in the http pipeline why here because it means that then this function will get automatically configured as a recurring job once we load the application which means that this is always going to run at least once so let me say here recording job dot add or update and in here i can do i time service and in here i can pass the name of the recurring job it is important to have an id or name for the recurring job because that way even if we execute this function several times it doesn't mean that we're going to have several instances of the recurring job running simultaneously so let me say here for example print time of course you can give it a descriptive name depending on the task that is going to do like if it is about sending emails you can say send emails or something like that but in my case print time is fine so here i will put service and then service print now this is a function that i want to execute once a minute now here i can say cronk dot and then say daily for example if i want to execute this functionality once a day or maybe immediately once a minute or maybe hourly once an hour or if i want to i can use a crown expression like i can say for example this like this and this means once a minute anyway but using a chrome expression which allows me to be very specific about the time in which i want to execute my recurring job so just to keep it simple i will just say cron dot immediately and that is great now let me run my application so let me save i will press ctrl f5 anyway because i like to make sure that when i modify the program class i get the latest version of it for my application because sometimes hot reload doesn't work very well when i modify the program class so let's come here and let's see that here we already have a recurring job we have a recurring job that says that we're going to execute itimeservice.printnow now if we go to the console let me go to the console and you are going to see that we have the current date and time here now i have to wait one minute more in order to have this being executed again so i will just forward the video to that part and as you can see here one minute has passed so we have that our function was executed again now let me show you that we can update this job that we have here for example and let me put this here so that you can visualize everything let's say that i want to update how frequently this function runs so let me say instead of minutely i will say for example daily so daily and therefore i can save again press ctrl f5 and i will come back here and you're going to see that now we have that this will run in five hours awesome so we have now that this job will not run every minute for every 24 hours so let me come back here and let's see the final part of this tutorial which is continuations as i mentioned before you can have it so that after you finish a job you can run another job so that you can have a chain of jobs let's see that let me come to the people controller class let's come to a schedule and let's see something before we do the continuation let's see something let's see that we have this green squiggly line here because this is not really async because when you use hum fire you are not really doing asynchronous programming so you don't really need to use async here nor here so let me put this like this and that is great all right i can save and you can see that now we don't have anything bad going on here so now let's go to the continuation example so what i want to do is that i need to get the id of the job that i am going to execute so this schedule and these enqueue functions actually return a string which is the job id so i can say job id and let me put this like this so that you can visualize everything and then with this job id what i can do is that i can say something like background client continue with and in here i can pass the job id and then in here i can put whatever job i want to run after we finish executing this job that we have here so let me say again a simple example of console right line so let me say console.writeline i will say let me put here this for using stream interpolation so that jobjob id has finished all right so let me put this again in another line and this is great let me put here a parenthesis that is missing let me save now let's go back to google chrome and let me come here to schedule try it out let me see by the way we have here five minutes i don't want to have to wait this time so let me say from seconds five seconds is just okay so let me save let me come back here let me say felipe let me execute and let's see that here we're going to have to wait a few seconds in order to get felipe but after we get felipe we're going to get the message that that job 11 in this case has finished so as you can see we were able to run this job that we have here only after this job that we have here finished so that's about it this was an introduction to home fire hope you like it and if you want to learn more about net react angular blazer and other technologies please check out my udemy courses there is a link in the description of this video with all of my courses with a discount applied thank you
Info
Channel: gavilanch3
Views: 20,422
Rating: undefined out of 5
Keywords:
Id: OkpXpMBUG9c
Channel Id: undefined
Length: 37min 38sec (2258 seconds)
Published: Fri Apr 08 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.