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

respawn-cluster

v2.1.2

Published

Spawn a process and restart it if it crashes

Downloads

15

Readme

respawn

Spawn a process and restart it if it crashes.

npm install respawn

Build Status

Usage

It is easy to use

var respawn = require('respawn')

var monitor = respawn(['node', 'server.js'], {
  env: {ENV_VAR:'test'}, // set env vars
  cwd: '.',              // set cwd
  maxRestarts:10,        // how many restarts are allowed within 60s
                         // or -1 for infinite restarts
  sleep:1000,            // time to sleep between restarts,
  kill:30000,            // wait 30s before force killing after stopping
  stdio: [...]           // forward stdio options
})

monitor.start() // spawn and watch

Optionally you can specify the command to to spawn in the option map as command: [...]

Per default respawn will restart you app indefinitely. To set a max restart limit set the maxRestarts option.

If sleep is an array of numbers it will use the value at the position of the current number of restarts as the timeout value. If the number of restarts exceed the length of the array it will use the last value in the array until it hits the maxRestarts.

sleep: [1000, 60000, 60000, 12000, 1000] will wait 1000ms before retrying, then it will wait 60000 before the next retry and so forth.

If sleep is a function it will be passed the number of times (including this one) that the app has been restarted (i.e. first time will be called with 1, second time 2 etc.) and should return a time in milliseconds.

API

  • monitor.start() Starts the monitor

  • monitor.stop(cb) Stops the monitor (kills the process if its running with SIGTERM)

  • monitor.status Get the current monitor status. Available values are running, stopping, stopped, crashed and sleeping

Events

  • monitor.on('start') The monitor has started

  • monitor.on('stop') The monitor has fully stopped and the process is killed

  • monitor.on('crash') The monitor has crashed (too many restarts or spawn error).

  • monitor.on('sleep') monitor is sleeping

  • monitor.on('spawn', process) New child process has been spawned

  • monitor.on('exit', code, signal) child process has exited

  • monitor.on('stdout', data) child process stdout has emitted data

  • monitor.on('stderr', data) child process stderr has emitted data

  • monitor.on('warn', err) child process has emitted an error

Graceful restart

To do graceful restart simply have your app stop gracefully when receiving SIGTERM and do

// graceful restart (do not wait for old process to die)
monitor.stop()
monitor.start()

// hard restart (wait for old process to die)
monitor.stop(function() {
  monitor.start()
})

License

MIT