Skip to content

Commit 9b56c41

Browse files
committed
[api] Allow for forced exit if scripts restart in less than minUptime
1 parent 650f874 commit 9b56c41

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

examples/always-throw.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('Dont spin restart')

lib/forever/monitor.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ var sys = require('sys'),
2222
//
2323
var Monitor = exports.Monitor = function (script, options) {
2424
events.EventEmitter.call(this);
25-
25+
26+
options = options || {};
2627
this.silent = options.silent || false;
2728
this.forever = options.forever || false;
2829
this.command = options.command || 'node';
2930
this.sourceDir = options.sourceDir;
31+
this.minUptime = options.minUptime || 2000;
3032
this.options = options.options || [];
3133
this.spawnWith = options.spawnWith || null;
3234
this.uid = options.uid || forever.randomString(24);
@@ -126,10 +128,11 @@ Monitor.prototype.start = function (restart) {
126128
listenTo('stderr');
127129

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

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

146-
self.emit('exit', self);
149+
self.emit('exit', self, spinning);
147150
}
148151
});
149152

test/spin-test.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* forever-test.js: Tests for forever module
3+
*
4+
* (C) 2010 and Charlie Robbins
5+
* MIT LICENCE
6+
*
7+
*/
8+
9+
require.paths.unshift(require('path').join(__dirname, '..', 'lib'));
10+
11+
var sys = require('sys'),
12+
assert = require('assert'),
13+
path = require('path'),
14+
vows = require('vows'),
15+
forever = require('forever');
16+
17+
vows.describe('forever').addBatch({
18+
"When using forever": {
19+
"and spawning a script that spin restarts": {
20+
topic: function () {
21+
var script = path.join(__dirname, '..', 'examples', 'always-throw.js'),
22+
child = new (forever.Forever)(script, { silent: true });
23+
24+
child.on('exit', this.callback.bind(null, null));
25+
child.start();
26+
},
27+
"should spawn both processes appropriately": function (err, monitor, spinning) {
28+
assert.isTrue(spinning);
29+
}
30+
}
31+
},
32+
}).export(module);

0 commit comments

Comments
 (0)