diff --git a/examples/graceful-exit-automatically.js b/examples/graceful-exit-automatically.js new file mode 100644 index 0000000..8c3a9f7 --- /dev/null +++ b/examples/graceful-exit-automatically.js @@ -0,0 +1,3 @@ +setTimeout(function (){ + process.exit(0); +}, 1000); diff --git a/lib/forever-monitor/monitor.js b/lib/forever-monitor/monitor.js index 02c75e6..62fe88c 100755 --- a/lib/forever-monitor/monitor.js +++ b/lib/forever-monitor/monitor.js @@ -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 @@ -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) { diff --git a/test/monitor/ignore-clean-exit-test.js b/test/monitor/ignore-clean-exit-test.js new file mode 100644 index 0000000..5ef5b1d --- /dev/null +++ b/test/monitor/ignore-clean-exit-test.js @@ -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);