Getting started with Background Jobs on SashiDo
Tags: backgroundjobs, tutorial, beginners, parseserver
Published: 2019-10-25 10:56:24

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. 

#### Why do I need Background Jobs?

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. 

####How to use Background Jobs on SashiDo? 

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. 

Add parameters “plan” and “payment”. Set payment with different values in order to check later if our function is working correctly. ![](https://media-blog.sashido.io/content/images/2016/09/img_4.png)

Go to your private GitHub repository where your Cloud Code is stored and clone it if you haven’t done it yet. Go to the cloud directory in the cloned project: <span class="pre">cd appfolder/cloud</span> And make a new javascript file in it with name checkUserStatus.js. Now let’s write the function that does the check: <script src="https://gist.github.com/pkeremidchieva/6de73f3fac7d8cd0ab60934f05dbd836.js"></script> 

*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: <script src="https://gist.github.com/pkeremidchieva/59587f0cddbb812075188f37d38a8c47.js"></script>

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): ![](https://media-blog.sashido.io/content/images/2016/09/img_3.png)

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). ![](https://media-blog.sashido.io/content/images/2016/09/Screen-Shot-2016-09-18-at-14-43-00.png)

Looks like our function is working, let’s, more importantly, see if it does the job. Go in the Browser and check class User: ![](https://media-blog.sashido.io/content/images/2016/09/Screen-Shot-2016-09-18-at-23-05-33.png) 

**Voilà, our first Background jobs are working!** 


Now you can build your own jobs according to your needs, it’s easy and fun. 

#### Background Jobs use cases

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.<br />Now you can start building Cloud functions which save you time and effort. 

Happy coding!