在 Heroku 上永远使用 Node.JS [英] Node.JS with forever on Heroku

查看:18
本文介绍了在 Heroku 上永远使用 Node.JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我需要在 heroku 上运行我的 node.js 应用程序,它运行良好,但是当我的应用程序崩溃时,我需要一些东西来重新启动它,所以我将永远添加到 package.json,并创建了一个名为永远的文件.js 与此:

So, I need to run my node.js app on heroku, it works very well, but when my app crashes, i need something to restart it, so i added forever to package.json, and created a file named forever.js with this:

var forever = require('forever');

var child = new (forever.Monitor)('web.js', {
  max: 3,
  silent: false,
  options: []
});

//child.on('exit', this.callback);
child.start();

forever.startServer(child);

在我的 Procfile(heroku 用来知道从什么开始)我放:

on my Procfile (that heroku uses to know what to start) i put:

web: node forever.js

好的!现在每次我的应用程序崩溃时它都会自动重启,但是,不时(几乎每 1 小时),heroku 开始抛出 H99 - 平台错误,关于这个错误,他们说:

alright! Now everytime my app crashes it auto restarts, but, from time to time (almost every 1 hour), heroku starts throwing H99 - Platform error, and about this error, they say:

与需要您采取行动纠正的所有其他错误不同,这一错误不需要您采取行动.请稍后重试,或查看状态站点.

Unlike all of the other errors which will require action from you to correct, this one does not require action from you. Try again in a minute, or check the status site.

但我只是手动重新启动我的应用程序,错误就会消失,如果我不这样做,它可能需要几个小时才能自行消失.

But I just manually restart my app and the error goes away, if I don't do that, it may take hours to go away by itself.

有人可以帮我吗?也许这是一个永远的问题?一个heroku问题?

Can anyone help me here? Maybe this is a forever problem? A heroku issue?

推荐答案

这是免费 Heroku 帐户的一个问题:Heroku 会在闲置 1 小时后自动杀死未付费的应用程序,然后在下次请求到来时将它们重新启动. (如下所述,这不适用于付费帐户.如果您扩展到两台服务器并为第二台付费,您将获得两台永远在线的服务器.) - https://devcenter.heroku.com/articles/dynos#dyno-sleeping

This is an issue with free Heroku accounts: Heroku automatically kills unpaid apps after 1 hour of inactivity, and then spins them back up the next time a request comes in. (As mentioned below, this does not apply to paid accounts. If you scale up to two servers and pay for the second one, you get two always-on servers.) - https://devcenter.heroku.com/articles/dynos#dyno-sleeping

这种行为可能不适用于 forever.要确认这一点,请运行 heroku logs 并查找Idling"和Stopping process with SIGTERM"行,然后看看接下来会发生什么.

This behavior is probably not playing nicely with forever. To confirm this, run heroku logs and look for the lines "Idling" and " Stopping process with SIGTERM" and then see what comes next.

与其使用 forever,您可能想尝试使用 Cluster API 并在每次死亡时自动创建一个新子项.http://nodejs.org/api/cluster.html#cluster_cluster 就是一个很好的例子,您只需将代码放入 else 块中.

Instead of using forever, you might want to try the using the Cluster API and automatically create a new child each time one dies. http://nodejs.org/api/cluster.html#cluster_cluster is a good example, you'd just put your code into the else block.

结果是您的应用现在更加稳定,而且它可以使用所有可用的 CPU 内核(我的经验是 4 个).

The upshot is that your app is now much more stable, plus it gets to use all of the available CPU cores (4 in my experience).

缺点是你不能在内存中存储任何状态.如果您需要存储会话或类似内容,请试用免费的 Redis To Go 插件 (heroku addons:add redistogo).

The downside is that you cannot store any state in memory. If you need to store sessions or something along those lines, try out the free Redis To Go addon (heroku addons:add redistogo).

这是一个当前使用 cluster 和 Redis To Go 在 heroku 上运行的示例:https://github.com/nfriedly/node-unblocker

Here's an example that's currently running on heroku using cluster and Redis To Go: https://github.com/nfriedly/node-unblocker

更新:Heroku 最近对免费应用程序的工作方式进行了一些重大更改,最大的变化是它们每天最多只能在线 18 小时,使其无法用作真正的"网络服务器.详情见 https://blog.heroku.com/archives/2015/5/7/heroku-free-dynos

UPDATE: Heroku has recently made some major changes to how free apps work, and the big one is they can only be online for a maximum of 18 hours per day, making it effectively unusable as a "real" web server. Details at https://blog.heroku.com/archives/2015/5/7/heroku-free-dynos

更新 2:他们再次更改了它.现在,如果您验证您的 ID,您可以持续运行 1 个免费 dyno:https://blog.heroku.com/annoucing_heroku_free_ssl_beta_and_flexible_dyno_hours#flexible-free-dyno-hours

UPDATE 2: They changed it again. Now, if you verify your ID, you can run 1 free dyno constantly: https://blog.heroku.com/announcing_heroku_free_ssl_beta_and_flexible_dyno_hours#flexible-free-dyno-hours

这篇关于在 Heroku 上永远使用 Node.JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆