Effortlessly Schedule Emails using Node.js & Cron Jobs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] job is a job or it's a task scheduled to run automatically at specified times or dates defined using a specific syntax in this video I'm going to show you how to schedule your emails so that they can be sent or they can run at specific times without the intervention of a human user hello everybody Welcome to coding 101 my name is scootlo if you're interested in improving your web dev coding skills this is the right channel to subscribe to so subscribe to my channel for more web development content if you've enjoyed like this video and leave a comment in a comment section now there are many reasons why you might want to schedule an email to be sent at a specific time now one reason I can think of is let's say you've built a subscription based e-commerce website and so you want to send a customer message by the way I get like some of these I still haven't paid my zoom subscription so now Zoom is sending me emails every single week I think so they're sending me emails every single week reminding me that I haven't paid my zoom subscription so those types of emails are emails that are sent through a Cron job because nobody's gonna actually sit there and actually send emails for like a million people who probably all Zoom by now please do not tell them that I owe them so yeah so nobody's sitting at the computer having to send these emails over and over again so they've set up a job and they've set up a task that is going to be executed once uh it's a specific time so another reason that we can think of it's let's say for example a network is delayed uh or something happens to your coded breaks you you need to schedule a task that is going to log that error is going to send you a message that hey you know what that website that you created it just crashed so um you might want to do something about that or maybe there's a network delay or something went wrong so you could set up a Cron job to just like keep tracking your status or keep tracking your server status rather and if there's something wrong then it just tells you um what has happened so that's another way to implement a Cron job in this instant so of course in order to create these repetitive tasks like I've been saying we're going to have to use what we call a Cron job so cron is actually a tool that allows you to execute something on a schedule it's typically done using the cron syntax so it allows you to execute a function whenever your scheduled job triggers so the syntax is used to specify a Cron job so the syntax is as follows and if you want to um learn more about the constant text you can just go to cron tab dot Guru and so this is where a current syntax is actually created so this is the time this is the schedule that we're going to give to our current job and it's going to know that at this specific time that should be implemented so what we have in front of our screen is the time five past four so there's going to be if we were to implement this directly in our code then what would happen is that each and every single day at five past four a certain task would be executed but as you can see this is sort of like a one two three four five so it has like five parameters we can specify the day that this crunch drop should actually run so we can say maybe the 12th so what's going to happen is that every month every 12th of every month uh the 12th day of every month let me say at five past four a function is going to be executed so the crown job is going to check each and every day that oh is it five plus four is it the 12th of that particular month and that's when it's gonna know when to execute whether to execute or not to execute a particular task we can also specify the month and we can just say just do it in February okay so each and every single year on the 12th of February um and yeah on the 12th of February at five past four make sure you execute certain tasks so this is how you can implement it uh so this is how you can create uh the cron syntax in order to so that it can align with your um with your crown job okay so and so these are like these are fields that we're going to combine in order to form that syntax and it's going to specify uh when and how often actually a particular command or a script should run the commands and schedules are defined uh as follows so now that we know the the commands or the the so now that we know the cross syntax we can just like go directly and Implement cron so we're using that code that we first used in order to send emails so um if I'll leave a video or link on the cards so that you can just like check it out so in order for you to get started using like Crown Jobs first you need to go to npm and you need to install node cron so node cron is the number one go-to package node.js package that is used in order to run these crotch jobs so that's exactly what we're first gonna do we're just going to first of all get rid of cancel that and then we're just going to say npm install node Cron and then after we're done we are going to um import the library into our node.js script as follows and then what we oh sorry and then what we want to do now is that we want to define a task so you can define a task by just simply using a function so there is our task so our task all it has to Simply do is to console log running a task every single minute because we're going to schedule it around every single minute so that is what our task is so now that we've created our task this is all our task does by the way we're going to schedule the task to run at specific intervals using the schedule method so we're just going to say cron.schedule like that and inside of here we're going to put in the crown expression in a minute and then we're going the second argument is going to be the task like that yeah now if we want the Cron job to run every single minute we can just go to cront tab.guru and what we can do is that we can just like get rid of everything in here and oh sorry about that let me just do it properly yeah we can just like replace everything with an at-risk like that and this is going to run our task every single minute we copy that put it inside of our code like that and so what's going to happen is that this console log is going to run every single minute so now we're just going we're going to start our project again I'm just going to say node mod app.js so I'm gonna wait for a minute I'm gonna pause and wait for um about I think two minutes or three minutes to come through and wait for a console to log for the task to actually be executed on our console so I'm just going to wait for three minutes I'm gonna pause the video so it's been over two minutes and I'm waiting for the third console to like come through and there it is so it has printed to our console at least three times because I've been waiting for three minutes so we can see that our Crown job is actually working as it should now remember what we want to happen is that we want our email actually email function our emails to be sent at a particular time so what I think we're going to do is that we want our emails to be sent each and every single month on the 28th right so this will be sort of like an invoice uh to just like a sort of remind a person how much they owe so in order to do that we just maybe we're going to send it um at what time Maybe I'm gonna put zero over here and then I'm gonna say 12. yeah I think this is reasonable it's not midnight it's midday and this is going to send every single what did I say 28 okay so an email is going to be sent on the 28th of every month at 12 o'clock noon I think that is good and we can just like copy this and you can paste it in here and then instead of saying task over here we're going to call that function that email function that we've had previously oh sorry actually the email function is called send email so yeah and so what's going to happen is that every single month on the 28th of every single month an email is going to be sent out and using the cron schedule right so I think that is about it for today's tutorial I hope you've learned something new because I know cron cron is not something that is really popular around so many different circles I think it has so many different applications that you could use it that you can use in your code so make sure that if you having problem understanding this just like let me know in the comment section and I'll probably make a second video that is only based on Chrome and does not have to do with emails and stuff so thank you very much for watching I hope you have subscribed to my channel if you haven't subscribed please do subscribe and if you've learned something new today or if you've enjoyed the video like the video leave a comment in the comment section telling me why you didn't enjoy the video so yeah thank you so much for watching I will see you on the next video
Info
Channel: Koding 101
Views: 5,072
Rating: undefined out of 5
Keywords:
Id: Q7S2SbadV6o
Channel Id: undefined
Length: 10min 43sec (643 seconds)
Published: Thu Feb 02 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.