naught icon indicating copy to clipboard operation
naught copied to clipboard

is there an example of using domains with naught

Open thedug opened this issue 10 years ago • 13 comments

Is there an example of using domains to capture an uncaught exception and gracefully shutdown?

thedug avatar Feb 19 '15 06:02 thedug

This is what I use in production for various servers.

https://gist.github.com/mathrawka/f7b31c73e5fb95c2720d

emostar avatar Feb 20 '15 00:02 emostar

Cool. Thanks.. I added a few questions.

thedug avatar Feb 20 '15 16:02 thedug

I just answered your questions. (Oddly I don't notifications about gist comments).

emostar avatar Feb 20 '15 21:02 emostar

Cool. Thanks!

I'm still trying to decide between naught and recluster. I'm not thrilled about the way naught writes it's own logs.

thedug avatar Feb 20 '15 21:02 thedug

I use a 3rd party service, so the logs on the server don't ever get touched unless I need to find some real old data. But I have no qualms with how naught logs stdout and stderr. You can also do your own logging to files yourself and ignore stdout and stderr.

emostar avatar Feb 21 '15 02:02 emostar

You have a point there. Are you using loggly or similar? What are you using on the node side to generate the logs? Seems like there are several good alternatives, winston, morgan, etc.

Also, is there a benefit to using domains vs. just doing a process.on('uncaughtExeption')?

thedug avatar Feb 21 '15 03:02 thedug

I have used loggly and papertrail in the past. Here is a snippet of more production code:

    logger = new (winston.Logger)({
      transports: [
        new (winston.transports.Mail)({
            to       : 'xxx'
          , from     : 'xxxx'
          , host     : 'smtp.mandrillapp.com'
          , port     : 587
          , tls      : true
          , username : process.env.MANDRILL_USERNAME
          , password : process.env.MANDRILL_APIKEY
          , level    : 'error'
          }),
        new (winston.transports.Console)({colorize: true, timestamp: true}),
        new (winston.transports.Papertrail)({
            host: 'logs.papertrailapp.com'
          , port: process.env.PAPERTRAIL_PORT
          , inlineMeta: true
        })
      ]
    })

    app.use(reqLogger.create(logger))

The reqLogger is from a different package of mine, express-request-logger. I expose the winston logger object to my routes, and use it like this:

      logger.warn('Invalid receipt', {body: body, email: req.user.email})

emostar avatar Feb 21 '15 06:02 emostar

Thanks!

Are you just sharing that same logger instance with all the routes? Wasn't thinking it might be cleaner to make a module with the code above and have it export the logger, but not sure if instating that mean winston.Loggers is a good idea.

thedug avatar Feb 22 '15 06:02 thedug

Yeah, I just share it be doing a 'app.set("logger", logger)' when I create it.

emostar avatar Feb 23 '15 06:02 emostar

From everything I've read everyone seems to agree that if an exception bubbles up to the process you should exit as quickly as possible and restart.

The one piece that is controversial is the use of domains. The other camp suggest using process.on('uncaughtExeption').

Do you have any thoughts on this debate?

thedug avatar Feb 23 '15 06:02 thedug

I think the official Node.js documentation spells it out...

Note that uncaughtException is a very crude mechanism for exception handling. Don't use it, use domains instead. If you do use it, restart your application after every unhandled exception!

Domains allows you to get context on the error as well, for websites that means you can have the exact http request object that was running when the crash occured. So you can get the url and params, which are extremely useful when trying to reproduce an error.

emostar avatar Feb 25 '15 07:02 emostar

There are many people who say domains are a failed experiment and should be avoided. I know some folks over a paypal that say that at scale domains were causing performance penalty.

thedug avatar Mar 05 '15 03:03 thedug

For my use cases (99% mobile traffic, peak ~10 req/s), I am happy with the performance I am getting. The biggest bottleneck for me are are slow and laggy 3G connections. When you talk about a global scale, you'll definitely want to evaluate what is causing performance issues... but until you get to that point, it doesn't really matter a whole lot.

emostar avatar Mar 05 '15 05:03 emostar