After launching Advanced Cloud Code the top most asked question amongst our customers was “Are Background jobs in the roadmap?” So we did it - now you can run scheduled tasks on your app.
Because they give you the comfort to write a function once and set it up to run as often as you want. Background Jobs allow you to build long-running tasks by writing code similar to Cloud Functions.
Let’s say you have an app game with free and paid user accounts. The second group has access to more features and you want to make a regular check if a user has an active paid account.
Of course, it will be very hard to do this check every couple of hours and as developers, we always think of a logical solution to a problem. This is a good time to use background jobs - we can write a simple function that checks twice a day all the users if they have inactive payment their plan changes to “free”.
You do it once, schedule it and update it anytime you want to change its functionality.
First, we’ll need some users that we want to check for payment status. Enter SashiDo’s dashboard and choose an app you want to work on. Create a new Class named User and add two Objects.
Function checkUserStatus(); checks all instances of Class User with a loop and if the payment status of the user. If it’s inactive the plan changes to free.
Now go to your appfolder/cloud/main.js file in the same repo and include the file we’ve just created:
It’s time to commit and push the changes. They will automatically deploy to your app so you don’t have to do it additionally. Enter SashiDo’s Dashboard and open Core > Cloud Code. You’ll see a button saying that the Cloud Code is building at the moment - wait until it says the process is finished.
Next, you have to go to Scheduled Jobs (Your app > Core > Jobs). Here you can manage your Background Jobs, create, edit and delete. Click the button to create a new Cloud Job and name it, then add JSON parameters (in our case it’s the new status of the user):
You can choose if and how often the jobs will repeat. We’ll choose twice a day. After the setup is finished we can start the function immediately in order to see if it’s working. We can see the result in Jobs Status - go there and refresh the page. You can also see input and result of the function in your Logs (Your app > Core > Logs).
Voilà, our first Background jobs are working!
Now you can build your own jobs according to your needs, it’s easy and fun.
You can use Background Jobs whenever you want to run a process without user intervention like logging, user notification, monitoring etc., when you need to automate a process that needs to be running repeatedly in your app.
This is the basic setup of SashiDo’s Background Jobs.
Now you can start building Cloud functions which save you time and effort.