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?
You have Configure the nodejs cron job for parse-server job schedule.
install "CRON" module - npm install cron,(reference:https://www.npmjs.com/package/cron).
Change the parse server job Schedule function declaration.
Parse.Cloud.job("jobScheduleName", function(){ })
to
function jobScheduleName() { };
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");
(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.
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.
© 2022 - 2024 — McMap. All rights reserved.