diff --git a/lib/forever-monitor/monitor.js b/lib/forever-monitor/monitor.js index 02c75e6..835c1aa 100755 --- a/lib/forever-monitor/monitor.js +++ b/lib/forever-monitor/monitor.js @@ -184,15 +184,16 @@ Monitor.prototype.start = function (restart) { // Re-emit messages from the child process this.child.on('message', onMessage); - child.on('exit', function (code, signal) { + function handleExit () { var spinning = Date.now() - self.ctime < self.minUptime; child.removeListener('message', onMessage); - self.emit('exit:code', code, signal); function letChildDie() { self.running = false; self.forceStop = false; - self.emit('exit', self, spinning); + process.nextTick(function () { + self.emit('exit', self, spinning); + }); } function restartChild() { @@ -205,7 +206,7 @@ Monitor.prototype.start = function (restart) { self.times++; if (self.forceStop || (self.times >= self.max && !self.forceRestart) - || (spinning && typeof self.spinSleepTime !== 'number') && !self.forceRestart) { + || (spinning && typeof self.spinSleepTime !== 'number' && !self.forceRestart)) { letChildDie(); } else if (spinning) { @@ -214,6 +215,22 @@ Monitor.prototype.start = function (restart) { else { restartChild(); } + } + + child.on('exit', function (code, signal) { + process.nextTick(function() { + self.emit('exit:code', code, signal); + }); + + handleExit(); + }); + + child.on('error', function (err) { + process.nextTick(function () { + self.emit('error', err); + }); + + handleExit(); }); return this; @@ -339,6 +356,7 @@ Monitor.prototype.restart = function () { // Stops the target script associated with this instance. Prevents it from auto-respawning // Monitor.prototype.stop = function () { + this.forceRestart = false; return this.kill(true); }; diff --git a/test/monitor/simple-test.js b/test/monitor/simple-test.js index 5c751c5..92cf9b8 100644 --- a/test/monitor/simple-test.js +++ b/test/monitor/simple-test.js @@ -120,5 +120,21 @@ vows.describe('forever-monitor/monitor/simple').addBatch({ assert.equal('' + buf, 'sample-script.js'); } } + }, +"attempting to start a command where the path and file do not exist": { + topic: function () { + var child = fmonitor.start('', { + max: 1, + silent: true, + sourceDir: '/blah/not/here', + cwd: '/blah/not/here', + command: 'npm start' + }); + child.on('error', this.callback.bind({}, null)); + }, + "should throw an error about the invalid file": function (err) { + assert.isNotNull(err); + assert.isTrue(err.message.indexOf('ENOENT') !== -1); + } } }).export(module);