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?
Thanks
EDIT开发者_StackOverflow:
I am using Heroku for 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. 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.
Since @rob-cowie already answered about supervisor, here you get a few pointer about 'forever' and '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.
- Full exemple of Forever with nodejs
- nodejs with updstart and monit (from somebody who was using forever before)
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!
One approach is to periodically check that you can successfully access a page you are serving, see 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. It can restart a crashed process and email you. There are numerous examples around the intarwebs, including deployment example1
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..)
Check it out!
If you're still using node.js on heroku, it's also worth looking at 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. See http://nodejs.org/api/process.html#process_event_uncaughtexception
Use something like forever!
精彩评论