Node.js: Get time till next cron job - node.js

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

Related

How to run a function after a given time in node.JS?

I am building a hosting server with node.JS and mongoDB.
The server will be used for people to save their files remotely. But I do not want to keep unattended files on the server because it will be a waste of memory on the server.
So there are two things I wish to do :
Delete Files if not attended for more than 10 days
Leave the Files otherwise
Is there a way this can be done easily?
You can use cron job for this type of scheduled task. NodeJS has a module named cron to perform a scheduled task.
Approach:
Every day at midnight you will check for unattended files. If you find any unattended file then you can delete it.
Example code:
const CronJob = require('cron').CronJob;
new CronJob('0 0 * * *', async () => {
// Find files
// Delete files
}, null, true, 'America/Vancouver', null, false);
You can schedule a cron and it will check for your condition and do the desired action.
https://www.npmjs.com/package/cron
example:
var CronJob = require('cron').CronJob;
var job = new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');
job.start();

Avoiding more than one cronjob in parallel in node.js

I would like to do some cronjob using the Node.js package "cron" every 10 minutes.
This cronjob takes between 5 to 15 minutes, and I don't want that in a case that one instance is still running - another will be joining it in parallel. Instead, it will skip the additional running and wait until the next period.
Is it possible to implement it using this package?
Here is the code of the implementation using the cron package :
const CronJob = require("cron").CronJob;
const job = new CronJob(
'0 */10 * * * *',
()=>SomeCronJob(),
null,
true,
'America/Los_Angeles',
);
I thought of implementing it using a combination of simple setInterval() and clearInterval() instead of the package, not sure how though.
I'd appreciate any help!
I would use a flag to check if the job is running. Example:
let isJobRunning = false;
function SomeCronJob() {
if (isJobRunning) {
// Skip
return;
}
isJobRunning = true;
// run stuff
// Once is finished
isJobRunning = false;
}

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?

can cron schedule a variable change in one of your scripts (node.js)

is it possible to get cron to go into one of your scripts on the server and change a variable at a certain time?
cron simply fires a program/script at a given time. You would need an intermediate script/program to change the variable. But you could schedule a script to run that changes your variable.
You can install cron npm module to do it:
$ yarn add cron
And then in your code:
const CronJob = require('cron').CronJob;
let i = 0;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
i++;
console.log(`Value of i = ${i}`);
}, null, true, 'America/Los_Angeles');

looking for a node.js scheduler that wont start if the job is still running

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

Resources