Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add .fork option #18

Merged
merged 3 commits into from
Feb 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ var monitor = respawn(['node', 'server.js'], {
// 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
stdio: [...], // forward stdio options
fork: true // fork instead of spawn
})

monitor.start() // spawn and watch
Expand Down
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var events = require('events')
var spawn = require('child_process').spawn
var fork = require('child_process').fork
var exec = require('child_process').exec
var ps = require('ps-tree')
var util = require('util')
Expand Down Expand Up @@ -51,7 +52,11 @@ var Monitor = function(command, opts) {
this.stdio = opts.stdio
this.stdout = opts.stdout
this.stderr = opts.stderr
this.silent = opts.silent
this.windowsVerbatimArguments = opts.windowsVerbatimArguments
this.spawnFn = opts.fork
? fork
: spawn

this.crashed = false
this.sleep = typeof opts.sleep === 'function' ? opts.sleep : defaultSleep(opts.sleep)
Expand Down Expand Up @@ -106,12 +111,13 @@ Monitor.prototype.start = function() {

var loop = function() {
var cmd = typeof self.command === 'function' ? self.command() : self.command
var child = spawn(cmd[0], cmd.slice(1), {
var child = self.spawnFn(cmd[0], cmd.slice(1), {
cwd: self.cwd,
env: xtend(process.env, self.env),
uid: self.uid,
gid: self.gid,
stdio: self.stdio,
silent: self.silent,
windowsVerbatimArguments: self.windowsVerbatimArguments
})

Expand Down Expand Up @@ -143,6 +149,10 @@ Monitor.prototype.start = function() {
}
}

child.on('message', function(message) {
self.emit('message', message)
})

var clear = function() {
if (self.child !== child) return false
self.child = null
Expand Down
1 change: 1 addition & 0 deletions test/apps/fork.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
process.send({ foo: 'bar' })
21 changes: 21 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var crash = path.join(__dirname, 'apps', 'crash.js')
var run = path.join(__dirname, 'apps', 'run.js')
var hello = path.join(__dirname, 'apps', 'hello-world.js')
var env = path.join(__dirname, 'apps', 'env.js')
var fork = path.join(__dirname, 'apps', 'fork.js')
var node = process.execPath

test('restart', function(t) {
Expand Down Expand Up @@ -290,3 +291,23 @@ test('restart using restart strategy function', function (t) {
})
mon.start()
})

test('fork', function(t) {
t.plan(1)

var mon = respawn([fork], {fork:true, maxRestarts:1, sleep:1})
var messages = []

mon.on('message', function(message) {
messages.push(message)
})

mon.on('stop', function() {
t.deepEqual(messages, [
{foo: 'bar'},
{foo: 'bar'}
])
})

mon.start()
})