Reputation: 23
I am developing a Nodejs server which I am uploading to Heroku. The file has a Cron job which is meant to run every 4th hour. But when I check in nothing happens. I am using the free version of Heroku and I do not appear to be exceeding my limit of free Dyno hours.
The code in my Index.js file looks like below. As you can see in this simple example, I just want the application to log the time every 4th hour. I also update a variable which should indicate the latest update, which can be used to check for status of updates.
import express from 'express';
const app = express();
import cors from 'cors';
app.use(cors());
import cron from 'node-cron';
import dotenv from 'dotenv'
dotenv.config()
const port = process.env.PORT || 3050;
var lasttime = '';
var task = cron.schedule("0 0 */4 * * *",() => {
console.log("date: "+Date());
lasttime = Date();
}, {
scheduled: true
}
);
app.get('/api_x', async (request, response) => {
reply=lasttime;
response.json({reply:reply});
});
This works fine when I run it on my local computer. When I run it on Heroku it logs the message once, doesn't update the variable lasttime and then does nothing. When I check the logs for the App on Heroku it reads:
Unidling
State changed from down to starting
Starting process with command npm start
[email protected] start /app
node index.js
listening at 10006
State changed from starting to up
What could be causing my Cron job not to run? (I have also tried using setInterval, but still no results.)
Upvotes: 1
Views: 987
Reputation: 1377
In the free version of Heroku the server sleeps after 30 mins of inactivity, otherwise always on depending on your remaining monthly free dyno hours.
The Hobby plan Never sleeps
Upvotes: 2