Skip to content

Commit

Permalink
[api] Push options hierarchy up one level. e.g. Forever.options.silen…
Browse files Browse the repository at this point in the history
…t is now Forever.silent
  • Loading branch information
indexzero committed Dec 23, 2010
1 parent 124cc25 commit 070313e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 37 deletions.
66 changes: 33 additions & 33 deletions lib/forever.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,33 +323,35 @@ function getAllPids (processes) {
var Forever = function (file, options) {
events.EventEmitter.call(this);

options.silent = options.silent || false;
options.forever = options.forever || false;
options.command = options.command || 'node';
options.options = options.options || [];
this.silent = options.silent || false;
this.forever = options.forever || false;
this.command = options.command || 'node';
this.options = options.options || [];
this.max = options.max;
this.outFile = options.outFile;
this.errFile = options.errFile;

this.childExists = false;

if (Array.isArray(file)) {
options.command = file[0];
options.options = file.slice(1);
this.command = file[0];
this.options = file.slice(1);
}
else {
options.options.unshift(file);
this.options.unshift(file);
}

// If we should log stdout, open a file buffer
if (options.outFile) {
this.stdout = fs.createWriteStream(options.outFile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
if (this.outFile) {
this.stdout = fs.createWriteStream(this.outFile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
}

// If we should log stderr, open a file buffer
if (options.errFile) {
this.stderr = fs.createWriteStream(options.errFile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
if (this.errFile) {
this.stderr = fs.createWriteStream(this.errFile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
}

this.times = 0;
this.options = options;
};

// Inherit from events.EventEmitter
Expand All @@ -371,7 +373,7 @@ Forever.prototype.start = function (restart) {
var child = this.trySpawn();
if (!child) {
process.nextTick(function () {
self.emit('error', new Error('Target script does not exist: ' + self.options.options[0]));
self.emit('error', new Error('Target script does not exist: ' + self.options[0]));
});
return this;
}
Expand All @@ -382,14 +384,13 @@ Forever.prototype.start = function (restart) {
// Hook all stream data and process it
function listenTo (stream) {
child[stream].on('data', function (data) {
// If we haven't been silenced, and we don't have a file stream
// to output to write to the process stdout stream
if (!self.options.silent && !self.options[stream]) {
if (!self.silent && !self[stream]) {
// If we haven't been silenced, and we don't have a file stream
// to output to write to the process stdout stream
process.stdout.write(data);
}

// If we have been given an output file for the stream, write to it
if (self.options[stream]) {
else if (self[stream]) {
// If we have been given an output file for the stream, write to it
self[stream].write(data);
}

Expand All @@ -405,7 +406,7 @@ Forever.prototype.start = function (restart) {
self.log('Forever detected script exited with code: ' + code);
self.times++;

if (self.options.forever || self.times < self.options.max) {
if (self.forever || self.times < self.max) {
self.emit('restart', null, self);
process.nextTick(function () {
self.log('Forever restarting script for ' + self.times + ' time');
Expand All @@ -416,12 +417,12 @@ Forever.prototype.start = function (restart) {
this.running = false;

// If had to write to an stdout file, close it
if (self.options.stdout) {
if (self.stdout) {
self.stdout.end();
}

// If had to write to an stderr file, close it
if (self.options.stderr) {
if (self.stderr) {
self.stderr.end();
}

Expand All @@ -440,18 +441,17 @@ Forever.prototype.start = function (restart) {
// trying to execute a script with an env: e.g. node myfile.js
//
Forever.prototype.trySpawn = function () {
if (this.options.command === 'node' || (this.options.checkFile
&& !this.childExists)) {
if (this.command === 'node' || (this.checkFile && !this.childExists)) {
try {
var stats = fs.statSync(this.options.options[0]);
var stats = fs.statSync(this.options[0]);
this.childExists = true;
}
catch (ex) {
return false;
}
}

return spawn(this.options.command, this.options.options);
return spawn(this.command, this.options);
};

//
Expand All @@ -469,15 +469,15 @@ Forever.prototype.save = function () {
var childData = {
pid: this.child.pid,
foreverPid: process.pid,
logFile: this.options.logFile,
options: this.options.options.slice(1),
file: this.options.options[0]
logFile: this.logFile,
options: this.options.slice(1),
file: this.options[0]
};

this.childData = childData;
if (this.options.pidFile) {
childData.pidFile = this.options.pidFile;
}
if (this.pidFile) childData.pidFile = this.pidFile;
if (this.outFile) childData.outFile = this.outFile;
if (this.errFile) childData.errFile = this.errFile;

var childPath = path.join(config.pidPath, childData.foreverPid + '.fvr');
fs.writeFile(childPath, JSON.stringify(childData), function (err) {
Expand Down Expand Up @@ -512,7 +512,7 @@ Forever.prototype.save = function () {
// Utility function for logging forever actions
//
Forever.prototype.log = function (message) {
if (!this.options.silent) {
if (!this.silent) {
sys.puts(message);
}
return this;
Expand Down
7 changes: 3 additions & 4 deletions test/forever-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ vows.describe('forever').addBatch({
child.emit('test', null, child);
},
"should have correct properties set": function (err, child) {
assert.isNotNull(child.options);
assert.equal(child.options.max, 10);
assert.isTrue(child.options.silent);
assert.isArray(child.options.options);
assert.isArray(child.options);
assert.equal(child.max, 10);
assert.isTrue(child.silent);
assert.isFunction(child.start);
assert.isFunction(child.save);
assert.isFunction(child.stop);
Expand Down

0 comments on commit 070313e

Please sign in to comment.