简体   繁体   English

解析服务器 - 如何使用调度程序一遍又一遍地运行“作业”

[英]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.我花了一整天的时间试图让某种调度程序与我的解析服务器(在 AWS Beanstalk Node.js 上运行)一起工作,我能够在 js 文件中获取一些代码,但它似乎没有工作。 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.您已为解析服务器作业计划配置 nodejs cron 作业。

  1. install "CRON" module - npm install cron,(reference: https://www.npmjs.com/package/cron ).安装“CRON”模块 - npm install cron,(参考: 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");

(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.) (免责声明,我之前没有使用过 Parse,但在查看它之后,Parse 似乎是由 nodejs 和 express 运行的,所以这应该可以工作。)

node-schedule is a simple to set up module for scheduling jobs. node-schedule 是一个简单的设置用于调度作业的模块。 To give it a try, run this in your main project directory that has your node_modules and package.json.要试一试,请在包含 node_modules 和 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.将此代码添加到一个单独的文件中,例如“schedule.js”,并在您的主启动文件中引入它,或者直接将其放入主启动文件中。 Based on the parse video tutorial screenshots I saw, the file might be called 'index.js':根据我看到的解析视频教程截图,该文件可能被称为“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.通过将 console.log 作业设置为从启动服务器开始运行一分钟来测试它,您应该会在启动后不久看到输出“作业开始于...”。

Once it's working, edit the console.log line to call the functions that you usually call through the dashboard.运行后,编辑 console.log 行以调用您通常通过仪表板调用的函数。

Edit the day of week array to be the days you want this to run.将星期几数组编辑为您希望它运行的日期。 0 is Sunday, 6 is Saturday. 0 是星期日,6 是星期六。 Edit the rule.hour and rule.minute as needed.根据需要编辑 rule.hour 和 rule.minute。

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.通过指定要运行的规则和函数,然后将规则和函数传递给 schedule.scheduleJob,您可以启动任意数量的作业,如上所示。

you can use this parse-server-scheduler npm-module to make it work out of the box.你可以使用这个parse-server-scheduler npm-module 让它开箱即用。

The alternative, is that you need another service to call the job HTTP-endpoints yourself.另一种方法是,您需要另一个服务来自己调用作业 HTTP 端点。
There are pros and cons with both directions, but in general you want internal scheduling anyways.两个方向都有利有弊,但总的来说,您还是希望进行内部调度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM