 Hey, welcome everybody. In this video, we're going to look into scheduled tasks using a library called Node Schedule. Chrome drops the 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 going to 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-NodeSchedule, exactly the same as the terminal. I'm going to remove this and let's start by initializing a new project. So npm init-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 one 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 project folder, you can do right click npm init-schedule. Press enter and this will install the package. If you open the package.json file super quickly and let's minimize this, you will see the dependency 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 going to 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.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 our cron job. So the first and most basic thing that you can do is use setInterval and for this you don't need any packages. So setInterval like so and then from here you can create a narrow function like so and you can do console.log and then run in 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 save this and if I wanted to run our project we can go here. Let's clear everything super quickly and let's do npm run dev. So n 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 come in 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 going to do node schedule. And now we can grab this from here and start using it. For example in their documentation they have 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 do dot you have the option of schedule job like so and then inside here you need to put the setting. 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 this 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 then 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 running task every minute 26 super quickly save it and now let's run this super, super quickly press enter and hopefully when the timer goes to three o'clock and 26 seconds as 26 minutes as you can see actually run. So yep so that works straight away and our time did 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 cron drops and if you have a problem and if you wanna see when your cron drop actually run there is a way of doing this. So what I'm gonna do is paste the one from above and from here I'm gonna do every second so I'm gonna do two more stars. So we need 16 total. From here inside function, inside of function we can do function and then I can do fire date. Do view well wrap because I'm zoomed in it's gonna 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 we can put 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'll see that it's obviously gonna continue doing because it's running every second but if I close it super quickly so we can see a little bit better. So as you can see here the job was supposed to run on Friday August 25th 2023 and the hour is 3 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 job run exactly when it 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 cover 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 all 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 gonna be equals new date and we need to start with the year so 2023 and then this is gonna be a tricky one. So for example we are currently August which is technically the 8th but the months start from 0 so August in this case is gonna be the 7th and then September is gonna 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 3 o'clock so 15 and then the actual minute is 30 but let's put it as 31 super quickly and then zero and this is the seconds by the way and now I'm gonna paste a super quick comment running the 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 scripts 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 and 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 gonna be new and then schedule dot recurrence rule like so and then now we should be able to use this rule so row and then from here if you put dot you will be able to see the options so for example we have the dev the week the hour the minute month second and so on and I'm gonna 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 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 gonna be running every 34 minutes and now let's do npm run depth super quickly hopefully if I time there correctly in the second this shit run yep here we go 34 and running every 34 minute after the hour which is awesome so this also work let's copy this super quickly and let's comment it out and now the other example that I'm gonna show you is that you can use a literal syntax which is kind of like easier to understand as do dinner time and let's do instead of rule 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 to seven I believe if you look at this year the other week yep zero to seven and then if I run this this shit 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 entire 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 gonna be the start time and then this is gonna be equals new date and then date dot now and then plus 5000 milliseconds which is five seconds and I'm gonna do the same thing for the end time and this time this is gonna 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 job schedule schedule job and now from here where we have the rule remove this and put it on another line here and we're gonna have free route so we're gonna have the start which is gonna be the start time from above from here from the cons then we're gonna have the end which is gonna be the end time from here and then we're gonna have the rule which is gonna be six stars but the first one needs to be half and now we can say time for them alright 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 or scripture 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 shut down like so and now let's say this is gonna be 10 seconds I believe so this is gonna be 10 seconds and now let's do NPM 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 is going to shut it down graceful 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 cancel method then you have cancel next then you have reschedule method and then you have next invocation method and so on the last thing I wanted to mention is that most of the modern Node.js hosting providers do have cron drops options that you can use for example here with Cyclic if I go to my project and if I go on the cron you'll be able to see that you can actually schedule API calls from here so this is a little bit different and all you need to do essentially is create a new cron task and from here you have two methods 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 you might want to 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 gonna 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 gonna run at 6.05, 7.05, 8.05, 9.05 and 10.05 and so on of course you can change the time from here and you can look at the chron expressions and then the cool thing with Cyclic is that they have some chron 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 fifth of every month at 1am and that's pretty much it anyways that's gonna 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