node-cron stop in the night - node.js

I created a cron job with node cron, for now the job is really simple:
module.exports.start = async ( args ) => {
console.log("start cron");
// every 5 minutes
CronJob.schedule('*/5 * * * *', () => {
chatLog.send("A text log sent with telegram bot");
});
}
Now for the third day I've noticed that the cron works like a charm all day long but suddenly stop during the night a 1 o'clock.
In the morning when I start working the cron starts again.
My app is based on express framework and hosted on a plesk server with phusion passenger extension.
Now I suspect that in the night the server stop working, and that a restart happen in the morning at the first api call. Is it possible? Or do you think that I'm missing something...
Tnx

Related

Setinterval running in nodejs auto stops when hosting in Cpanel, It works perfect in localhost

I was running a setInterval in Nodejs at every 1 second interval. But when I hosted it on Cpanel after 30 minutes website inactivity It stops running. But in locahost it works without any issue. I am passing a socket in every second with this setinterval. Why the issue happen?
let intervalId = setInterval(() => {
var io = req.app.get('socketio');
let time = req.body.time++;
console.log(time)
let timerId = intervalId[Symbol.toPrimitive]();
io.to(req.user.email).emit("socketTimer", { intervalId: timerId, time});
}, 1000);
If you are running your NodeJS app through the CloudLinux NodeJS Selector, it might be an issue with the Apache Passenger, which is handling this. You might want to try running your app through SSH as you do on localhost and see if it happens again. If you can't seem to find a solution for that, then I would suggest switching to a NodeJS specific web hosting provider.

How to automatically send a ``get request`` in a loop after some interval of time?

I am making a web application that is scrapping the news from a news site and saves to my database (scrapping is done just for learning purpose). After the database is updated all the stored data is sent to the user frontend.
Here is the route responsible for the above action.
router.get('/news, postController.getNewsPost);
New news are added to the site being scrapped as the day passes. Lets say if no user logs in to my application my database does not updates because the route mentioned above does not fires.
I want my database to be updated periodically even when no users have logged into my web application.
I am new to backend development so please guide me on how i can achieve this, also let me know if more information is required.
The simplest solution would be to use a setInterval to execute a specific function every N seconds:
setInterval(function() {
// scrap news and save them to the database every 5 seconds
}, 5000)
More versatile and solid solutions can be implemented using an external library for scheduling task, something like Agenda
You can use node-cron module to schedule your scrapping job.
Install node-cron using npm:
$ npm install --save node-cron
Import node-cron and schedule your scrapping job:
var cron = require('node-cron');
cron.schedule('* * * * *', () => {
console.log('Scrapping....');
});
Here is the module link. https://www.npmjs.com/package/node-cron
You can use the following method
$ npm install --save node-cron
var cron = require('node-cron');
cron.schedule('second minute hour day month', () => {
//update database code
});
library link enter link description here

Do I need to keep my script running for node-cron to do scheduled tasks?

I am building a web scraper and I am new to it. What it does is that it scrapes the amazon prices for the URL provided by the user which then sends an email whenever the price goes below the price set by the user. I am using SendGrid to send the emails.
However, I had a question. I wanted to set the script to run automatically every hour or so and send an email if the price is found to be less. After researching online, I found that node-cron can help me do that and it did. However, it only works when my script is running in the background.
So my main question is that to run the script and send the emails, does my script need to actually run the entire time? If not what can I do?
This is my cron.js file. I also have a AmazonTracker.js which has code for tracking the price and sending the email.
const cron = require('node-cron');
let shell = require('shelljs');
const url = process.argv[2]
// minimum price for which user wants an alert
const minPrice = process.argv[3]
const emailID = process.argv[4];
// scheduled to run every second
cron.schedule("* * * * * *", function() {
console.log("running");
if(shell.exec(`node AmazonTracker.js ${url} ${minPrice} ${emailID}`).code !== 0){
console.log("Something went wrong");
}
})
Also, I get this error when trying to run the code for some links. My script works for some links and throws this error for some others. If anyone has any suggestions.
(node:4284) UnhandledPromiseRejectionWarning: Error: .wait() for #priceblock_dealprice timed out after 30000msec
at newDone (C:\Users\dtdan\node_modules\nightmare\lib\actions.js:545:9)
at Timeout._onTimeout (C:\Users\dtdan\node_modules\nightmare\lib\actions.js:578:5)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
Yes, the logic behind Crons is, that some daemon process will keep looking at the time and trigger some task. since you are using node-cron you need to keep this piece of code running it will trigger your AmazonTracker.js,
ideally one should go with systems Cron, which is more reliable and prone to failure due to system shutdown etc.
check below (this is one of the examples)
https://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/

How do i build a node js program/service that stays alive in the background and performs a polling action every few seconds?

I'm looking for a way to build a node.js service that runs in the background and polls a Redis Stream (it could be anything really). While i understand how to build a web server in node.js, this "background service that stays alive and polls something (say invokes a REST endpoint or polls a msg queue) every few seconds" is something i have not been able to find. If you can show it in a few lines of code, that'll be awesome
Start an interval that to stuff every x seconds
startInterval() {
const x = 1;
setInterval(() => {
// Do your Stuff every x seconds here
}, 1000 * x)
},
You can achieve this with PM2.
It is an production process manager for deploying and daemonizing Node.js applications.
In combination with #BraveButter answer you should be able to do what you want.
You may be overthinking this, since Node server processes by default stay alive and actively respond to requests as events. This is in contrast to some server runtime architectures (such as PHP) that will by default have a process per request and so start
the script 'from scratch' for every new request.
So, in case you are being confused by PHP or somesuch, where you have to jump through extra hoops to create a persistent process, then bear in mind that you shouldn't have to do that for a simple Node server script. You can just set an interval that will run a function every so often using setInterval
You can use CronJS.
Basic Cron Usage from the doc :
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'America/Los_Angeles');

node-cron does not work properly?

I deployed an app on Heroku: myapp
and I wrote some cron code in it:
var time = require('time');
var CronJob = require('cron').CronJob;
var job = new CronJob({
cronTime: '00 45 12 * * *',
onTick: clearReserve,
start: true,
timeZone: 'Asia/Shanghai'
});
My purpose is call the function named 'clearReserve' everyday in the specific time.
but it only work in the first day I upload my code to heroku, and never do this cron job again.
PS: this "clearReserve" function will manipulate my database, I use MongoLab URI which I created in MongoLab, not the Heroku add-on;
Your cron job is never ran again because Heroku dyno went to sleep. https://devcenter.heroku.com/articles/dynos#dyno-sleeping
In order to have your cron jobs executed you need to keep Heroku app awake 24/7. To make sure your Heroku app does not go to sleep, install the NewRelic Heroku Addon you can set up availability monitoring. You provide a URL which NewRelic pings every 30 seconds, therefore keeping your app awake. The intended purpose of this feature is to alert you if your site goes down for business and performance reasons, but it has the added benefit of preventing your app from idling.
Hope it helps!

Resources