Background Jobs/Scheduling

Background Jobs

Background Jobs are Cloud Code functions that are intended for longer-running tasks. They can run up to 15 minutes. They have a different declaration than Cloud Code declarations, here’s an example:

Parse.Cloud.job(“myBackgroundJob”, function(request, status) {
// Your background job code.
});

All Background Job declarations (and for that matter all Cloud Code declarations) need to live in the main.js file of the cloud folder. For more details see our Cloud Code documentation.

For more details on Background Jobs see here.

We have deployed Scheduled Background Jobs and Cloud Code UI in the dashboard. The below is for historical/troubleshooting only.

Scheduled Background Jobs and Cloud Code functions REST API

Both Background Jobs and Cloud Code functions can be scheduled via the Scheduler REST API. An easy way to interact with the REST API is to use the curl command line tool. All the examples below use curl. See here for more details on curl.

In the following curl examples make sure to replace {Parse on Buddy App ID} with the app ID of your app, and {Parse on Buddy App Master Key} with your app’s master key.

To create a schedule:

Here are the parameters you can use to specify a schedule. They need to be passed to the REST scheduler endpoint in JSON format (see below curl examples):

frequency – Can have the following values: minute, hour, day, week, month.
interval – Number of intervals at the specified frequency. For instance if frequency is day, and interval is 5, then the schedule will fire every 5 days.
start_time – Time when you want the job to start, in number of seconds since the Unix epoch (January 1, 1970 UTC). If unspecified, schedules start immediately.
action.type – Can have the following values: job or function.
action.name – The name of the job or function you are scheduling.
action.body – JSON that is passed in each request to your code. They can be whatever you’d like. You can think of this as the ‘body’ of the schedule’s request.
count – Maximum number of times for schedule to run.

Here is an example of a Background Job schedule named myJobSchedule that runs a Background Job named myJob, once a minute:

curl -X POST -d ‘{ “interval”: 1, “frequency”: “minute”, “action”: { “type”: “job”, “name”: “myJob”, “body”: {}}}’ -H “X-Parse-Application-Id: {Parse on Buddy App ID}” -H “X-Parse-Master-Key: {Parse on Buddy App Master Key}” -H “Content-Type: application/json” https://parse.buddy.com/schedule/myJobSchedule

You can name each schedule however you’d like.

Here is an example of a Cloud Code function schedule named myCoolCCFunctionSchedule that runs a Cloud Code function named myFunction, every other day:

curl -X POST -d ‘{ “interval”: 2, “frequency”: “day”, “action”: { “type”: “function”, “name”: “myFunction”, “body”: {}}}’ -H “X-Parse-Application-Id: {Parse on Buddy App ID}” -H “X-Parse-Master-Key: {Parse on Buddy App Master Key}” -H “Content-Type: application/json” https://parse.buddy.com/schedule/myCoolCCFunctionSchedule

To list all schedules:

curl -H “X-Parse-Application-Id: {Parse on Buddy App ID}” -H “X-Parse-Master-Key: {Parse on Buddy App Master Key}” -H “Content-Type: application/json” https://parse.buddy.com/schedule

To list the history of a schedule:

curl -H “X-Parse-Application-Id: {Parse on Buddy App ID}” -H “X-Parse-Master-Key: {Parse on Buddy App Master Key}” -H “Content-Type: application/json” https://parse.buddy.com/schedule/{schedule name}/history

where {schedule name} is the name of the schedule in question.

To delete a schedule:

curl -X DELETE -H “X-Parse-Application-Id: {Parse on Buddy App ID}” -H “X-Parse-Master-Key: {Parse on Buddy App Master Key}” -H “Content-Type: application/json” https://parse.buddy.com/schedule/{schedule name}

where {schedule name} is the name of the schedule in question.