Sunday, 23 October 2016

Parse Cloud : Background Jobs

Today's tutorial is regarding Parse. Parse is an Open source backend or Parse can be roughly described as a could for hosting the APIs that your mobile app needs to communicate with in order to function. It also helps you send push notifications to your app users in order to communicate with them or bring them back to your app. If you are building a small application that consists of user login and after logging in you have some small stuff to do that requires a small logic which is user as the main point in the application then Parse can be a good option for that as it provides you a facility for cloud code and jobs that are used to run background long running function without the callbacks from your code. Other alternatives for Parse are Back4App, Firebase, Digital Ocean. This tutorial is regarding Parse server hosting on Back4App.

Getting Started
Cloud Code is built into Parse Server. The default entry point for your Cloud Code is at ./cloud/main.js.

What is Cloud Code?
For complex apps, sometimes you just need a bit of logic that isn’t running on a mobile device. Cloud Code makes this possible.
Cloud Code is easy to use because it’s built on the same JavaScript SDK that powers thousands of apps. The only difference is that this code runs in your Parse Server rather than running on the user’s mobile device. When you update your Cloud Code, it becomes available to all mobile environments instantly. You don’t have to wait for a new release of your application. This lets you change app behavior on the fly and add new features faster.
Even if you’re only familiar with mobile development, Cloud Code is straightforward and easy to use and understand.

Cloud Jobs
Sometimes you want to execute long running functions, and you don’t want to wait for the response. Cloud Jobs are just meant for that.

Define a Job

Parse.Cloud.job("myCloudJob", function(request, status) {
  Parse.Cloud.httpRequest({
      url: 'http://nishantsh.blogspot.in/2016/10/parse-cloud-background-jobs.html'
      success: function(httpResponse) {
        response(httpResponse.text);
      },
      error: function(httpResponse) {
        response('Request failed with response code ' + httpResponse.status)
      }
    });
});
:-Note that calling status.success or status.error won’t prevent any further execution of the job.

Running a Job
Calling jobs is done via the REST API and is protected by the master key.
1.What you need to do is set the following:

url: https://api.parse.com/1/jobs/[name of your test job] (maybe you need to encode the space to a %20 but I would recommend do start without spaces)
2.method: POST

3.request header:

X-Parse-Application-Id: [your app id]
X-Parse-REST-API-Key: [your master key] //(find it here: goto your app => CoreSettings)
Content-Type: application/json

That's all. Run it.
You can pass some data alongside the call if you want to customize the job execution.

Scheduling a Job
You can schedule your jobs according to your requirements that is you can run them manually also or you can schedule them to run on their own at some intervals. You can schedule your jobs as follows:
Goto BackgroundJobs => Schedule a Job => fill in and click save. Then from Browser a Job you can run your job manually.

That's all.