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

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

问题描述

因此,我需要在heroku上运行我的node.js应用程序,它工作得很好,但是当我的应用程序崩溃时,我需要重新启动它,所以我永远添加到package.json,并创建了一个名为forever .js与此:

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

在我的Proc文件上(heroku用来知道开始的内容):

  web:node forever.js 

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


与其他所有需要您纠正的操作错误不同,此操​​作不需要您执行任何操作。


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



任何人都可以帮我吗?也许这是一个永远的问题?一个Heroku问题?

解决方案

这是免费的Heroku帐户的问题:Heroku会在1小时不活动后自动杀死未付费的应用程序,然后在下一次请求进入时让它们备份。(如下所述,这不适用于付费帐户,如果您扩展到两台服务器并为第二台服务器付费,则会有两台始终在线的服务器。) - https://devcenter.heroku.com/articles/dynos#dyno-sleeping



此行为可能与 forever 不能很好地搭配。要确认这一点,运行 heroku logs 并查找怠速和用SIGTERM停止进程行,然后看看接下来会发生什么。



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



结果是您的应用程序现在更加稳定,再加上它可以使用所有可用的CPU核心(根据我的经验,4个)。 b
$ b

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



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



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

更新2:他们再次改变它。现在,如果您验证了自己的ID,则可以不断运行1个免费的测试版: https: //blog.heroku.com/announcing_heroku_free_ssl_beta_and_flexible_dyno_hours#flexible-free-dyno-hours


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

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

web: node forever.js

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.

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

解决方案

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

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.

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.

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

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

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

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

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

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

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