Skip to content

Instantly share code, notes, and snippets.

@TalhaAwan
Last active August 3, 2017 18:41
Show Gist options
  • Save TalhaAwan/3a3e8c2e87646d1b5f8adb55d50663ef to your computer and use it in GitHub Desktop.
Save TalhaAwan/3a3e8c2e87646d1b5f8adb55d50663ef to your computer and use it in GitHub Desktop.
Example node js scheduler to run jobs at intervals of seconds, minutes and days of weeks
const CronJob = require('cron').CronJob;
const moment = require('moment-timezone');
const cronJobs = [
{
cronTime: "*/20 * * * * *",
job: function (){
console.log("Twenty seconds job has run");
}
},
{
cronTime: '00 */2 * * * *',
job: function (){
console.log("Two minutes job has run");
}
},
{
cronTime: "00 */3 * * * *",
job: function (){
console.log("Three minutes job has run");
}
},
{
cronTime: "00 */5 * * * *",
job: function (){
console.log("Five minutes job has run");
}
},
{
cronTime: "00 00 9 * * 1",
job: function (){
console.log("Monday 7 am job has run");
}
},
{
cronTime: "00 00 9 * * 3",
job: function (){
console.log("Wednesday 9 am job has run");
}
},
{
cronTime: "00 00 14 * * 4",
job: function (){
console.log("Thursday 2 pm job has run");
}
},
];
cronJobs.forEach(function (cj) {
var job = new CronJob({
cronTime: cj.cronTime,
onTick: cj.job,
start: false,
timeZone: 'America/Los_Angeles'
});
job.start();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment