Go back to 8base Academy
July 21, 2022

Writing and Running Advanced CRON Jobs on Task Functions

Sebastian Scholl
@SebScholl

Speaker 1 (00:03)
Hey everyone. Today we're going to be looking at how you can set up cron jobs in 8base, which is essentially saying how can I run a task function on a nontypical schedule? Before we jump into it, I'm going to read you a quick little sentence. I want to run a function at the half hour every hour between 9:00 PM and 5:00 PM every month of the year within the first 7 days of the month. Let's do it. Cool.

Speaker 1 (00:31)
Just as a little bit of set-up, I have created a new directory for server functions using the 8base command line. I have the 8base command line installed, NPM install 8base CLI or Yarn, whichever package manager you use. I'll include a link in the video description that gives you a little bit more of the environment set up. However, I run 8base in net that creates my new project and then I just deleted some of the boilerplate that comes in there, so we have a completely empty project here.

Speaker 1 (01:00)
Now, the first thing that we are going to want to do is run 8base generate task. We're going to generate a task function and then we're going to call this my cron job. We're not going to actually get into writing functions, just going to focus on cron syntax and validating that. Then we're going to say that the syntax is going to be JavaScript. Once we run that, it's going to go ahead and generate the function declaration for us and we have the function here, if we wanted to write the function code. I'm just going to x out of that.

Speaker 1 (01:32)
Now we can add another argument to the declaration which is called schedule. Here is where we actually have to give it a schedule. Now I'm going to jump over to the AWS docs here. Under the hood of 8base, the function that you write is going to be deployed to lambda and lambda is going to be the one that executes that function. Using CloudWatch behind the scenes, that's what is going to actually utilize the syntax that we give it to know on which cadence we want to actually trigger that event or trigger that function.

Speaker 1 (02:07)
Now, because of that, we have two different ways of specifying the schedule. One way which is called the simple way, is a rate expression. You say at a rate of a value in unit and the values are minute or minutes, so one minute or five minutes, hour or hours, 1 hour or 5 hours, day or days, one day, five days. Now, that works great because a lot of the time you just need to run once a day or every few hours. We don't have to be that specific.

Speaker 1 (02:37)
However, in the example that we gave on the intro of this video, that's not going to fly. We have to have a much more sophisticated approach to how we actually specify that interval. The way that we do that is using cron. Now cron expressions they allow you to specify the minute, hours, day of the month, month, day of the week and the year. For example, here are some examples that DWS stocks give you around different types of cron jobs that you can set up.

Speaker 1 (03:10)
We're going to do a couple of things here. First off is here's something called Freeformata.com, Cron Expression Generator. This is actually going to help us just use a form to generate our cron syntax. If you really want to learn cron, you can go through it. I don't find it to be that interesting of a language, so I'd rather just use the generator and then do a few of the tweaks that we needed to do to validate it.

Speaker 1 (03:32)
Here what we're going to do is go through and start based on that description that we use, filling out the form. There was no specification on the second, so we're just going to say any second, that's the year, so seconds, cool. We're just going to say any second or every second, any second, here we go. Cool. Then we're going to say on the minute, so we said every 30 minutes or on the minute. We're going to uncheck that and just leave the 30th minute checked.

Speaker 1 (04:07)
The hours, we can see I already filled out this form, but the hours that we specified were at 9-5, so every hour starting at 9, excuse me, other way around. Every hour starting at nine, we have to do it between the two hours. I filled that wrong the first time. Between the 9th hour and the 16th hour. Cool, 9-16, awesome.

Speaker 1 (04:35)
Now the days of the month we want it to be on Monday, Tuesday, Wednesday, Thursday, Friday. During those weekdays on the first seven days of the month. Those the days of the week, okay, cool. We want to change that. Here we can do it on the first seven days of the month. Cool. Awesome. That's up to you there. Then we can go to the month and we want to do it every month of the year so we can leave that and then we want to do it every year. But as you can see, there's a ton of different options in here that we could utilize. However, this is our expression now, so any second, every 30 minutes, I think that's actually going to trip us up on there any second, I think that would be every second. I'm just going to click it. I'm going to leave it that way because you'll see in a minute. Every 30 minutes between 9 and 6, the first 7 days of the month, every month, any year, any day of the week, every year. Cool.

Speaker 1 (05:48)
I'm going to copy that. I'm going to go back into our code editor. What I'm going to do is I'm just going to drop it in here and if you looked at the docs, if you want to use a rate, you have to say rate, if you want to use cron to put in the cron and put in the brackets. Now we need a way of validating this before we deploy it, 'cause if it's not right, it's just going to wait for deploy, or wait a few minutes and they'll say this is invalid.

Speaker 1 (06:15)
What we're going to do is we're going to write a little script that allows us to perform that validation. I'm going to something called crontest.js and the root of my directory here and cool, we have our file there. Then I'm going to go to and install this NPM module called aws-cron-parser. I'm going to copy that and install it in my project. Cool. I'm just going to copy their usage here. Since what we're going to do is, we don't need to get… The next one we just want to see that this is valid. I'm just going to write a little console.log statement here which shows us that our result. Print that to a new line, is the call it now occurrence, which is going to then have the occurrence and then let's make this available to, occurrence next, as well as, let's give it a new line once again. Then we can just copy this line down here for the next occurrence and occurrence next. Awesome.

Speaker 1 (07:45)
Let me go grab that cron syntax. Here we don't need to actually give it the function declaration. I think that's what it would be. We can just give it the actual string and so we've dropped that in there. Now let's run our function using node cron test js and cool. Since we're not a module, we have to use a require, sp constant AWS Parser equals quire constant. Awesome, so not a valid number, so we're getting an error here. The reason being for that is because with CloudWatch, if we look here with CloudWatch events they don't support seconds. The intervals that you can do are based on minutes, hours, day of the month, month, day of the week and year.

Speaker 1 (08:46)
That first one and if you look at a bunch of different cron providers, they all have a different syntax and they try to embellish theirs with some different helpers or decorators, so it's a little bit all over the place. But if you see here, you can see that this one did allow us to specify seconds, so it's a different system. Pretty simple. All we have to do is delete the first number 'cause that was specifying the seconds where it should have been. Let's try it again. Cron test, awesome. Now we see that we are getting the… The currents now would be coming at 9:30 on the first day of next month, and then the next occurrence after that would be at 10:30 on the same day. Our cron syntax is looking like it's working accurately, and the script helped us validate that.

Speaker 1 (09:41)
Now what I can do is I can take that syntax and drop it into my cron job with 8base. Imagining that the code was written and everything was working as expected, I could either run 8base deploy, deployed directly to my workspace, which would hopefully be your development environment, or commit it to GitHub and let the Git-Repo or an action script handle that.

Speaker 1 (10:04)
I hope that this gave you some helpful tips on some resources that you can leverage to start writing more complex or more advanced cron jobs to specify the schedule or cadence on what your task functions will invoke. If you found this video helpful, please give it a thumbs up. If you would like to get updates when future videos are released, please subscribe to the channel. As always, happy developing and looking forward to seeing you in future videos. Take it easy.

Share this post on social media!

Ready to try 8base?

We're excited about helping you achieve amazing results.