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

4

6

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.

Dragonfly answered 30/8, 2016 at 1:56 Comment(1)
I've wrote an article about this: andreygordeev.com/2016/08/04/…Paulo
H
5

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');
  }
});
Hileman answered 30/8, 2016 at 8:29 Comment(0)
P
1

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

Paulo answered 31/8, 2016 at 4:55 Comment(3)
Hi @andrey-gordeev. Thanks for your feedback. Although you haven't indicated directly, but am I correct in concluding that you cannot schedule a job through some sort of UI with any of the kue, kue-ui or kue-scheduler combinations? Thanks.Dragonfly
@Dragonfly kue has a separate UI component, but I haven't tried it.Paulo
Wander what's the use for Parse.Cloud.job if I change to kue-scheduler?Filth
C
0

If you are on AWS this may be an option:

  1. Create a AWS CloudWatch Event Rule that is triggered at specific intervals and calls a Lambda function. The Event Rule can pass parameters to the Lambda function.

  2. Create a simple Lambda function that calls a Cloud Code function / job. If you receive the cloud code function name and other parameters from the Event Rule, you only need one generic Lambda function for any Cloud Code call.

This has several advantages as Event Rules are part of AWS infrastructure and can be easily integrated with other AWS services. For example, you can set up intelligent queueing of Event Rule calls so that if the previous call did not complete yet, you discard the next call in the queue, overflow to another queue, notify an operator, etc.

Cosher answered 23/4, 2020 at 16:35 Comment(0)
E
0

You can use the parse-server-scheduler npm-module.

It does not require any external server and simply allows you to setup scheduling in parse-dashboard.

Economist answered 8/6, 2020 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.