简体   繁体   English

Node.js:服务器崩溃时如何警告

[英]Node.js: How To Get Warned When Server Crashes

How can I get warned when my server crashes for any reason? 当我的服务器因任何原因崩溃时如何收到警告? Does HopToad or any other service does that? HopToad或任何其他服务是否可以做到这一点?

Thanks 谢谢

EDIT: 编辑:

I am using Heroku for node.js. 我正在使用Heroku作为node.js. Which doesn't allow me to run anything else. 哪个不允许我运行其他任何东西。 I need something to monit my code from inside out, without needing to run any other process. 我需要从内到外监视我的代码,而不需要运行任何其他进程。

Thanks 谢谢

You can try to handle uncaughtException event within your node.js program and do your "warn me" stuff there. 您可以尝试在node.js程序中处理uncaughtException事件,并在那里“警告我”。 Or you can use service like uptime robot to monitor if your app is listening on specified port for example. 或者,您可以使用正常运行时机器人等服务来监控您的应用是否正在侦听指定端口。

Quite a few options actually! 实际上有很多选择!

monit , forever and supervisord are the ones that stick out of the crowd in my opinion. 在我看来, monit永远supervisord是那些在人群中脱颖而出的人。

Since @rob-cowie already answered about supervisor, here you get a few pointer about 'forever' and 'monit'. 既然@ rob-cowie已经回答过主管,那么你会得到一些关于'forever'和'monit'的指针。

Monit is an overall solution to manage services on your machine - notifying you and restarting them when they crash or take up too many resources. Monit是管理机器上服务的整体解决方案 - 在崩溃或占用太多资源时通知您并重新启动它们。

I have yet to find a good reason to user supervisord instead of monit, but on supervisord mailing list I saw suggestions to use monit to monitor supervisord! 我还没有找到一个很好的理由使用supervisord而不是monit,但是在supervisord邮件列表中,我看到了使用monit来监控supervisord的建议!

I've just put together a class which listens for unhandled exceptions, and when it see's one it: 我刚刚组建了一个监听未处理异常的类,当它看到它的时候:

  • prints the stack trace to the console 将堆栈跟踪打印到控制台
  • logs it in it's own logfile 将其记录在自己的日志文件中
  • emails you the stack trace 通过电子邮件向您发送堆栈跟踪
  • restarts the server (or kills it, up to you) 重新启动服务器(或杀死它,由你决定)

It will require a little tweaking for your application as I haven't made it generic as yet, but it's only a few lines and it might be what you're looking for! 它需要对你的应用程序进行一些调整,因为我还没有把它变成通用的,但它只有几行,它可能就是你想要的!

It runs from inside your app, no need to include any other files (except nodemailer.js if you want the mail notifications..) 它从你的应用程序内部运行,不需要包含任何其他文件(如果你想要邮件通知,则nodemailer.js除外)

Check it out! 看看这个!

One approach is to periodically check that you can successfully access a page you are serving, see http://wasitup.com/ . 一种方法是定期检查您是否可以成功访问您正在服务的页面,请参阅http://wasitup.com/

Another approach would be to monitor the server process on your machine. 另一种方法是监视计算机上的服务器进程。 One good way to do this is to launch node.js using Supervisord . 一个好方法是使用Supervisord启动node.js。 It can restart a crashed process and email you. 它可以重新启动崩溃的进程并通过电子邮件发送给您。 There are numerous examples around the intarwebs, including deployment example1 intarwebs周围有很多例子,包括部署示例1

If you're still using node.js on heroku, it's also worth looking at Nodejitsu. 如果您仍然在heroku上使用node.js,那么也值得一看Nodejitsu。 It has great support for node and runs forever, with email notifications if your app crash loops and other things. 它对节点有很大的支持并且可以永久运行,如果您的应用程序崩溃循环和其他事情,则会收到电子邮 Very handy. 非常便利。

Also, as far as handling uncaughtException goes I'd avoid that, from experiences it's unstable and it's also noted in the latest node.js api docs that it's unreliable and unstable. 另外,就处理uncaughtException而言,我会避免这种情况,从经验来看它是不稳定的,并且在最新的node.js api文档中也注意到它不可靠和不稳定。 See http://nodejs.org/api/process.html#process_event_uncaughtexception 请参阅http://nodejs.org/api/process.html#process_event_uncaughtexception

Use something like forever ! 永远的东西!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM