Reputation: 99
I am writing a NodeJS application for my server which will act as an API.
The server runs a function which retrieves an RSS feed and processes it into a MongoDB database every X amount of minutes.
The server also handles routes, for example: when the user requests xyz.com/api/data
, the server requests the RSS data from the MongoDB and sends it back to the user.
My question is about how to handle the function which checks the RSS feed every X amount of minutes. Currently I am using setInterval(rssCheck, X)
, but I am worried that if the rssCheck()
is running while a user requests xyz.com/api/data
, will the user be frozen until rssCheck()
has finished?
I understand that NodeJS is single threaded so is there a better way to do what I am trying to do?
Upvotes: 0
Views: 96
Reputation: 658
node-cron
can be used to set a cron job that call the rssCheck()
function every x amount of minutes.
const cron = require("node-cron");
cron.schedule(
"X * * * *",
function() {
log.info("Running a rss check function");
rssCheck()
}, {
timezone: "timezone",//replace with respective time-zone
scheduled: true,
}
);
To check cron schedule string meaning use this
Upvotes: 2