I am looking for a node job schedule that will allow me to schedule a number of tasks at different intervals. For instance,
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.
This question is related to
node.js
cron
scheduled-tasks
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();
});
nodeJS default
https://nodejs.org/api/timers.html
setInterval(function() {
// your function
}, 5000);
I have written a small module to do just that, called timexe:
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.
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.
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();
Source: Stackoverflow.com