Skip to content

Commit

Permalink
[api] Allow for forced exit if scripts restart in less than minUptime
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Apr 29, 2011
1 parent 650f874 commit 9b56c41
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions examples/always-throw.js
@@ -0,0 +1 @@
throw new Error('Dont spin restart')
9 changes: 6 additions & 3 deletions lib/forever/monitor.js
Expand Up @@ -22,11 +22,13 @@ var sys = require('sys'),
//
var Monitor = exports.Monitor = function (script, options) {
events.EventEmitter.call(this);


options = options || {};
this.silent = options.silent || false;
this.forever = options.forever || false;
this.command = options.command || 'node';
this.sourceDir = options.sourceDir;
this.minUptime = options.minUptime || 2000;
this.options = options.options || [];
this.spawnWith = options.spawnWith || null;
this.uid = options.uid || forever.randomString(24);
Expand Down Expand Up @@ -126,10 +128,11 @@ Monitor.prototype.start = function (restart) {
listenTo('stderr');

child.on('exit', function (code) {
var spinning = Date.now() - self.ctime < self.minUptime;
self.error('Forever detected script exited with code: ' + code);
self.times++;

if ((self.forever || self.times < self.max) && !self.forceStop) {
if ((self.forever || self.times < self.max) && !self.forceStop && !spinning) {
process.nextTick(function () {
self.warn('Forever restarting script for ' + self.times + ' time');
self.start(true);
Expand All @@ -143,7 +146,7 @@ Monitor.prototype.start = function (restart) {
// If had to write to an stderr file, close it
if (self.stderr) self.stderr.end();

self.emit('exit', self);
self.emit('exit', self, spinning);
}
});

Expand Down
32 changes: 32 additions & 0 deletions test/spin-test.js
@@ -0,0 +1,32 @@
/*
* forever-test.js: Tests for forever module
*
* (C) 2010 and Charlie Robbins
* MIT LICENCE
*
*/

require.paths.unshift(require('path').join(__dirname, '..', 'lib'));

var sys = require('sys'),
assert = require('assert'),
path = require('path'),
vows = require('vows'),
forever = require('forever');

vows.describe('forever').addBatch({
"When using forever": {
"and spawning a script that spin restarts": {
topic: function () {
var script = path.join(__dirname, '..', 'examples', 'always-throw.js'),
child = new (forever.Forever)(script, { silent: true });

child.on('exit', this.callback.bind(null, null));
child.start();
},
"should spawn both processes appropriately": function (err, monitor, spinning) {
assert.isTrue(spinning);
}
}
},
}).export(module);

0 comments on commit 9b56c41

Please sign in to comment.