respawn

Spawn a process and restart it if it crashes

  • 所有者: mafintosh/respawn
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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'], {
  name: 'test',          // set monitor name
  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
  fork: true             // fork instead of spawn
})

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

主要指标

概览
名称与所有者mafintosh/respawn
主编程语言JavaScript
编程语言JavaScript (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2013-12-10 21:31:22
推送于2019-01-14 13:06:24
最后一次提交2019-01-14 14:06:15
发布数28
最新版本名称v2.6.0 (发布于 2019-01-14 14:06:15)
第一版名称v0.1.0 (发布于 2013-12-10 22:30:56)
用户参与
星数253
关注者数9
派生数34
提交数90
已启用问题?
问题数11
打开的问题数5
拉请求数11
打开的拉请求数1
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?