Heroku上的Node.js应用程序不会从空闲状态唤醒

时间:2014-06-08 17:14:28

标签: node.js heroku

我在一个Heroku网络dyno上有一个简单的node.js应用程序,并且保持1个月不变。最近,我发现应用程序不会从空闲状态返回,因为它应该按照Heroku文档(https://devcenter.heroku.com/articles/dynos#the-dyno-manager)返回。

相反,我的日志显示没有Heroku在收到http请求时尝试unidle并且应用程序返回503错误:

2014-06-08T08:07:22.736817+00:00 heroku[web.1]: State changed from up to down
2014-06-08T08:07:22.736356+00:00 heroku[web.1]: Idling
2014-06-08T08:07:27.844277+00:00 heroku[web.1]: Process exited with status 143
2014-06-08T08:07:26.009475+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2014-06-08T16:37:36.565924+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/ host=brisksoft.herokuapp.com request_id=5e7040c6-fe3b-44b1-bf99-fa0214df9e5c fwd="174.21.203.228" dyno= connect= service= status=503 bytes=
2014-06-08T16:37:36.773503+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path=/favicon.ico host=brisksoft.herokuapp.com request_id=92627039-51f6-4a99-bafa-2c6f3a96d5ea fwd="174.21.203.228" dyno= connect= service= status=503 bytes=

我知道通过New Relic和Pingdom来防止睡眠的自动变通方法,但我更愿意实际解决这个问题,并让Heroku dyno表现得像记录一样。

我的package.json是:

{
  "name": "agent-node",
  "version": "0.0.1",
  "dependencies": {
    "elementtree": "^0.1.6",
    "request": "^2.27.0",
  },
  "scripts": {
    "start": "node index.js"
  },
  "description": "Node.js application for Job Agent",
  "main": "index.js",
  "devDependencies": {},
  "repository": {
    "type": "git",
    "url": "https://github.com/brenbob/nodejs"
  },
  "author": "Brenden West",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/brenbob/nodejs/issues"
  },
  "homepage": "https://github.com/brenbob/nodejs"
}

Index.js是:

// index.js
var server = require("./server");

server.start();

和server.js:

var http = require("http");
var url = require('url'); 
var api = require("./api");

function start() {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    switch(pathname) {
    case '/getjobs':
        api.getjobs(request, response);

    break;
    default:
        response.writeHead(200, {"Content-Type": "text/plain"});
        response.write("Hello World");
        response.end();
    }
  }

  http.createServer(onRequest).listen(process.env.PORT || 8888);
  console.log("Server has started.");
}

exports.start = start;

2 个答案:

答案 0 :(得分:0)

这似乎与怠速无关。当您的应用从空闲状态返回时,它会崩溃。它是在本地运行吗?

答案 1 :(得分:0)

有时问题就会消失。我推了一个新的版本,增加了“永远”,现在我的dyno乖乖地unidles。