Nestjs cron job example with Task Scheduler

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this next J tutorial we are going to learn how to do Crown jobs Crown jobs are often used to Shad tasks that run repeatedly or just once at a specified time some of the common use cases is to send email notification take for example you want to have a crown job that runs to check which employees have work anniversary today and then send them email notification to everybody something like that another common use case is to Shadle database backup or to clear cash and etc etc next JS has made cron jobs Easy by providing us with this Shadle module so with this shadu module we can easily execute Crown jobs let's see how to make use of it the first thing I'm going to do is to create a new njs project using the CLI so I'm going to say nextest new let me just call this Nest Chron you can call it whatever you want okay the project is created let me see it into the project next crown and install the shedule module so I'm going to copy this npm install next just shed you paste it over here done let me go ahead and open this in vs code the next thing we need to do is to import the shadow module in app module. TS so in the list of inputs I'm going to say schedule module. for root with this we can go ahead and create a service class and method for the scheder let me go to app service and have one example of task scheduling here I will make use of an example from here to save time so to specify the interval you can write using this uh more common Chron syntax where um the first item is how many seconds the next one is minutes hours day Etc so that means for example if you write a Chown um job like this this means to execute every 45 seconds if you put something like two here that means 2 minutes 45 seconds Etc but njs provides us with a different syntax that makes this easy so you can use this uh Chron expression do whatever thing you want to you want the time to be let me copy this one as it is easier to use so I'll paste it over here instead of console do instead of uh logger.log let me just do console.log executing every 30 seconds make sure to import chrone from from the Shadle module and then expression so whatever the job does is totally up to you you can send an email you can back up database you can do whatever you want to do but the point is that this is going to execute every 30 seconds in this example let's pick something that is more something shorter like every second every every 5 Seconds okay so this is going to execute every 5 seconds and then we can give this a try let me go ahead and start the server npm R start dev then we wash out in the console to see if every 5 Seconds the job is going to execute you can see the first execution and then we wait another one after 5 Seconds and so on and so forth so this is how easy it is for you to Shadow tasks in next J using the Shadle module I hope this makes sense until next time enjoy coding
Info
Channel: ZestMade
Views: 2,005
Rating: undefined out of 5
Keywords: Nestjs cron example, Nestjs cron tutorial, Nestjs cron, nestjs scheduler, nestjs cron not working, nestjs task scheduler
Id: 8ZE1DQah2fc
Channel Id: undefined
Length: 4min 14sec (254 seconds)
Published: Sat Nov 25 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.