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

时间:2021-12-24 02:11:59

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
  • 每30秒调用函数A
  • call function B every 60 seconds
  • 每60秒调用函数B
  • call function C every 7 days
  • 每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

    时间计划——太简单了,不能开始也不能停止

I find the syntax of the latter confusing.

我发现后者的语法很混乱。

6 个解决方案

#1


176  

I would recommend node-cron. It allows to run tasks using 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 complex schedules 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-cron wiki

你可以在node-cron wiki上找到更多的例子

More on cron configuration can be found on cron wiki

更多关于cron配置的信息可以在cron wiki上找到

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

我在很多项目中都使用过这个库,它可以完成任务。我希望这能有所帮助。

#2


30  

I've used node-cron and agenda.

我用过点头和议程。

node-cron is 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();

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

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

Note: You need Mongodb to use Agenda

注意:您需要Mongodb来使用议程

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

#3


10  

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

#4


8  

I think the best ranking is

我认为最好的排名是

1.node-schedule

1. node-schedule

2.later

2.之后

3.crontab

3. crontab

and the sample of node-schedule is 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.

也许您可以从节点模块中找到答案。

#5


6  

nodeJS default

nodeJS违约

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

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

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

#6


0  

I have written a small module to do just that. You can find it here:
https://github.com/paragi/timexe.git
https://www.npmjs.com/package/timexe

我已经写了一个小模块来做这个。您可以在这里找到它:https://github.com/i/timexe.git https://www.npmjs.com/package/timexe

Some of the features:

的一些特点:

  • 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)
  • Cron喜欢,但不兼容(颠倒顺序和其他改进)
  • I works in the browser too
  • 我也在浏览器中工作

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作业数组中删除/重新添加条目来实现启动/停止功能。但它不是一个表达函数。

#1


176  

I would recommend node-cron. It allows to run tasks using 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 complex schedules 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-cron wiki

你可以在node-cron wiki上找到更多的例子

More on cron configuration can be found on cron wiki

更多关于cron配置的信息可以在cron wiki上找到

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

我在很多项目中都使用过这个库,它可以完成任务。我希望这能有所帮助。

#2


30  

I've used node-cron and agenda.

我用过点头和议程。

node-cron is 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();

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

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

Note: You need Mongodb to use Agenda

注意:您需要Mongodb来使用议程

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

#3


10  

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

#4


8  

I think the best ranking is

我认为最好的排名是

1.node-schedule

1. node-schedule

2.later

2.之后

3.crontab

3. crontab

and the sample of node-schedule is 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.

也许您可以从节点模块中找到答案。

#5


6  

nodeJS default

nodeJS违约

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

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

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

#6


0  

I have written a small module to do just that. You can find it here:
https://github.com/paragi/timexe.git
https://www.npmjs.com/package/timexe

我已经写了一个小模块来做这个。您可以在这里找到它:https://github.com/i/timexe.git https://www.npmjs.com/package/timexe

Some of the features:

的一些特点:

  • 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)
  • Cron喜欢,但不兼容(颠倒顺序和其他改进)
  • I works in the browser too
  • 我也在浏览器中工作

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作业数组中删除/重新添加条目来实现启动/停止功能。但它不是一个表达函数。