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

3

5

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?

Radnorshire answered 22/11, 2016 at 22:28 Comment(3)
lotsa libs will do that .... github.com/scripting/nodeRunner or use cronCrinum
This will work with Parse Server?? I have already tried npm node-cron and it said it would work but either I just didnt know what I was doing or the code wouldnt run (There were no errors)Radnorshire
psrvr is process #1. proc #2 can be run such as crond for other jobsCrinum
C
8

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");
Croquet answered 26/11, 2016 at 6:7 Comment(3)
I have set up everything just as you how it. but nothing seems to be running. except the server seems to be slowed down and breaking when I have the cron job in the Cloud Code. Any ideas?Radnorshire
Please provide the more description and code so i can understand.Croquet
Ok!! I Got it to work! I messed up in the function of the Cron job. ThanksRadnorshire
P
2

(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.

Pesticide answered 23/11, 2016 at 7:34 Comment(1)
Thanks, ill give this a tryRadnorshire
A
1

you can use this parse-server-scheduler npm-module to make it work out of the box.

The alternative, is that you need another service to call the job HTTP-endpoints yourself.
There are pros and cons with both directions, but in general you want internal scheduling anyways.

Ariel answered 8/6, 2020 at 9:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.