diff --git a/lib/forever.js b/lib/forever.js index ee7fe69c..46bf0dcb 100644 --- a/lib/forever.js +++ b/lib/forever.js @@ -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 @@ -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; } @@ -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); } @@ -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'); @@ -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(); } @@ -440,10 +441,9 @@ 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) { @@ -451,7 +451,7 @@ Forever.prototype.trySpawn = function () { } } - return spawn(this.options.command, this.options.options); + return spawn(this.command, this.options); }; // @@ -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) { @@ -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; diff --git a/test/forever-test.js b/test/forever-test.js index 0b1d8d84..d8693de4 100644 --- a/test/forever-test.js +++ b/test/forever-test.js @@ -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);