Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Server - How to use a scheduler to run 'jobs' over and over

I have spent an entire day trying to get some kind of scheduler working with my Parse Server (running on AWS Beanstalk Node.js) I was able to get some code inside the js file but it did not seem like it was working. Is there any way I can set up some kind of scheduler so I don't have to manually run my jobs through the Dashboard?

like image 656
D34thSt4lker Avatar asked Nov 22 '16 22:11

D34thSt4lker


2 Answers

You have Configure the nodejs cron job for parse-server job schedule.

  1. install "CRON" module - npm install cron,(reference:https://www.npmjs.com/package/cron).

  2. Change the parse server job Schedule function declaration. Parse.Cloud.job("jobScheduleName", function(){ })
    to
    function jobScheduleName() { };

  3. Run cron

    var CronJob = require('cron').CronJob; //include dependency
    //add this code to run job every 15 minute.
    //set your time as per your run schedule.
    new CronJob('0 */15 * * * *', function() {
        Cron.jobScheduleName();
    }, null, true,"timeZone");
like image 71
IftekharDani Avatar answered Oct 10 '22 04:10

IftekharDani


(disclaimer, I haven't used Parse before, but after looking into it, Parse appears to be run by nodejs and express, so this should work.)

node-schedule is a simple to set up module for scheduling jobs. To give it a try, run this in your main project directory that has your node_modules and package.json.

npm install node-schedule --save

add this code to a separate file, like 'schedule.js' and require it in your main start file, or put it directly in the main start up file. Based on the parse video tutorial screenshots I saw, the file might be called 'index.js':

var schedule = require('node-schedule');

// start a job running Monday through Friday at 2:15 PM
var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [1,2,3,4,5];
rule.hour = 14;
rule.minute = 15;

var startJob = schedule.scheduleJob(rule, function () {
  console.log('job started at ' + new Date());
});

Test it out by setting up the console.log job to run a minute from when you start your server and you should see the output 'job started at...' soon after start up.

Once it's working, edit the console.log line to call the functions that you usually call through the dashboard.

Edit the day of week array to be the days you want this to run. 0 is Sunday, 6 is Saturday. Edit the rule.hour and rule.minute as needed.

You can start as many jobs as you like by specifying a rule and a function to run, then passing the rule and the function to schedule.scheduleJob as shown above.

like image 42
curtwphillips Avatar answered Oct 10 '22 04:10

curtwphillips