node-cron does not work properly? - node.js

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!

Related

node-cron stop in the night

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

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

Node.js "node-cron" not running on Google app engine

I have configured this cronjobe using node-cron in my node.js project
const cron = require('node-cron');
cron.schedule('0 0 * * *', () => {
console.log("CRON: Running");
// Do someting
});
and deployed project to Google APP engine, this cron normally runs on my local during testing at 24:00 no issue, but on Google APP engine it is not ...
Project itself normally deployed to server and running no issues (accessible through web) but cornjobe seems did not triggered at 24:00 by app engine server, i'm trying to understand why ?? Seems server was off at moment when no one is using it, or ??
This is because the App Engine instance is not necearily running at the time of the execution. (this happends more with Standard as it can scale down to 0)
Therefore a workaround for have Cronjobs in app engine is to have the schedule of this jobs managed by something else, in this case Google Cloud Scheduler which basically is a scheduler for tasks in app engine.
In This Guide You can see how to create cron job with the cloud Scheduler by the cloud console.

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

heroku node.js scheduler is not working

I am trying to schedule a task after 10 minutes in node.js heroku. I have created a file worker.js in program main directory. In worker.js I have just called the controller function that I want to schedule like this :
const reports = require('./app/controllers/reports');
reports.sendEmail();
sendEmail function use to send emails. in Heroku scheduler I have add worker.js as :
but my scheduler is not working. What I am missing in my configuration?
Edit your Heroku Scheduler dashboard, and type node worker.js for the command to be executed.

Resources