cron job in node js running multiple times - node.js

I am running a cron job using the module node-cron for node js . My code is below.
var Job = new CronJob({
cronTime: '* * 01 * * *', //Execute at 1 am every day
onTick : function() {
co(function*() {
yield insertToDatabase(); //this is the function that does insert operation
}).catch(ex => {
console.log('error')
});
},
start : false,
timeZone: 'Asia/Kolkata'
});
I need to execute this only one time but this cronjob once starts runs multiple times due to which same data gets inserted to my database. I only need to run this job only one time. What should i do.

I know I am late to the party but I think that I have a solution. I hope this can help someone else in the future (Hi future!)
I encountered what I think to be the same issue as the asker, that is, the OnTick function executes multiple times after the scheduled time, this appeared to the author to be an endless loop. The asker expected this function to run only once at the scheduled time everyday (today, tomorrow, etc).
With that in mind the cause of this "issue" is simple. The cron job is scheduled to to exactly this. The cron time entered is * * 01 * * * which means the hour is 01 (aka 1:00am) but the minutes and seconds are *. This means any minute and any second where the hour is 01. So this will run from 1:00am up until 1:59am. It will run for each second of each minute until 2:00am. If the author had waited until 2:00am the "looping" would have ceased.
The solution is to set the seconds and minutes to anything that is not * (0 in my case worked).
I feel really silly for not having figured this out right away but here it is!

You can call Job.stop() from onTick:
onTick : function() {
Job.stop();
co(function*() {
yield insertToDatabase();
}).catch(ex => {
console.log('error');
});
}

In my case, i changed my code from this :
var job = new CronJob('*/59 * * * *', onTick, onComplete, true, 'Asia/Kolkata'); // onTick and onComplete is a function, which i'm not showing here
job.start();
To this :
var job = new CronJob('*/59 * * * *', onTick, onComplete, false, 'Asia/Kolkata'); // onTick and onComplete is a function, which i'm not showing here
job.start();
Thanks

Related

How to run cron job 4 times in a day using nodejs?

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?

How to schedule process in the way that it should run immediately for the first time and run with scheduled time from second time in node js

I tried with node-schedule package
[https://www.npmjs.com/package/node-schedule][1]
var schedule = require('node-schedule');
var j = schedule.scheduleJob('42 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
But if I schedule for 10 minutes and start the process it is processing after 10 minutes.
In my case, I need the process to be run for the first time and later it should run with scheduled time.
Is there any solution for this kind of issue?
Thanks in Advance..
There is a way to do it , just found it
let startTime = new Date(Date.now()) ;
var j = schedule.scheduleJob({ start: startTime, rule:'42 * * * *'},
function(){
console.log('The answer to life, the universe, and everything!');
});
Hope this helps
See this documentation of node-schedule
You can use codes from https://github.com/kelektiv/node-cron.
Then you can fire the crontab job at the second when you start nodejs program. After the crontab job will be run at certain interval.
This is an example to run the job immediately when creating the crontab work.
const CronJob = require('cron').CronJob;
const job = new CronJob({
cronTime: '0 */5 * * * *',
onTick: () => console.log(`Round at ${new Date()}`);
runOnInit: true
});
job.start();
In codes above, the "console.log" will be run when I start my program. Then it will run at every 5 minutes. For more usage, you can refer to the lib provided.

cron job is not working node-cron

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 ;)

How to setup node-schedule for every day at 12am

I am using node-schedule to schedule my tasks. Now I need to schedule a job everyday at 12am.
Below given is the code I am using,
var rule3 = schedule.scheduleJob('00 00 00 * * *', function(){
console.log('my test job!');
});
This is not working for me.
Any help appreciated. Thanks in advance.
You can use node-cron module simply.
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 0-6', function() {
/*
* Runs every day
* at 12:00:00 AM.
*/
}, function () {
/* This function is executed when the job stops */
},
true, /* Start the job right now */
timeZone /* Time zone of this job. */
);
Read docs for more pattern.
For anyone who is stuck on this also check what time your app is operating in. Your app by default might be in Greenwich Mean Time which would definitely make you think your schedule is not working. Toss a console.log(Date.now()) in a convenient location and check. You might just have to adjust the time on your schedule by a couple hours.

node JS: How to run a task for every 30 minutes , including now

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.

Resources