* This is an automated transcript. Please excuse inaccuracies.
Hey there, in this video, we're going to be talking about custom task functions and how they can be run like cron jobs on a schedule interval. We'll be using the command line tool to generate a new task function then write some custom code within it and deploy it to a workspace, where then we can see it executing by reviewing the functions production logs. Let's jump right into it.
So, before we get started please note that we have some good documentation about task functions if you use docs. 8base.com. Here you can find out you know the different parameters and configurations that they expose, as well as how to just set them up in general. However, in this example what we're going to be doing is we're going to be looking at a workspace called Vue Todos, it's an application that I've used before in previous videos. However, the one thing that we will do for the sake of this video is add a complete by field to our to-do which is going to be a date. Let's just leave it as date. And let's give it a default value of let's say we have to complete all our new things by December 5th of 2019 and its mandatory. I'm going to create that field and we're good.
So, let's now jump into the command line and get started. So, now in my command line and I also have my text editor open here, I'm going to just take a peek at the generator commands that we have for custom functions. So, if we do 8base G and we use generate or G, it's just an alias task and then let's look at the help that we have for this. So, we can see that, okay well on the generate task command we can pass schedule which would be the schedule on which the task runs; whether or not we want to include mock files; whether or not the syntax should be typescript or JavaScript; as well as whether or not we want to disable printing the information into the console. So, really the important one here is the schedule right.
So, first we're just going to initialize the task function with a schedule of going once per day or let's actually do once per minute, so we can see it when we when we run it after deployment. So, let's do 8base G task and then let's give it the name. So, let's call this name “incomplete tasks report”. All right and then we're going to say, okay “has a scheduled… S-c-h- scheduled which goes at a rate of 1 minute”… cool. So, if I run that, we can see that okay so it updated our 8base.yml file which if I click over here, we can see that did that. We also created a handler file as well as the mock, so we could run it immediately all right so and you can see here how I did the syntax versus, okay well the schedule is running at is one wait per minute. These are just convenience functions essentially or syntax. So, if we want to say one per minute one per hour. one per day. it would be super straightforward. However, if you do want to use cron syntax you absolutely can and I would recommend the check out the documentation for a much more thorough overview of what that would look like.
So, in that case let's jump into our source directory and look at our tasks which now we have our incomplete tasks report and the handler function for it. So, you can see that it also gave us the command that we could use to invoke that function locally. So, if we just pasted that into our command line, we can see that hey it's running and boom it received the data that we sent it which is the data that was specified within the mock file. So, if you wanted to have any different types of arguments coming to that function, you could specify here. But remind you this is only for your local development environment, so you won't have to deploy your function to production every time you want to invoke it.
So, if we were to look at the handler file you see that, hey we have our context argument coming in, we have the event coming through and then inside of here we can run any type of JavaScript that we want- any script we write, right. So, since this is the incomplete tasks or what we will do is, write a query that will bring back all the tasks that are not done and after a certain date or after their completion date; so, let's jump in and do that right now.
So, to save a little bit of time I went ahead and wrote the query out in my API Explorer and just tested that it worked and as well as created the argument that we are going to be passing to that query. So, just to walk you through it really quickly what we're going to be doing is passing a date, which is going to be today to our query which then query is a list of to-dos and filters them by where they completed or should they be completed by later than today and are they still pending. So, this will be our list of incomplete reduce. And then we'll get back how many that we have, as well as the description for each of those. And then to get the date for today, simply say, “create a new date”, “convert it to JSON” and then split it on T and give me back the first argument or the first item in the array. So, that will work to give us a date string that looks something along the lines of this… so 2019… let's say 09 for the month and then 25. Cool.
So, now what we're going to do is we're going to say, okay well taking our context argument that we get from the custom function let's get the API and run a Graph QL request; which then we're going to pass the query to is the first argument. And then our variables there's going to be today. And then one thing we're going to do is we're just going to say, “Check permissions false.” The reason we're doing this is, because we do not want to have to specify any type of API token or any type of role of this custom function is going to be inhibited by. We just want to say, all the to-dos that are out there bring them back and we're going to end up just printing to the console for the sake of example.
So, what we're going to do is a wait for this to execute and then we're going to get or store the response right here, cool. And so, then what we want to do very simply is we're going to console.log out the minute report. Then right here just put the response. Perfect! There! And what we can then say is, report for the result on our custom function. We could say, “The report has run.” Cool. So, one second… let me think… that is what it's asking for. Yeah that's it, cool.
So, what's next? Just to mention in this example to where our task is running on, an interval though response does not mean as much as maybe the console.log output would right. So, let's actually take this and deploy it so eight basic one second, I'm going to 8base config, make sure that I'm in the right workspace. So, I'm going to go to my new to-dos, that's the one. Cool. Alright. And then I'm going to run 8base deploy. So, let's give this just a quick minute to upload the new function the functions now deployed. And I've let it stay up there for I'm in there - so we can jump in and actually look at the production logs as they've been running. So, if I move over to my workspace and I go to my logic section I'm going to reload this page and cool our incomplete tasks report is not here and we can see that it runs every minute.
So, let's move over to the function logs which is right here in this tab. And if we do that, cool, so we see that it's been running a number of times and the minute report is giving us our to-dos list. These are all the to-do's that are not done yet, but still have time to be completed. And we can just see that there's only one of them in there that fits the criteria our search criteria. We want to of course to tail these logs and have them start populating in real time right.
So, this is a great example of how you could create any type of reoccurring task or automating any type of reoccurring task by deploying a custom function on a scheduled interval. However, if we look at the documentation, we also see that we could use tasks in a different way. So, for example, if we did not specify a schedule right that task would have been deployed, but we wouldn't really have a way of invoking it. The way that you would invoke it is potentially from other custom functions, whether that be a webhook, resolver or even some type of trigger. And what you can do is, you can use the context our and there's a method on it called, “Invoke function” which you give the function name to you then can specify the arguments that you want passed to your custom function as well as specify whether or not you want to wait for these response. If you say, wait for response false, that function or that task that just got invoked will run asynchronously and you can return an immediate response to whichever function or the script that you are currently working in; rather than having to wait for a task that might take a long time to actually complete.
I hope this was a really valuable video for you, in understanding how to get up and running by writing custom tasks and deploying them to a workspace, as well as how to specify them or when you want them to run an interval or potentially use them from other functions by invoking them with the invoke function method. If you have any questions please leave some comments in the video and looking forward to seeing you in future videos. Take care.
We're excited about helping you achieve amazing results.