How to get Parse Background Job functionality on Parse Server

One of important features missing in Parse Server is background scheduled job functionality. In this doc Parse Team recommends us to use kue for this purpose. Well, let’s check it out!

kue is open source Node.js module, developed for managing a priority job queue. However, it doesn’t allow to schedule jobs out of the box, so we’ll need another utility called kue-scheduler for this. Both of them are easy to install and use.

Prerequisites: kue is backed by Redis, so we’d need to set Redis up before we start integrating kue. DigitalOcean has a great tutorial how to install and configure Redis on your server: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis. Please follow it until Redis Operations part, then come back here.

After you’ve installed Redis, run this command to install kue and kue-scheduler globally:

sudo npm install -g kue kue-scheduler

Now we’re able to adapt our CloudCode for Parse Server. Let’s say you had a background job for posting a POST request to a webhook every hour. Here’s how it would look like on good ol’ Parse.com:

Parse.Cloud.job("sendReport", function(request, response) {
Parse.Cloud.httpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
url: "https://example.com/url/", // Webhook url
body: "body goes here",
success: function(httpResponse) {
console.log("Successfully POSTed to the webhook");
},
error: function(httpResponse) {
console.error("Couldn't POST to webhook: " + httpResponse);
}
});
});

However, Parse Server doesn’t have Parse.Cloud.job function, so it will throw the following error:

TypeError: Parse.Cloud.job is not a function

To fix the error, replace that part by this code:

// Create a kue instance and a queue.
var kue = require('kue-scheduler');
var Queue = kue.createQueue();
var jobName = "sendReport";
// Clear all previous jobs.
Queue.clear(function(error, response) {
// Create a job instance in the queue.
var job = Queue
.createJob(jobName)
// Priority can be 'low', 'normal', 'medium', 'high' and 'critical'
.priority('normal')
// We don't want to keep the job in memory after it's completed.
.removeOnComplete(true);

// Schedule it to run every 60 minutes. Function every(interval, job) accepts interval in either a human-interval String format or a cron String format.
Queue.every('60 minutes', job);

// Processing a scheduled job.
Queue.process(jobName, sendReport);
});
// The body of job goes here.
function sendReport(job, done) {
Parse.Cloud.httpRequest({
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
url: "https://example.com/url/", // Webhook url
body: "body goes here"}).then(function(httpResponse) {
console.log("Successfully POSTed to the webhook");
// Don't forget to run done() when job is done.
done();
}, function(httpResponse) {
var errorMessage = "Couldn't POST to webhook: " + httpResponse;
console.error(errorMessage);
// Pass Error object to done() to mark this job as failed.
done(new Error(errorMessage));
});
}

That’s it! This Cloud Code would run just fine on Parse Server. This is a pretty basic example of what kue can do, so I strongly recommend you to check out it’s documentation.