Job scheduling in node.js - node.js

I want to develop an app which needs to run a scheduled job which writes something to database for every 15 minutes from 9 am to 9 pm
How can i implement this?
Thanks

Look into this package: https://www.npmjs.com/package/node-cron
Here's an example from their docs:
var cron = require('node-cron');
cron.schedule('*/2 * * * *', () => {
console.log('running a task every two minutes');
});
To write to the database the details vary based on what database you're using exactly. For simplicity you can use Express to connect + write to a DB quickly.
Read more here: https://expressjs.com/en/guide/database-integration.html

You can do this.
const CronJob = require("cron").CronJob;
console.log("Before job instantiation");
const job = new CronJob("0 */15 9-21 * * *", function () {
const d = new Date();
console.log("Every 15 minutes between 9-21:", d);
});
console.log("After job instantiation");
job.start();

Related

How can I setinterval with Nodej.s for a specific time of day?

I have the following function that runs every hour but I would like it to run once per day at 3 AM:
setInterval(async () => {
await updateData();
}, 1000 * 60 * 30);
How can I achieve this?
setInterval doesn't have this functionality directly. You could play around with date math to make this happen, but honestly, the easiest approach would probably be to use a thrid-party that does this for you, like node-cron.
First, you'd need to install it:
npm install node-cron
Then, in your code:
cron = require('node-cron');
cron.schedule('0 3 * * *', async () => {
await updateData();
});

How can I execute multiple cron jobs at different timings using node-cron in nodejs?

How can I execute this with different timings? Please help on this. Thanks in Advance...
const Cron = require('node-cron');
const Cron2 = require('node-cron');
var TaskOne = Cron.schedule('*/10 * * * *', async() => {
//first job implemented here and its working fine
function1();
});
var TaskTwo = Cron2.schedule('*/11 * * * *', async() => {
// Second job implemented here....
//Why this block is not getting executed???????????????????????
function2();
});
How can I execute this with different timings? TaskTwo is not getting executed. Debugger not goes into TaskTwo. Please help on this. Thanks in Advance...
if node-cron is not the only prefrence for you then you can use https://github.com/agenda/agenda
There's no need to require the same package twice, so you should remove Cron2 on the second line. Thus the line var TaskTwo = Cron2.schedule('*/11 * * * *', async() => { should be changed to:
var TaskTwo = Cron.schedule('*/11 * * * *', async() => {
As for why the second schedule is not working, it might be because you didn't start() the schedule, as shown in https://github.com/node-cron/node-cron#start. So adding the code below at the end of your script might trigger both cron to run:
TaskOne.start();
TaskTwo.start();

How to run a function only at the end of the month in NodeJS

Hi I'm doing a website and the backend is nodeJS. I need to reset some information in the database every end of the month but I don't know how can I do a function that will be executed the last day of the month at 23:59. Any idea about how can I do that?
You can use node-cron for that:
var cron = require('node-cron');
cron.schedule('0 0 1 * *', () => {
console.log('running on the 1st day of the month at 00:00');
});
You can change some information of database using moment function in cronjobs.
example:
const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
const endOfMonth = moment().endOf('month').format('YYYY-MM-DD hh:mm');

How to call function every given period on a route

I need to call a function every given period of time on a route for example my function:
function hello(){
console.log("Hi")
}
app.post("/", (req,res) => {
res.send("Hi")
hello()
}
Well, I didn't put all the code in my application, just the part that matters, I need to call the hello function every 5 seconds. I need to use this method of calling a given function every given period, in my application.
you can use node-schedule !
for example: Execute a cron job every 5 Minutes = */5 * * * *
var schedule = require('node-schedule');
var j = schedule.scheduleJob('*/5 * * * *', function(){
console.log('The answer to life, the universe, and everything!');
});
Another solution:
setInterval
setInterval(() => {
// do something every 5 seconds
}, 5000);

Schedule task for every 4 hours in Node.js

How could I schedule a task to run after 4 hours using "node-schedule" in Node.js
Currently my code is as below but it isn't responding as expected.
var schedule = require('node-schedule');
var task = schedule.scheduleJob('* */4 * * *', function () {
console.log('Scheduled Task');
});
Your syntax creates a cron that runs every minute every 4 hours.
The syntax you are looking for is 0 */4 * * *. Wich executes ONCE every 4 hours.
You can test the cron syntax with the website http://crontab.guru
Another option setting your cron in node is using rules. See https://github.com/node-schedule/node-schedule
var cron = require('node-schedule');
var rule = new cron.RecurrenceRule();
rule.hour = 4;
rule.minute = 0;
cron.scheduleJob(rule, function(){
console.log(new Date(), 'Every 4 hours');
});
try this
var cron = require('node-schedule');
var rule = new cron.RecurrenceRule();
rule.hour = new cron.Range(0,23,4);
rule.minute = 0;
cron.scheduleJob(rule, function(){
console.log(new Date(), 'Every 4 hours');
});
new cron.Range(0,23,4); 4 is the optional step parameter

Resources