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

查看:342
本文介绍了我需要一个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:


  • a href =https://github.com/bunkat/later>稍后 - 语法使我感到困惑,显然您无法安排超过一个月的任务。

  • 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 - to simple, can't start and stop

我发现后者混淆的语法

推荐答案

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

但更多复杂时间表

'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

有关cron配置的更多信息,请访问 cron wiki

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天全站免登陆