Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploy nodeJS App without http server on Heroku

i'm currently using heroku (www.heroku.com) to host my web app : http://jeveuxskier.ski which is working pretty well as an http app. Now i'd like to deploy a non-http nodeJS App to heroku. Basically, it's just a cron that calls a webservice everyday à 1:30 and stores the result into my firebase database.

However, i can't manage to deploy that on heroku as it seems to want a http server launched to receive requests etc..

I've tried to put my script into a http server and it looks like the cron service is well initiated when i call my heroku container url but then, after few minutes, the container gets into snooze state. If anyone has an idea of how deploying that kind of script to heroku (free) account :

var rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [0, new schedule.Range(0, 6)]; // every day 
rule.hour = 1; // 1h to 1h30 am 
rule.minute = 30;

var j = schedule.scheduleJob(rule, function(){
    console.log("cron woke up at:" + new Date());  
});

Thanks in advance,

Best regards,

Quentin BERNET

like image 462
Quentin Bernet Avatar asked Jan 27 '17 08:01

Quentin Bernet


1 Answers

If you're using free dynos, your web app will ALWAYS sleep after 30 minutes. There is no way around this. If you want to have non-sleeping dynos, you need to use the paid ones.

With that said, you can indeed use a free worker dyno. This dyno will run constantly, and doesn't require a web server to operate.

Here's how you can do it (this is a full, working example application I just deployed and tested myself):

Procfile

worker: node worker.js

worker.js

console.log("Worker starting...");

setInterval(() => {
  console.log("Worker still running.");
}, 1000);

package.json

{
  "name": "test",
  "version": "1.0.0",
  "main": "worker.js",
  "license": "ISC"
}

If you push this app to Heroku (git push heroku master), then run the following commands, you will be running a free worker dyno:

$ heroku ps:scale web=0 worker=1

If you then view the Heroku streaming logs, you'll see that your worker process is indeed running, as you can verify by the log output:

$ heroku logs --tail
2017-01-27T17:28:01.476780+00:00 heroku[worker.1]: Starting process with command `node worker.js`
2017-01-27T17:28:02.206558+00:00 heroku[worker.1]: State changed from starting to up
2017-01-27T17:28:03.810579+00:00 app[worker.1]: running worker...
2017-01-27T17:28:04.819944+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:05.821946+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:06.822191+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:07.826204+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:08.830177+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:09.831974+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:10.834909+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:11.834361+00:00 app[worker.1]: Worker still running.
2017-01-27T17:28:12.835244+00:00 app[worker.1]: Worker still running.
like image 175
rdegges Avatar answered Sep 20 '22 06:09

rdegges