Skip to content

Commit

Permalink
[api test doc dist] Version bump. Merged from donnerjack. Added abili…
Browse files Browse the repository at this point in the history
…ty to log to file(s). Updated docs.
  • Loading branch information
indexzero committed Sep 28, 2010
1 parent d5d2f1d commit a4f1700
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Expand Up @@ -27,6 +27,8 @@ You can use forever to run any kind of script continuously (whether it is writte
-m MAX Only run the specified script MAX times
-s, --silent Run the child script silencing stdout and stderr
-h, --help You're staring at it
-o, OUTFILE Logs stdout from child script to OUTFILE
-e, ERRFILE Logs stderr from child script to ERRFILE
</pre>

There are several samples designed to test the fault tolerance of forever. Here's a simple example:
Expand All @@ -51,6 +53,20 @@ You can also use forever from inside your own node.js code.
child.run();
</pre>

### Options available when using Forever in node.js
There are several options that you should be aware of when using forever:

<pre>
{
'max': 10, // Sets the maximum number of times a given script should run
'forever': true, // Indicates that this script should run forever
'silent': true, // Silences the output from stdout and stderr in the parent process
'outfile': 'path/to/file', // Path to log output from child stdout
'errfile': 'path/to/file', // Path to log output from child stderr
}
</pre>

### Events available when using Forever in node.js
Each forever object is an instance of the node.js core EventEmitter. There are several core events that you can listen for:

* restart [err, forever]: Raised each time the target script is restarted
Expand All @@ -63,4 +79,4 @@ Each forever object is an instance of the node.js core EventEmitter. There are s
vows test/*-test.js --spec
</pre>

#### Author: [Charlie Robbins](http://www.charlierobbins.com);
#### Author: [Charlie Robbins](http://www.charlierobbins.com)
8 changes: 6 additions & 2 deletions bin/forever
Expand Up @@ -14,13 +14,17 @@ var help = [
"options:",
" -m MAX Only run the specified script MAX times",
" -s, --silent Run the child script silencing stdout and stderr",
" -h, --help You're staring at it"
" -h, --help You're staring at it",
" -o, OUTFILE Logs stdout from child script to OUTFILE",
" -e, ERRFILE Logs stderr from child script to ERRFILE"
].join('\n');

var mappings = {
'm': 'max',
's': 'silent',
'silent': 'silent'
'silent': 'silent',
'o': 'outfile',
'e': 'errfile'
};

// Show help prompt if requested
Expand Down
39 changes: 38 additions & 1 deletion lib/forever.js
Expand Up @@ -7,17 +7,32 @@
*/

var sys = require('sys'),
fs = require('fs'),
eyes = require('eyes'),
path = require('path'),
events = require('events'),
spawn = require('child_process').spawn;

var Forever = function (file, options) {
events.EventEmitter.call(this);

this.times = 0;
options.options.unshift(file);
options.silent = options.silent || false;
options.forever = options.forever || false;
options.stdout = typeof options.outfile !== 'undefined';
options.stderr = typeof options.errfile !== 'undefined';

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

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

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

Expand All @@ -28,18 +43,30 @@ Forever.prototype.run = function () {
this.child = child;

child.stdout.on('data', function (data) {
// If we haven't been silenced, write to the process stdout stream
if (!self.options.silent) {
process.stdout.write(data);
}

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

self.emit('stdout', null, data);
});

child.stderr.on('data', function (data) {
// If we haven't been silenced, write to the process stdout stream
if (!self.options.silent) {
process.stdout.write(data);
}

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

self.emit('stderr', null, data);
});

Expand All @@ -52,6 +79,16 @@ Forever.prototype.run = function () {
});
}
else {
// If had to write to an stdout file, close it
if (self.options.stdout) {
self.stdout.end();
}

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

self.emit('exit', null, self);
}
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "forever",
"description": "A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)",
"version": "0.1.0",
"version": "0.2.0",
"author": "Charlie Robbins <charlie.robbins@gmail.com>",
"repository": {
"type": "git",
Expand Down
2 changes: 2 additions & 0 deletions test/forever-test.js
Expand Up @@ -43,6 +43,8 @@ vows.describe('forever').addBatch({
var child = new (forever.Forever)(path.join(__dirname, '..', 'samples', 'error-on-timer.js'), {
max: 3,
silent: true,
outfile: 'test/stdout.log',
errfile: 'test/stderr.log',
options: []
});

Expand Down

0 comments on commit a4f1700

Please sign in to comment.