npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

exit-strategy

v0.2.1

Published

handle uncaught exceptions with node's domains

Downloads

3

Readme

Exit Strategy

Build Status

  • Generates error event handlers to gracefully kill your app
  • Optimised for use with node's domains
  • Uses visionmedia's logger to print POSIX error codes to STDOUT that will be picked up by logwatch

Full Example

var	app					= require('express').express(),
	domain				= require('domain'),
	appDomain			= domain.create(),
	exitStrategy		= require('exit-strategy'),
	exitAppDomain		= exitStrategy.app.exitAppDomain,
	exitServerDomain	= exitStrategy.server.exitServerDomain,
	exitApp				= exitAppDomain(app),
	server, exitServer;

// attach error handler
appDomain.on('error', exitApp);

// wrap app execution in domain to deal with uncaught exceptions
appDomain.run(function () {

	// 1. create http server
	server = server.listen(app.get('port'), function() {
		// you need to add this!
		app.set('serverListening', true);
	});

	// 2. replace exitApp with exitServer error handler once inited server
	exitServer = exitServerDomain(app, server);
	appDomain.on('error', exitServer).removeListener('error', exitApp);

	// 3. kill app for serious error evts you catch and broadcast with app.emit('seriousError')
	app.on('seriousError', exitServer);
	process.on('SIGTERM', exitServer);

	// 4. send 502s to new connections if started server shutdown process
	app.use(exitStrategy.middleware.closeGracefully(app));

	// define routes etc
});

Logwatch Setup

  • by default logwatch watches all files in /var/log and subdirs
  • make sure your app logs to a file in this dir
  • logwatch will email you alerts depending upon the --detail level you set it to

API

.app.exitServerDomain(app, httpServer) returns -> fn (err)

  • app needs get/set methods (see getter-setter module if you don't want to use express)
  • returns error handler function (fn (err))
var app       	  = require('express').express(),
	appDomain	  = domain.create(),
	exitAppDomain = require('exit-strategy').app.exitAppDomain(app);

var server = server.listen(app.get('port'), function() {
	// you need to add this!
	app.set('serverListening', true);
});
appDomain.on('error', exitServerDomain(app, server));
appDomain.run(function () {
	// run your app - set up routes etc...
});
what the server domain error handler does:
  1. checks if the server is active
  2. calls server.close()
  3. sets timer to kill app if server.close() takes more than 30s
  4. calls exitApp
  5. calls exitProcess

.app.exitAppDomain(app) returns -> fn (err)

  • app needs get/set methods (see getter-setter module if you don't want to use express)

  • returns error handler function (fn (err))

    var app       	  = require('express').express(),
    	appDomain	  = domain.create(),
    	exitAppDomain = require('exit-strategy').app.exitAppDomain(app);
    
    appDomain.on('error', exitApp);
    appDomain.run(function () {
    	// run your app
    });
what the app domain error handler does:
  1. if app.get('serverListening') then lets serverDomain handle it
  2. sets app flag
app.set('killingApp', true);
  1. calls exitProcess

.exitProcess(app, err)

what it does:
  1. logs error to STDOUT
  2. exits process

.middleware.closeGracefully(app)

  • Middleware to prevent new connections once the exit routine has been initiated.
  • If you are load balancing then see this guide for configuring nginx to fail over to the next upstream server
var app = require('express').express(),

// send 502s to new connections if started server shutdown process
app.use(exitStrategy.middleware.closeGracefully(app));