Skip to content
Open
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
26 changes: 22 additions & 4 deletions lib/forever-monitor/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down
16 changes: 16 additions & 0 deletions test/monitor/simple-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);