NodeJs Cron Jobs: A Comprehensive Guide | Node Schedule

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome everybody in this video we're going to look into scheduled tasks using a library called node schedule Chrome drops a scheduled tasks that run automatically at specific intervals they are incredibly versatile and can be used for wide range of purposes such as backup and maintenance automated reports data synchronization scheduled tasks system monitoring and alerts security measures data processing and transformation automation of repetitive tasks content updates and so much more we'll also have a quick look into Chrome jobs which you can do straight from your hosting provider hello and welcome everybody I've already created a brand new project folder and inside this folder we're gonna create a new project so I'm going to be using Powershell just because it's a little bit easier to see here on the right side but you can definitely use the terminal from Visual Studio code by going here terminal and a new terminal this is just going to CD to your project folder here as you can almost see it's kind of hard to see but here it is node.js node schedule exactly the same as the terminal I'm gonna remove this and let's start by initializing a new project so npm init Dash Y and this is going to create a package.json file without asking us all of those questions the first thing that we need to do is install the package that we need for this tutorial and this is going to be the node schedule if you have a look super quickly it has over 1 million weekly downloads which makes it a very popular option today's tutorial is going to be heavily based on their documentation and the style width we need to install it so let's copy this code and let's jump back into Visual Studio code so here in Powershell as long as you are inside your product folder you can right click npm I node schedule press enter this will install the package if you open the package.json file super quickly and let's minimize this you will see the dependencies here and we have the node schedule version of 2.1.1 as of today let's create a development script which we can run for this project and I'm gonna put a comma inside here and then create a Dev script and then this Dev script is basically going to tell node to run a file called server dot JS which I'm going to create right now so let's copy this save package.json and close it let's go back to the Explorer new file server.js and now we can focus on All Chrome drop so the first and most basic thing that you can do is use set interval and for this you don't need any packages so set interval like so and then from here you can create a narrow function like so and you can do console log and then running a task every second that's it and now here you need to put the time so this will be 100 milliseconds sorry 1000 milliseconds is equals one second so if I say this and if I wanted to run our project we can go here let's clear everything super quickly unless the npm Run Dev so any is on the line here but yeah as you can see npm run Dev and press enter and now this is going to run the task every second so this is the very basic thing that you can do let's stop this by doing control and c and then y to terminate the job and now let's comment this out now let's have a look into the node schedule package the first thing that we need to do is include it so const schedule equals and then require and I'm gonna do node schedule that's it and now we can grab this from here and start using it for example in that documentation they have a as a const job so we can do const job and then we can use the schedule from here in order to do the granular work so from here if you did dot you have the option of schedule job like so and then inside here you need to put the settings if you go to the documentation super quickly and if you scroll down a little bit you will see that the Chrome style scheduling is by seconds minutes hours day of the month and then month and then day of the week and if you look super closely in here first of all the seconds is optional and then also if you look at the minutes for example you can choose from 0 to 59 for the hours you can choose from 0 to 23 and so on so let's have a look at how we can do this every second how we can set the scheduler to run every second just like in here so in order to do this we need six stars one two three four five six and that's it now we need to create another function like so and then inside here we can actually copy the console log from here and paste it so running a task every second save this and let's rerun our script by doing up and an npm run Dev and as you can see our task is running every second just like before let's stop this press Y and play this now I'm going to copy this and comment it out and now let me show you super quickly if you wanted to run this on every 57th minute then you would do something like this you remove the second from here and then you put 57 and you're done at the moment we're on 20 minute 25th but if I say minute 26 and then every rain and task every I mean 20 super quickly save it and now let's run this super super quickly press enter and hopefully when the timer goes to 3 o'clock and 26 seconds as 26 minutes as you can see actually run so yep so that works straight away and I timed it pretty well and that worked save this okay let's copy this and comment it out super quickly and now let's look at another example and sometimes you might have a little delay between your Chrome jobs and if you have a problem and if you wanna see when your Chrome job actually run there is a way of doing this so what I'm going to do is paste the one from above and from here I'm going to do every second so I'm gonna do two more stars so we need six in total from here inside function inside the function we can function and then I can do fire date if I do View well wrap because I'm zoomed in it's going to look a little bit ugly so for the console we can say in single quotes we can say this job was supposed to run at and then we can bring with the plus we can bring the fire date when it fired plus and then in another single quotes Wikipedia but you actually run at and then we can do the same thing plus and then now we can use the date now so new date like so and that's it if I was to run this save it first of all and if I was to run this you will see that it's obviously going to continue doing because it's raining every second but if I close this super quickly so we can see a little bit better so as you can see here this job is supposed to rain on Friday August 25th 2023 and the hour is three o'clock 28 minutes and 10 seconds and if you look but it actually run on Friday August 25th 2023 3 o'clock 28 10 which means that or cron drop run exactly when he was supposed to but sometimes this can be quite useful okay let's clear this up super quickly and now let's look at another example I'm gonna comment this out I'm gonna copy this one here just because it was a simpler example so with the next example we'll be able to Target the year the month the date and the time when or schedule should be running and in order to do this we can remove this from here and we can just put a const so for example I can call a date and now we're gonna need to create this date so I'm gonna do const date and then from here this is going to be equals new date and we need to start with the year so 2023 and then this is going to be a tricky one so for example uh we are currently August which is technically the 8th but the moms start from zero so August in this case is going to be the seventh and then September is going to be the 8th if that makes sense so in this case we're gonna do August which is the 7th and then the day is 25th and then the time at the moment is three o'clock so 15 and then the actual minute is 30. but uh let's press 31 super quickly and then zero and this is the second by the way and now I'm gonna paste a super quick comment s running at a specific date and time given save it and super quickly let's do run npm run Dev and I think I just missed it so if I was to do 32 save it and let's rerun this npm run Dev and now when the timer hits 32 hopefully all script should run and I'm gonna probably speed up the video so we hit 32. and here we go we are 32 and as you can see running a specific date and time given which means that this is also working let's comment let's copy this and comment it out on the next example I'm gonna paste two comments so for example this is going to be a specific recurrence rule scheduling and it's going to run every for example 42 minutes after the hour in order to do this we need to set up a row so we're gonna do const and then all row is going to be new and then schedule like so and then now we should be able to use this rule so rule and then from here if you put dot you'll be able to see the options so for example we have the day of the week the hour the minute month second and so on and I'm going to be using the minute so let's put this equals as the moment is 33 let's do 34. I probably won't be able to finish it but let's have a look and now instead of the date here all we need to do is put the row and then save and then let's and then let's put a let's put a comment so this is going to be running every 34 minute and now let's do npm run Dev super quickly hopefully if I time there correctly in a second this should run yep here we go 34 and running every 35 minute after the hour which is awesome so this also worked let's copy this super quickly and let's comment it out and now the other example that I'm going to show you is that you can use a literal syntax which is kind of like easier to understand as the dinner time and let's do instead of row here we can use the literal Time by doing Curly brackets and inside here we can put the row so for example our and we can say every every and we can say every hour and then minute every 30 minutes and then we can do day of the week and then the day of the week can be zero and so zero two seven I believe if you look at this here the other week yep zero to seven and then if I run this this should also work and so on I'm not gonna test this one let's move on to the more interesting ones alright so the next one is pretty cool what we can do is set a start time and an enter so we can start a script after five seconds and stop it after 10 seconds let's have a look at how we can do that first of all we can set two counts the first one is going to be the start time and then this is going to be equals new date and then date dot now and then plus 5 000 milliseconds which is five seconds and then I'm gonna do the same thing for the end time and this time this is going to be equals new date and then let's remove this actually date start time dot get time plus 5000 milliseconds and now we can do the crown drop which I can copy from potentially this one here might do the job just so we can speed up the process so cons drop schedule schedule job and now from here where we have the row remove this and put it on another line here and we're gonna have three rules so we're gonna have to start which is going to be the start time from above from here from the counts then we're gonna have the end which is going to be the end time from here and then we're gonna have the room which is going to be six stars but the first one needs to be halved and now we can say time for them all right let's save this and let's start npm run Dev after five seconds we should see something time for dinner time for dinner and now after 10 seconds we're done as you can see I can demonstrate it one more time after five seconds of scriptures start one two three four five and then after another five seconds we get to go and that's it and that's it now I'm gonna comment this out and for the last example we can actually do a graceful shutdown and I'm not exactly sure how to demonstrate this but what I can do is copy this one here so the most basic one so let's say we have this schedule here which runs every second and maybe I can set an interval here and this interval is going to shut down or schedule so in order to shut it down we can do schedule dot graceful shutdown like so and now let's say this is gonna be 10 seconds I believe so this is going to be 10 seconds and now that's the RPM run Dev and now this is gonna run for 10 seconds hopefully and after that the set interval is going to run after 10 seconds it's going to shut it down race for shutdown and that's it as you can see this no longer works and just like that I wanted to show you the last few bits if you scroll down to the bottom at the bottom here you can see that you can also invalidate any job with the console method then you have cancel next then you have reschedule method and then you have next invocation method and so on the last thing that I wanted to mention is that most of the modern node.js hosting providers do have crown jobs options that you can use for example here with cyclic if I go to my project and if I go on the Crone you'll be able to see that you can actually schedule API calls from here so this is a little bit different and we need to do essentially is create a new Chrome task and from here you have two method one is get and one is paste so the get method is the default one when you kind of like visit the page so for example you might have a route on your node.js application that is about or whatever and then you might have a script in the about page and you might want to run that script and do my motor schedule to run that page for some reason a couple of times a day so what you can do from here you can schedule you can first of all change your headers if you're doing something specific then from here you can schedule a type this means that you can set it to run once and that's it or you can schedule it now this is going to be very familiar because we've kind of like already looked into this but if you set five as an example here then this means that it's gonna run every five minutes past every hour and then if you look here at the bottom this is obviously it's using my time zone and then it's going to run at 605 705 805 905 and 1005 and so on of course you can change the time from here and you can look at the Chrono expressions and then the cool thing with cyclic is that they have some Chrome presets so you can do every 10 minutes past every hour every day at 4 50 every Monday and Friday at noon and the first and the 5th of every month at 1am and that's pretty much it anyways that's going to be everything from this tutorial thank you very much for watching I hope that you found this useful consider liking this video and subscribing to my channel [Music] thank you
Info
Channel: Raddy
Views: 4,154
Rating: undefined out of 5
Keywords: cron jobs nodejs, cron, cron job, nodejs, node, node js tutorial, cron jobs, cron job tutorial, cronjob, node js, nodejs tutorial, learn node.js, node js cron job, Node Schedule, node-schedule
Id: Ezv30i47sDs
Channel Id: undefined
Length: 17min 18sec (1038 seconds)
Published: Mon Aug 28 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.