How to run cron job 4 times in a day using nodejs? - node.js

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?

Related

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.

Nodejs once every eight hours

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. */
);

cron job in node js running multiple times

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

Recurring functions in Node JS

I try to develop a little browser game based on NodeJS and Angular 4.
I have an API server running on NodeJS which is connected to a MongoDB and a second server running Angular 4.
I want to execute recurring standard functions (like every 15 minutes) in the background.
Do I need a third server which runs that functions? Or can I run that functions independently on my API server - no matter which route is open?
You might want to have a look to this library node-cron. You can set it up to work with your services. You will need to initialise the job right after your sever is initialised. An example:
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: '00 30 11 * * 1-5',
onTick: function() {
/*
* Runs every weekday (Monday through Friday)
* at 11:30:00 AM. It does not run on Saturday
* or Sunday.
*/
},
start: false,
timeZone: 'America/Los_Angeles'
});
job.start();
You can use setTimeout() and setInterval() in Node just like in the browser:
setInterval(() => {
// this runs every 15 minutes
}, 15 * 60 * 1000);

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