简体   繁体   English

在Heroku上部署没有http服务器的nod​​eJS App

[英]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. 我目前正在使用heroku(www.heroku.com)来托管我的网络应用程序: http//jeveuxskier.ski ,它作为一个http应用程序运行良好。 Now i'd like to deploy a non-http nodeJS App to heroku. 现在我想将一个非http nodeJS应用程序部署到heroku。 Basically, it's just a cron that calls a webservice everyday à 1:30 and stores the result into my firebase database. 基本上,它只是一个cron,每天1:30调用一次web服务,并将结果存储到我的firebase数据库中。

However, i can't manage to deploy that on heroku as it seems to want a http server launched to receive requests etc.. 但是,我无法设法在heroku上部署它,因为它似乎想要启动http服务器来接收请求等。

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. 我试图把我的脚本放到一个http服务器上,当我调用我的heroku容器URL时看起来cron服务很好,但几分钟后,容器就进入了贪睡状态。 If anyone has an idea of how deploying that kind of script to heroku (free) account : 如果有人知道如何将这种脚本部署到heroku(免费)帐户:

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 昆汀BERNET

If you're using free dynos, your web app will ALWAYS sleep after 30 minutes . 如果您使用的是免费动态,那么您的网络应用程序将在30分钟后始终保持睡眠状态 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. 话虽如此,你确实可以使用自由工作者dyno。 This dyno will run constantly, and doesn't require a web server to operate. 此dyno将持续运行,不需要Web服务器即可运行。

Here's how you can do it (this is a full, working example application I just deployed and tested myself): 以下是您可以这样做的方法(这是我刚刚部署并测试过的完整,有效的示例应用程序):

Procfile Procfile

worker: node worker.js

worker.js worker.js

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

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

package.json 的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( git push heroku master ),然后运行以下命令,你将运行一个免费的工作人员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流日志,您将看到您的工作进程确实正在运行,因为您可以通过日志输出进行验证:

$ 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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM