I have some scheduled tasks in my node app. These are scheduled using node-cron. I'd like run job in parallel because I noticed when a cron job is not finished yet that others don't run.
Here an example
let cron = require('node-cron');
cron.schedule('*/2 * * * * *', ()=>
{
console.log("Every minute", new Date());
});
cron.schedule('*/2 * * * * *', ()=>
{
let a = 0;
for(i=0; i< 10000000000; i++)
a = a + 1;
console.log("Hello..... from scheduler");
});
How to solve this problem to run job in parallel ?
Thanks
Related
I have a server that has a cron job running at certain times in the day. I want to get the time till the next job will execute. I looked at the cron and node-cron packages in npm, but they don't seem to have this functionality.
How can I implement this functionality by myself for these packages?
The cron package allows you to get the next runs of the cron job, so you can use this to determine the time until the next run.
This is exposed in the CronJob.nextDates() function.
const CronJob = require("cron").CronJob;
const cronJob = new CronJob(
"*/30 * * * * *",
() => {
console.log("Timestamp: ", new Date());
},
null,
true,
"UTC"
);
setInterval(() => {
let timeUntilNextRunSeconds = cronJob.nextDates(1)[0].unix() - new Date().getTime()/1000;
console.log("Time until next run (s): ", Math.round(timeUntilNextRunSeconds));
}, 1000);
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 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.
The problem I am facing is that the project has already programmed with cluster to distribute task.
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
});
} else {
var server = http.createServer(app);
var usernames = {};
var showusernames = {};
var usersmessages = [];
require('../server/controllers/communication/chat.js').chatConfig(io, usernames);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
}
I have a basic idea that this code is for distributing task among cpu and keeping server live in case one cpu fails.
The question is:
Everything was working fine till I need started working with node to schedule the cron job (which will be send email).
The cron now is ran by all workers simultaneously and the email is send to worker depending on how many cpu are there on server.
The I worker my way out by scheduling job as:
if(cluster.isWorker)
if(cluster.worker.id == 1){
cron.schedule('*/1 * * * *', function() {
//CRON JOB
})
}
This worked very fine within local system but failed in staging server maybe because of CPU aligned to this very project.
Is there any way to get only the first free worker and assign task to
him.
Now i tried this
var wokerArr = []
wokerArr.push(cluster.worker.id)
if(cluster.worker.id == wokerArr[0])
cron.schedule('*/1 * * * *', function() {
//CRON JOB
})
I did that using the crontab.
Making a separate cron file and schedule the job using crontab in command prompt to schedule job. Thanks for support.
You can schedule the cron in master process itself. Cron needs to be handled in an idempotent way.
if (cluster.isMaster) {
cron.schedule('*/1 * * * *', function() {
//CRON JOB
})
// continue initializing workers here
}
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