I need a Nodejs scheduler that allows for tasks at different intervals
node.jsCronScheduled Tasksnode.js Problem Overview
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
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.
node.js Solutions
Solution 1 - node.js
I would recommend node-cron
. It allows to run tasks using Cron patterns e.g.
'* * * * * *' - 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:
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
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.
Solution 2 - node.js
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.
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.
Note: You need Mongodb to use Agenda
var Agenda = require("Agenda");
var agenda = new Agenda({db: { address: 'localhost:27017/agenda-example'}});
agenda.every('*/3 * * * *', 'delete old users');
agenda.start();
Solution 3 - node.js
I think the best ranking is
> 1.node-schedule > > 2.later > > 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.
Solution 4 - node.js
I have written a node module that provides a wrapper around setInterval using moment durations 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();
});
Solution 5 - node.js
nodeJS default
https://nodejs.org/api/timers.html
setInterval(function() {
// your function
}, 5000);
Solution 6 - node.js
I have written a small module to do just that, called 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
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.