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
3 changes: 3 additions & 0 deletions examples/graceful-exit-automatically.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(function (){
process.exit(0);
}, 1000);
4 changes: 3 additions & 1 deletion lib/forever-monitor/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ var Monitor = exports.Monitor = function (script, options) {
this.append = options.append;
this.usePolling = options.usePolling;
this.pollingInterval = options.pollingInterval;
this.ignoreCleanExit = options.ignoreCleanExit || false;

//
// Define some safety checks for commands with spaces
Expand Down Expand Up @@ -205,7 +206,8 @@ 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
|| (self.ignoreCleanExit && code === 0)) {
letChildDie();
}
else if (spinning) {
Expand Down
57 changes: 57 additions & 0 deletions test/monitor/ignore-clean-exit-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* ignore-clean-exit-test.js: Tests for spin restarts in forever.
*
* (C) 2010 Charlie Robbins & the Contributors
* MIT LICENCE
*
*/

var assert = require('assert'),
path = require('path'),
vows = require('vows'),
fmonitor = require('../../lib');

vows.describe('forever-monitor/monitor/ignore-clean-exit').addBatch({
"When using forever-monitor": {
"and spawning a script that exits gracefully": {
"with ignoreCleanExit enabled": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'graceful-exit-automatically.js'),
child = new (fmonitor.Monitor)(script, { ignoreCleanExit: true });

child.on('exit', this.callback);
child.start();
},
"should not restart script": function (child, spinning) {
assert.isFalse(child.running);
},
},
"with a ignoreCleanExit disabled": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'graceful-exit-automatically.js'),
child = new (fmonitor.Monitor)(script, { ignoreCleanExit: false });

child.on('start', this.callback);
child.start();
},
"should restart script": function (child, data) {
assert.isTrue(child.running);
}
}
},
"and spawning a script that exits with uncaughtException": {
"with a ignoreCleanExit enabled": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'always-throw.js'),
child = new (fmonitor.Monitor)(script, { ignoreCleanExit: true, silent: true });

child.on('start', this.callback);
child.start();
},
"should restart script": function (child, data) {
assert.isTrue(child.running);
}
}
}
}
}).export(module);