From 52a9b36c8069e50bdd51eb59036086e39eee537a Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 Feb 2017 12:25:46 +0100 Subject: [PATCH 1/3] add .fork option --- README.md | 3 ++- index.js | 6 +++++- test/index.js | 23 +++++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 96856fb..a60e9aa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index e0b0b71..d8394ae 100644 --- a/index.js +++ b/index.js @@ -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') @@ -52,6 +53,9 @@ var Monitor = function(command, opts) { this.stdout = opts.stdout this.stderr = opts.stderr this.windowsVerbatimArguments = opts.windowsVerbatimArguments + this.spawnFn = opts.fork + ? fork + : spawn this.crashed = false this.sleep = typeof opts.sleep === 'function' ? opts.sleep : defaultSleep(opts.sleep) @@ -106,7 +110,7 @@ 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, diff --git a/test/index.js b/test/index.js index d18dc38..8be2924 100644 --- a/test/index.js +++ b/test/index.js @@ -290,3 +290,26 @@ test('restart using restart strategy function', function (t) { }) mon.start() }) + +test('fork', function(t) { + t.plan(5) + + var mon = respawn([crash], {maxRestarts:1, sleep:1, fork:true}) + + var spawned = 0 + var exited = 0 + + mon.on('spawn', function() { + t.ok(spawned++ < 2, 'less than 2 spawns') + }) + + mon.on('exit', function() { + t.ok(exited++ < 2, 'less than 2 exits') + }) + + mon.on('stop', function() { + t.ok(true, 'should stop') + }) + + mon.start() +}) From 24aaec50ef1df08fc0e53803474e60df1a56eebd Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 Feb 2017 12:40:44 +0100 Subject: [PATCH 2/3] add .silent --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index d8394ae..cf98aa5 100644 --- a/index.js +++ b/index.js @@ -52,6 +52,7 @@ 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 @@ -116,6 +117,7 @@ Monitor.prototype.start = function() { uid: self.uid, gid: self.gid, stdio: self.stdio, + silent: self.silent, windowsVerbatimArguments: self.windowsVerbatimArguments }) From d47656c64b120d94d0b299018d89f830125579ab Mon Sep 17 00:00:00 2001 From: Julian Gruber Date: Thu, 16 Feb 2017 13:03:22 +0100 Subject: [PATCH 3/3] forward ipc messages --- index.js | 4 ++++ test/apps/fork.js | 1 + test/index.js | 22 ++++++++++------------ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 test/apps/fork.js diff --git a/index.js b/index.js index cf98aa5..d73dbd4 100644 --- a/index.js +++ b/index.js @@ -149,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 diff --git a/test/apps/fork.js b/test/apps/fork.js new file mode 100644 index 0000000..7fadebb --- /dev/null +++ b/test/apps/fork.js @@ -0,0 +1 @@ +process.send({ foo: 'bar' }) diff --git a/test/index.js b/test/index.js index 8be2924..b83a011 100644 --- a/test/index.js +++ b/test/index.js @@ -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) { @@ -292,23 +293,20 @@ test('restart using restart strategy function', function (t) { }) test('fork', function(t) { - t.plan(5) - - var mon = respawn([crash], {maxRestarts:1, sleep:1, fork:true}) - - var spawned = 0 - var exited = 0 + t.plan(1) - mon.on('spawn', function() { - t.ok(spawned++ < 2, 'less than 2 spawns') - }) + var mon = respawn([fork], {fork:true, maxRestarts:1, sleep:1}) + var messages = [] - mon.on('exit', function() { - t.ok(exited++ < 2, 'less than 2 exits') + mon.on('message', function(message) { + messages.push(message) }) mon.on('stop', function() { - t.ok(true, 'should stop') + t.deepEqual(messages, [ + {foo: 'bar'}, + {foo: 'bar'} + ]) }) mon.start()