我需要一个 Nodejs 调度程序,它允许以不同的时间间隔执行任务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20499225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:29:27  来源:igfitidea点击:

I need a Nodejs scheduler that allows for tasks at different intervals

node.jscronscheduled-tasks

提问by user379468

I am looking for a node job schedule that will allow me to schedule a number of tasks at different intervals. For instance,

我正在寻找一个节点作业计划,它允许我以不同的时间间隔安排许多任务。例如,

  • call function A every 30 seconds
  • call function B every 60 seconds
  • call function C every 7 days
  • 每 30 秒调用一次函数 A
  • 每 60 秒调用一次函数 B
  • 每 7 天调用一次函数 C

I also want to be able to start and stop the process.

我还希望能够启动和停止该过程。

So far, I have looked at:

到目前为止,我已经看过:

  • later- the syntax confuses me, also apparently you cant schedule tasks beyond a month

  • agenda- seems the most promising, however I'm confused about the database functionality

  • timeplan- too simple, can't start and stop

  • 后来- 语法让我感到困惑,而且显然你不能安排超过一个月的任务

  • 议程- 似乎是最有前途的,但是我对数据库功能感到困惑

  • timeplan- 太简单了,无法启动和停止

I find the syntax of the latter confusing.

我发现后者的语法令人困惑。

回答by Tom

I would recommend node-cron. It allows to run tasksusing Cron patterns e.g.

我会推荐node-cron。它允许使用 Cron 模式运行任务,例如

'* * * * * *' - runs every second
'*/5 * * * * *' - runs every 5 seconds
'10,20,30 * * * * *' - run at 10th, 20th and 30th second of every minute
'0 * * * * *' - runs every minute
'0 0 * * * *' - runs every hour (at 0 minutes and 0 seconds)

But also more complexschedules e.g.

但也有更复杂的时间表,例如

'00 30 11 * * 1-5' - Runs every weekday (Monday through Friday) at 11:30:00 AM. It does not run on Saturday or Sunday.

Sample code: running job every 10 minutes:

示例代码:每 10 分钟运行一次作业:

var cron = require('cron');
var cronJob = cron.job("0 */10 * * * *", function(){
    // perform operation e.g. GET request http.get() etc.
    console.info('cron job completed');
}); 
cronJob.start();

You can find more examples in node-cronwiki

您可以在node-cronwiki 中找到更多示例

More on cron configuration can be found on cronwiki

可以在cronwiki上找到有关cron配置的更多信息

I've been using that library in many projects and it does the job. I hope that will help.

我一直在许多项目中使用该库,它完成了这项工作。我希望这会有所帮助。

回答by Ryan Wu

I've used node-cronand agenda.

我使用过node-cron议程

node-cronis a very simple library, which provide very basic and easy to understand api like crontab. It doesn't need any config and just works.

node-cron是一个非常简单的库,它提供了非常基本且易于理解的 api,如 crontab。它不需要任何配置,就可以工作。

var cronJob = require('cron').CronJob;
var myJob = new cronJob('00 30 11 * * 1-5', function(){...});
myJob.start();

agendais very powerful and fit for much more complex services. Think about ifttt, you have to run millions of tasks. agendawould be the best choice.

议程非常强大,适合更复杂的服务。想想ifttt,你必须运行数百万个任务。议程将是最好的选择。

Note: You need Mongodbto use Agenda

注意:您需要使用Mongodb才能使用 Agenda

var Agenda = require("Agenda");
var agenda = new Agenda({db: { address: 'localhost:27017/agenda-example'}});
agenda.every('*/3 * * * *', 'delete old users');
agenda.start();

回答by Richard Xue

I think the best ranking is

我认为最好的排名是

1.node-schedule

2.later

3.crontab

1.节点调度

2.以后

3.crontab

and the sample of node-scheduleis below:

节点调度示例如下:

var schedule = require("node-schedule");
var rule = new schedule.RecurrenceRule();
//rule.minute = 40;
rule.second = 10;
var jj = schedule.scheduleJob(rule, function(){
    console.log("execute jj");
});

Maybe you can find the answer from node modules.

也许你可以从node modules 中找到答案。

回答by raygerrard

I have written a node module that provides a wrapper around setInterval using moment durations providing a declarative interface:

我编写了一个节点模块,它使用提供声明性接口的时刻持续时间为 setInterval 提供了一个包装器:

npm install every-moment

npm 安装每时每刻

var every = require('every-moment');

var timer = every(5, 'seconds', function() {
    console.log(this.duration);
});

every(2, 'weeks', function() {
    console.log(this.duration);
    timer.stop();
    this.set(1, 'week');
    this.start();
});

https://www.npmjs.com/package/every-moment

https://www.npmjs.com/package/every-moment

https://github.com/raygerrard/every-moment

https://github.com/raygerrard/every-moment

回答by Rodrigo Adachi

nodeJS default

nodeJS 默认

https://nodejs.org/api/timers.html

https://nodejs.org/api/timers.html

setInterval(function() {
    // your function
}, 5000);

回答by Simon Rigét

I have written a small module to do just that, called timexe:

我写了一个小模块来做到这一点,称为timexe

  • Its simple,small reliable code and has no dependencies
  • Resolution is in milliseconds and has high precision over time
  • Cron like, but not compatible (reversed order and other Improvements)
  • I works in the browser too
  • 其简单、小巧、可靠的代码且无依赖
  • 分辨率以毫秒为单位,随着时间的推移具有高精度
  • Cron 类似,但不兼容(颠倒顺序和其他改进)
  • 我也在浏览器中工作

Install:

安装:

npm install timexe

use:

用:

var timexe = require('timexe');
//Every 30 sec
var res1=timexe(”* * * * * /30”, function() console.log(“Its time again”)});

//Every minute
var res2=timexe(”* * * * *”,function() console.log(“a minute has passed”)});

//Every 7 days
var res3=timexe(”* y/7”,function() console.log(“its the 7th day”)});

//Every Wednesdays 
var res3=timexe(”* * w3”,function() console.log(“its Wednesdays”)});

// Stop "every 30 sec. timer"
timexe.remove(res1.id);

you can achieve start/stop functionality by removing/re-adding the entry directly in the timexe job array. But its not an express function.

您可以通过直接在 timexe 作业数组中删除/重新添加条目来实现启动/停止功能。但它不是一个明确的功能。