Nested cron job stop and start issue - node.js

I want to start two jobs simultaneously, but after some time i will stop one of them and will continue second one.
In our case i am starting 2 jobs every 30 seconds and each jobs itself execute every 4 second, If i stop first job in the middle of execution, it will stop and second job is performing its task but the problem is that after 30 second first job also start performing task even though we have stop that job.
Here Is my Code:
var job1;
var job = new CronJob({
cronTime: '*/30 * * * * *',
onTick: function () {
var interval = 4000;
var index = 0;
var index1 = 0;
var users = ['U1', 'U2', 'U3', 'U4']
var users1 = ['D1', 'D2', 'D3', 'D4']
job1 = new CronJob({
cronTime: '*/4 * * * * *',
onTick: function () {
var user = users[index];
console.log("Sent To :" + user);
index++;
if (users.length == index) {
job1.stop();
}
},
start: false,
timeZone: 'GMT'
});
job1.start();
var job2 = new CronJob({
cronTime: '*/4 * * * * *',
onTick: function () {
var user = users1[index1];
console.log("Sent To :" + user);
index1++;
if (users1.length == index1) {
job2.stop();
}
},
start: false,
timeZone: 'GMT'
});
job2.start();
},
start: false,
timeZone: 'GMT'
});
job.start();
For stop job:
job1.stop();

Your first job starts is just because you have set the cron to run both the jobs at every 30 sec which is what I can see from your code. This will keep on running your whole bunch of stuff to run every 30 sec.

Related

How to send email after a one week in nodejs

I created a script: when a user signups our app, then I will send a welcome email, then I want after one another will be send.
Note: Now cron job runs every minute, I will change it later.
var job = new CronJob('* * * * *', function() {
console.log('Cron Job is happen on',Date());
emailSend()
}, null, true, '');
function emailSend(){
SingUpDate = something
comapredate = today-7
if(comparedate == signupdate){
sendEmail()
}
else{
return false
}
}
I want to know about how to stop cron after a week.
You could have your emailSend function remove its own cron job:
var job = new CronJob('* * * * *', function () {
console.log('Cron Job will happen on', Date())
emailSend()
}, null, true, '')
// you will need to start the job:
job.start()
function emailSend () {
SingUpDate = something
comapredate = today-7
if (comparedate == signupdate) {
sendEmail()
// and then stop it here:
job.stop()
} else {
return false
}
}
Or you can schedule it for the specific date that you want:
const date = new Date();
date.setDate(date.getDate() + 7)
const job = new CronJob(date, () => {
console.log(`Scheduled job for ${date} running at ${new Date()}`)
emailSend()
}, null, true, '')
job.start()

Nodejs - How to set cron job to run on every 2 Sunday

Below cron job runs every Sunday 14:35:00, but I want to run the run job every 2 Sunday 14:35:00.
Is it possible to do that?
var CronJob = require('cron').CronJob;
new CronJob('0 35 14 * * 0', async function () {
}, null, true, 'America/Los_Angeles');
I didn't see any pattern for your requirement, but you can this
var CronJob = require('cron').CronJob;
var x = 1;
new CronJob('0 35 14 * * 0', async function () {
if (x / 2 != 1) {
x++;
//do somthing
} else {
x = 1;
}
}, null, true, 'America/Los_Angeles');

NodeJs: Setting up cron server every 2 minutes but stop it in between 12am to 6 am?

I need some help in setting up a cron server in which the process will run every two minutes, but stop at 12am and restart running every two minutes at 6 am.
I have already set up it to run every two minutes. Any help please ?
new cronJob({
cronTime: '0 */2 * * * *',
onTick: function() {
//process run after every two minutes
},
start: true
});
You can check the current hour inside the onTick:
onTick : function() {
// Don't do anything if between the hours of 12AM and 6AM.
if (new Date().getHours() < 6) return;
// The job code follows:
},
How about 2 other cronJobs that sets a flag on/off at 12 & 6?
var enabled = true;
new cronJob({
cronTime: '0 */2 * * * *',
onTick: function() {
if (enabled) {
//process run after every two minutes
}
},
start: true
});
new cronJob({
cronTime: '0 0 12 * * *',
onTick: function() {
enabled = false;
},
start: true
});
new cronJob({
cronTime: '0 0 6 * * *',
onTick: function() {
enabled = true;
},
start: true
});

Mongodb colelction find in cron job throws new Error('Can\'t wait without a fiber');

I have set up a cron job using the npm package cron. I am trying to do the following Coll.find().forEach function but I am getting the error Error: Can't wait without a fiber
var job = new CronJob({
cronTime: '00 09 11 * * 1-5',
onTick: function() {
var userIds = []
Coll.find().forEach(function(doc) {
userIds.push(doc._id)
});
},
start: false,
timeZone: "Europe/London"
});
job.start();
I have been using npm packages fibers and future library. I still got the same error.
var resultOne = collFind();
function collFind() {
var f = new future()
var userIds = []
Coll.find().forEach(function(doc) {
userIds.push(doc.userId)
});
return f['return']({userIds:userIds}
return f.wait()
}
Try using Meteor.bindEnvironment.
var job = new CronJob({
cronTime: '00 09 11 * * 1-5',
onTick: Meteor.bindEnvironment(function() {
var userIds = []
Coll.find().forEach(function(doc) {
userIds.push(doc._id)
});
}),
start: false,
timeZone: "Europe/London"
});
job.start();
It will ensure that the callback get's run in the current fiber, and ensure all global variables are accessible.

node-schedule is not work at time

i want to use node-schedule , i get information from Data Base every day , and for each item i want to do some thing at special time.
this is my code :
users.forEach(function(users_entry){
if(err){
console.log(err);
}
else{
var date = new Date(2014, 11, 29, 11, 45, 0);
schedule.scheduleJob(date,
function(){
console.log('The world is going to end today.');
});
}
});
but above code doesn't run at mentioned time and works all the time.
what is problem?
i changed my code and used cron.
https://www.npmjs.org/package/cron
it works very well :)
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function(){
console.log('You will see this message every second');
}, null, true, "America/Los_Angeles");
try this
var CronJob = require('cron').CronJob;
var job = new CronJob('00 00 12 * * 1-7', 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. */
);
Try this:
const schedule = require('node-schedule');
const moment = require('moment');
const rule = scheduled.RecurrenceRule();
let time = '2021-07-30 14:20:00';
rule.tz = 'Asia/Kolkata';
rule.year = moment(time).year();
rule.month = moment(time).month();
rule.date = moment(time).date();
rule.hour = moment(time).hours();
rule.minute = moment(time).minutes();
rule.second = moment(time).seconds();
schedule.scheduleJob(rule, async function () {
console.log("Scheduled")
});

Resources