我需要一个 Nodejs 调度程序,它允许以不同的时间间隔执行任务 [英] I need a Nodejs scheduler that allows for tasks at different intervals

查看:23
本文介绍了我需要一个 Nodejs 调度程序,它允许以不同的时间间隔执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  • 每 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 - 太简单了,无法启停

timeplan - too simple, can't start and stop

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

I find the syntax of the latter confusing.

推荐答案

我会推荐 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.

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

Sample code: running job every 10 minutes:

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();

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

You can find more examples in node-cron wiki

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

More on cron configuration can be found on cron wiki

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

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

这篇关于我需要一个 Nodejs 调度程序,它允许以不同的时间间隔执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆