I need a Nodejs scheduler that allows me to complete tasks at different intervals

I am looking for a node work schedule that will allow me to schedule a series of tasks at different intervals. For example,

  • call function A every 30 seconds
  • call function B every 60 seconds
  • C call function every 7 days

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

So far I watched:

  • later - the syntax bothers me, also, apparently, you cannot schedule tasks for a month

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

  • timeplan - too simple, cannot start and stop

I find the syntax of the latter confusing.

+117
cron scheduled-tasks
Dec 10 '13 at 16:02
source share
6 answers

I would recommend node-cron . It allows you to run tasks using Cron templates, for example.

 '* * * * * *' - 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 complicated schedules, for example

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

Code example : work is done every 10 minutes:

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

You can find more examples in the node-cron wiki

More information on cron configuration can be found on the cron wiki

I use this library in many projects and it does the job. Hope this helps.

+203
Dec 10 '13 at 16:13
source share

I used node-cron and agenda .

node-cron is a very simple library that provides a very simple and intuitive api like crontab. It does not need any configuration and just works.

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

the agenda is very powerful and suitable for much more complex services. Think about ifttt , you need to run millions of tasks. an agenda would be a better choice.

Note. To use the Agenda

you need Mongodb
 var Agenda = require("Agenda"); var agenda = new Agenda({db: { address: 'localhost:27017/agenda-example'}}); agenda.every('*/3 * * * *', 'delete old users'); agenda.start(); 
+35
Dec 10 '13 at 16:23
source share

I wrote a node module that provides a wrapper around setInterval using a length of time providing a declarative interface:

npm install every moment

 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://github.com/raygerrard/every-moment

+10
Jan 12 '15 at 2:08
source share

I think the best rating

1.node graph

2.later

3.crontab

and the node-schedule sample 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"); }); 

You may find the answer from node modules .

+10
May 16 '15 at 5:38
source share

nodeJS by default

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

 setInterval(function() { // your function }, 5000); 
+6
May 15, '15 at 10:38
source share

I wrote a small module for this called timexe :

  • Its simple, small, reliable code and has no dependencies
  • Millisecond resolution and high accuracy over time
  • Cron is similar but not compatible (reverse order and other improvements)
  • I also work in the browser

Installation:

 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 by deleting / re-adding the record directly to the timexe task array. But this is not an express function.

0
Jun 27 '15 at 11:19
source share



All Articles