Skip to content

Commit

Permalink
disable autorestart on clean exit
Browse files Browse the repository at this point in the history
  • Loading branch information
ojhaujjwal committed May 5, 2018
1 parent 771ea22 commit 9ae718b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
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.neglectCleanExit = options.neglectCleanExit || 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.neglectCleanExit && code === 0)) {
letChildDie();
}
else if (spinning) {
Expand Down
57 changes: 57 additions & 0 deletions test/monitor/neglect-clean-exit-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* neglect-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/neglect-clean-exit').addBatch({
"When using forever-monitor": {
"and spawning a script that exits gracefully": {
"with neglectCleanExit enabled": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'graceful-exit-automatically.js'),
child = new (fmonitor.Monitor)(script, { neglectCleanExit: true });

child.on('exit', this.callback);
child.start();
},
"should not restart script": function (child, spinning) {
assert.isFalse(child.running);
},
},
"with a neglectCleanExit disabled": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'graceful-exit-automatically.js'),
child = new (fmonitor.Monitor)(script, { neglectCleanExit: 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 neglectCleanExit enabled": {
topic: function () {
var script = path.join(__dirname, '..', '..', 'examples', 'always-throw.js'),
child = new (fmonitor.Monitor)(script, { neglectCleanExit: true, silent: true });

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

0 comments on commit 9ae718b

Please sign in to comment.