<分区>
我在单个 Heroku web dyno 上有一个简单的 node.js 应用程序,并且 1 个多月没有改变。最近,我发现该应用程序没有按照 Heroku 文档(https://devcenter.heroku.com/articles/dynos#the-dyno-manager)从空闲状态返回。
相反,我的日志显示没有 Heroku 尝试在接收到 HTTP 请求时取消空闲并且应用程序返回 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;