Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is everyone going about implementing scheduled jobs / cloud jobs on parse-server?

According to the parse-server migration guide we could use something like Kue and Kue-UI to emulate the parse.com scheduled jobs functionality.

I haven't implemented Kue or Kue-ui, but looking at the guides, it doesn't look like it provides anywhere close to the same level of functionality as the existing parse.com scheduled jobs. Is this observation correct? Has someone implemented this? Is it true that jobs have to be scheduled through Kue in javascript and Kue-ui only provides a summary of the current status of the jobs and new schedules can't be added through Kue-ui?

Has anyone tried to achieve the same outcome with something like Jenkins? So this is what I had in mind:

  • each job would still be defined in the cloud code Parse.Cloud.job("job01", function(request, response) {));
  • modify parse-server slightly to expose job at a similar url to existing cloud functions e.g. /parse/jobs/job01 (this might exist in parse-server soon: github.com/ParsePlatform/parse-server/pull/2560)
  • create a new jenkins job do a curl at that url
  • define a cron like schedule for that jenkins job from within the jenkins web ui

I can see the benefits being:

  • little to no coding
  • setting up jenkins sounds like much less work then setting up kue, redis and kue-ui
  • existing cloud job / definitions stay exactly the same
  • schedule and manually trigger jobs through the jenkins web ui

The only thing that the current parse.com schedule jobs / cloud jobs can do that a jenkins based solution can't is being able to select a job name to create a new schedule for from a drop down list.

Am I missing something? How is everyone else going about this? Thanks.

like image 502
asdf01 Avatar asked Aug 30 '16 01:08

asdf01


2 Answers

I ended up doing this with ´node-schedule´. Not sure if it is the best option but it is working fine for me.

index.js

var schedule = require('node-schedule');
var request = require('request');
schedule.scheduleJob('*/15 * * * *', function() {
    var options = {
        url: serverUrl + '/functions/{function name}}',
        headers: {
            'X-Parse-Application-Id': appID,
            'X-Parse-Master-Key': masterKey,
            'Content-Type': 'application/json'
        }
    };
    request.post(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body);
        }
    });
});

main.js

Parse.Cloud.define('{function name}', function(req, res) {
  //Check for master key to prevent users to call this 
  if (req.master === true) {
    //Do operations here
  } else {
    res.error('Need Master Key');
  }
});
like image 136
rob180 Avatar answered Nov 16 '22 17:11

rob180


I use kue for this purpose. I've described the appoach in my article. In short, this function:

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);
      }
  });
});

becomes this:

// Create a kue instance and a queue.
var kue = require('kue-scheduler');
var Queue = kue.createQueue();
var jobName = "sendReport";

// 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));
  });
}

It works fine, though I noticed that sometimes kue-scheduler fires the event more often than needed. See this issue for more info: https://github.com/lykmapipo/kue-scheduler/issues/45

like image 37
Andrey Gordeev Avatar answered Nov 16 '22 15:11

Andrey Gordeev