I am trying to schedule a Cron Job between 9AM to 9PM for every 15 minutes. I am able to schedule it for every 15 minutes, but not for the time duration. Below is the code snippet
const CronJob = require('cron').CronJob
const splunkNode = require('./splunk_node')
let job = new CronJob("*/15 08-21 * * * 1-5", function(){
console.log('ran at ', new Date())
// CRON JOB()
},function(){
console.log('Job stopped')
},true,'America/Los_Angeles')
job.start()
Does the syntax for specifying range has to be enclosed between brackets[]?
A lil workaround is to schedule a function that calls itself after a certain amount of time
function callCronJob (){
const job = new CronJob("*/15 08-21 * * * 1-5", function(){
// if time range we are in doesn't fall between 9AM and 9PM cancel this job and call // this function again with it's 9AM
// next part is pseduocode cause I am lazy to write actual code
if ( 9am >timeRange > 9pm ){
job.cancel()
new cronJob(timeRange+ 12 hours , ()=>{
callCronJob();
});
return;
}
// CRON JOB()
},function(){
console.log('Job stopped')
},true,'America/Los_Angeles')
job.start()
}
Related
I have written on cron job to update users table using Nodejs. I have used cron node package. I need to run cron 4 times a day. I have written 2 crons. The first cron will run 4 times a day with particular time duration and will start the second cron. The second cron will run in every 5 minutes till all users updated. after updating of users the second cron will stop.
sample code :
//This cron will run at every 5 minutes
var job = new CronJob({
cronTime: '*/5 * * * *',
onTick: function() {
//cron runs in every 5 min;
/* API logic */
// when all user updated this cron will stopped
job.stop();
},start: false
});
//runs on 7est,8est,9est,10est,11est
var mainjob = new CronJob('0 0 7-11 * * *', function() {
job.start();
}, function () {},true
);
First time It's running well but the second time not started. Can you please advice on this?
I am trying to impliment into my nodejs script a function to allow once per 8 hours a select command.
example:
!hug <--- would let bot respond with a hug but only once every 8 hours
I've been scouring online but cannot find what I need... I am trying to get it as simplified as possible.. (i.e without mongo... etc)
You can use node-schedule for this and for more versatility where you can configure days, hours and minutes and cancel on particular conditions being met this also gives you to use cron expressions as well.
var schedule = require("node-schedule");
var rule = new schedule.RecurrenceRule();
//Will run at 1am 9am and 5pm
rule.hour = [1, 9, 17];
var task = schedule.scheduleJob(rule, function(){
//Do Stuff
console.log("Scheduled Task Running...")
/*
if(condition met)
task.cancel();
*/
});
You can use node-cron
var CronJob = require('cron').CronJob;
var job = new CronJob('00 30 11 * * 1-5', function() {
/*
* Runs every weekday (Monday through Friday)
* at 11:30:00 AM. It does not run on Saturday
* or Sunday.
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);
I'm looking for a schedular/ cron for nodejs.
But I need an important feature- if the jobs did not finish (when the time for it to start again arrived), I want it to not start/ delay the schedule.
For example, I need to run a job every 5 minutes. The job started at 8:00, but finished only at 8:06. so I want the job of 8:05 to either wait until 8:06, or not to start at all, and wait for the next cycle at 8:10.
Is there a package that does that? If not, what is the best way to implement this?
You can use the cron package. It allows you to start/stop the cronjob manually. Which means you can call these functions when your cronjob is finished.
const CronJob = require('cron').CronJob;
let job;
// The function you are running
const someFunction = () => {
job.stop();
doSomething(() => {
// When you are done
job.start();
})
};
// Create new cronjob
job = new CronJob({
cronTime: '00 00 1 * * *',
onTick: someFunction,
start: false,
timeZone: 'America/Los_Angeles'
});
// Auto start your cronjob
job.start();
You can implement it by yourself:
// The job has to have a method to inform about completion
function myJob(input, callback) {
setTimeout(callback, 10 * 60 * 1000); // It will complete in 10 minutes
}
// Scheduler
let jobIsRunning = false;
function scheduler() {
// Do nothing if job is still running
if (jobIsRunning) {
return;
}
// Mark the job as running
jobIsRunning = true;
myJob('some input', () => {
// Mark the job as completed
jobIsRunning = false;
});
}
setInterval(scheduler, 5 * 60 * 1000); // Run scheduler every 5 minutes
I am trying to run a cron job after 10 minutes, sometimes it runs after 10 minutes and sometimes it runs after like 2 minutes when I call the webservice. Below is the code
router.post('/getUser', function (req, res) {
var task = cron.schedule('0 */10 * * * *', function () {
console.log("cron job started")
}, false);
task.start();
})
It should always runs after 10 minutes not like sometime 2 minutes as soon as the webservice is called.
The cron syntax says to run the command at a fix time not after an interval.
The */10 means execute the command if the modulo is 0
In your case the code will be excecuted at second 0 of every 10 minutes at every hour at every day and so on.
So your cron will be executed for instance at
09:00, 09:10, 09:20, 09:30 and so on.
The only way I know with build in methods is to use something like
setTimeout(myFunc, 10 * 60 * 1000);
An other option is to set a fixed cron running at the calculated correct time now +10 minutes with moment.js where you specify the exact execution time.
Example
var moment = require('moment')
router.post('/getUser', function (req, res) {
var cronString = moment().second() +' '+ moment().add(10,'minutes').minute() +' '+ moment().hour() +' '+ moment().day() +' '+ moment().month() +' *';
var task = cron.schedule(cronString, function () {
console.log("cron job started")
}, false);
task.start();
})
But beware of the fact that this would be executed every year at the same time ;)
I'm running CronJob Every 30 mins.
But it is not running at the time of start.
How can i make it run at the time of start as well as every 30 min?
var CronJob = require('cron').CronJob;
console.log('started ' + new Date());
var job = new CronJob('0 */30 * * * *', function() {
myJob()
}, function () {
},
true,
'Indian/Mauritius'
);
job.start();
function myJob()
{
console.log('in Job');
console.log(new Date());
}
Output
started Mon Sep 21 2015 18:44:29 GMT+0530 (IST)
You could run myJob() and then start your cronjob.
or ...
you could change the way you use cron so that it runs at particular times, say now and every half hour like this:
var job = new CronJob('* 16,46 * * * *', // etc
Say the time is now 15:45, the above command will run in a minute (i.e. on the 16th and 46th minute of every hour). With a bit more code, you could generate the string "16,46" to be one minute from now and 30 minutes after that.