Skip to content

Commit

Permalink
add .fork option (mafintosh#18)
Browse files Browse the repository at this point in the history
* add .fork option

* add .silent

* forward ipc messages
  • Loading branch information
juliangruber authored and mafintosh committed Feb 16, 2017
1 parent 2638ffb commit 0211437
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
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()
})

0 comments on commit 0211437

Please sign in to comment.