Schedule Tasks in Node.js | JavaScript Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to this tutorial so today what I'm going to be showing you is how you can schedule tasks in node.js to occur at a specific point in time and also how you can repeat a task at a given time interval so for this I'm going to be using a library available via npm it's called node schedule so I'm going to be copying the installation command and then in my terminal I've already cd'd into my project folder so I'm going to paste the installation command there so now that I've installed this Library I can go ahead and start using it in my app.js file so first of all I need to import this Library into my project so I'll save it under a reference of schedule you could call it something else and it would work just as well I'm just sticking to the reference that they use in the documentation for this package and inside require all I need to do to import it is to name the package and now I have available to me all of the functions of this library on this schedule object that will allow me to schedule my tasks so I'll save this now and set a task to occur at a certain point in time so this Library works with the native JavaScript data object so first thing to do is to create one of those containing the date and time when you want the task to occur so to create a new data object you create a new instance of date and I'm going to create a date here by passing in numeric arguments so today is the 9th of December you need to watch out with the months so with a date object the accounting of months it starts from zero so December would be 11 that's not the case for days however so for the 9th of December I can just enter nine there and I'll enter the time as 1500 hours and I'll come back and edit this in a moment so this task occurs just after I've written the next bit of code so we're now going to call on the library to schedule the task so it's under the reference schedule and you want to call the schedule job method the first argument you pass in here is the date object that you've created and the second argument is a function in which you place the task whatever it is you want to do so in practice you might be using this to schedule the running of a web scraper or to fetch data from an API but in this tutorial I'm just going to log a message to the console when it's complete so I'm just going to say that the task is complete and also include a log of the time when that occurred so I can do that by just calling the date Constructor and not passing anything into it okay so the time now is two minutes past three coming up to three minutes past three so I'll just quickly set the scheduled time of this task to occur at three minutes past three I'll run my app and now when it hits three o'clock we should see the message log to the console with the current time so there you go the task ran as expected okay so now what I'm going to show you is how you can set a task to repeat at a specified time interval so this time instead of passing in a date object we're going to be passing in something called a cron expression now if you're not familiar with what one of those is don't worry I'll be showing you how you can create your own in a moment but first I'll call schedule job again so very similar to last time except the first argument is a chrome expression the task itself is passed inside a function in the second position so you can get an overview of how a chrome expression is formatted if you visit the node schedule npm page and then scroll down so this is a good overview of how you do it I'll just comment it out so each start represents a unit of time starting from seconds to minutes to hours to days to months and then finally days of the week now in terms of creating your own chronic expression if you're unfamiliar with them you can visit this it's a chrome expression generator so in case you're not familiar with chronic Expressions I'd recommend playing around with this tool for a few minutes there's a key here showing you the values that you can enter for each of the units and also some example cron Expressions here to get started with now there's only five units listed here the seconds in a chrome expression are usually optional but in this tutorial we're actually going to be setting the task to run in seconds so that you can see the task repeating in real time so I'm going to copy this expression to use it in my code so I enter it as the first argument here and I need to set a task to run every three seconds so I'll just say job done here and in fact I could use console count here to see how many times this has run okay so hopefully this is working now I can start my app and the task should be running every three seconds so you can see this is repeating itself and it's just going to repeat indefinitely here unless I cancel it so that's the final thing I want to show you in this tutorial so to cancel a job you need a reference to the job that you want to cancel and you can get that as a return value when you first create a job so I'll save this under a reference of job and then we need to do is call the cancel method on the job so now when I run my app this job done it's only going to run at once so I start a new instance of Powershell run node app so now job done it only runs once and then my node app it shuts down now if you want the job to complete a certain number of times let's say for example three times what you can do is create a variable that starts with a value of zero and then a variable that specifies the number of times that the task should be completed and then each time the job is done increase the value of I by one and then check if I is equal to n then you want to cancel the job at that point so I'll run the app once more this time the job should only complete on three occasions so you see as soon as the third one has completed my node app stops running because there are no more tasks to be completed so this is how you can schedule tasks in node.js I hope you found this tutorial useful if you did please consider hitting the like button down below this video it helps us with the algorithm and others to find this video and if you'd like to see more content like this from us in the future don't forget you can subscribe to the channel
Info
Channel: OpenJavaScript
Views: 11,334
Rating: undefined out of 5
Keywords:
Id: ZG0Fl5Ejq68
Channel Id: undefined
Length: 8min 13sec (493 seconds)
Published: Fri Dec 09 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.