Skip to content

Commit a4f1700

Browse files
committed
[api test doc dist] Version bump. Merged from donnerjack. Added ability to log to file(s). Updated docs.
1 parent d5d2f1d commit a4f1700

File tree

5 files changed

+64
-5
lines changed

5 files changed

+64
-5
lines changed

README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ You can use forever to run any kind of script continuously (whether it is writte
2727
-m MAX Only run the specified script MAX times
2828
-s, --silent Run the child script silencing stdout and stderr
2929
-h, --help You're staring at it
30+
-o, OUTFILE Logs stdout from child script to OUTFILE
31+
-e, ERRFILE Logs stderr from child script to ERRFILE
3032
</pre>
3133

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

56+
### Options available when using Forever in node.js
57+
There are several options that you should be aware of when using forever:
58+
59+
<pre>
60+
{
61+
'max': 10, // Sets the maximum number of times a given script should run
62+
'forever': true, // Indicates that this script should run forever
63+
'silent': true, // Silences the output from stdout and stderr in the parent process
64+
'outfile': 'path/to/file', // Path to log output from child stdout
65+
'errfile': 'path/to/file', // Path to log output from child stderr
66+
}
67+
</pre>
68+
69+
### Events available when using Forever in node.js
5470
Each forever object is an instance of the node.js core EventEmitter. There are several core events that you can listen for:
5571

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

66-
#### Author: [Charlie Robbins](http://www.charlierobbins.com);
82+
#### Author: [Charlie Robbins](http://www.charlierobbins.com)

bin/forever

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@ var help = [
1414
"options:",
1515
" -m MAX Only run the specified script MAX times",
1616
" -s, --silent Run the child script silencing stdout and stderr",
17-
" -h, --help You're staring at it"
17+
" -h, --help You're staring at it",
18+
" -o, OUTFILE Logs stdout from child script to OUTFILE",
19+
" -e, ERRFILE Logs stderr from child script to ERRFILE"
1820
].join('\n');
1921

2022
var mappings = {
2123
'm': 'max',
2224
's': 'silent',
23-
'silent': 'silent'
25+
'silent': 'silent',
26+
'o': 'outfile',
27+
'e': 'errfile'
2428
};
2529

2630
// Show help prompt if requested

lib/forever.js

+38-1
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@
77
*/
88

99
var sys = require('sys'),
10+
fs = require('fs'),
11+
eyes = require('eyes'),
1012
path = require('path'),
1113
events = require('events'),
1214
spawn = require('child_process').spawn;
1315

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

17-
this.times = 0;
1819
options.options.unshift(file);
1920
options.silent = options.silent || false;
2021
options.forever = options.forever || false;
22+
options.stdout = typeof options.outfile !== 'undefined';
23+
options.stderr = typeof options.errfile !== 'undefined';
24+
25+
// If we should log stdout, open a file buffer
26+
if (options.stdout) {
27+
this.stdout = fs.createWriteStream(options.outfile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
28+
}
29+
30+
// If we should log stderr, open a file buffer
31+
if (options.stderr) {
32+
this.stderr = fs.createWriteStream(options.errfile, { flags: 'a+', encoding: 'utf8', mode: 0666 });
33+
}
34+
35+
this.times = 0;
2136
this.options = options;
2237
};
2338

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

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

51+
// If we have been given an output file for stdout, write to it
52+
if (self.options.stdout) {
53+
self.stdout.write(data);
54+
}
55+
3556
self.emit('stdout', null, data);
3657
});
3758

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

65+
// If we have been given an output file for stderr, write to it
66+
if (self.options.stderr) {
67+
self.stderr.write(data);
68+
}
69+
4370
self.emit('stderr', null, data);
4471
});
4572

@@ -52,6 +79,16 @@ Forever.prototype.run = function () {
5279
});
5380
}
5481
else {
82+
// If had to write to an stdout file, close it
83+
if (self.options.stdout) {
84+
self.stdout.end();
85+
}
86+
87+
// If had to write to an stderr file, close it
88+
if (self.options.stderr) {
89+
self.stderr.end();
90+
}
91+
5592
self.emit('exit', null, self);
5693
}
5794
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "forever",
33
"description": "A simple CLI tool for ensuring that a given node script runs continuously (i.e. forever)",
4-
"version": "0.1.0",
4+
"version": "0.2.0",
55
"author": "Charlie Robbins <[email protected]>",
66
"repository": {
77
"type": "git",

test/forever-test.js

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ vows.describe('forever').addBatch({
4343
var child = new (forever.Forever)(path.join(__dirname, '..', 'samples', 'error-on-timer.js'), {
4444
max: 3,
4545
silent: true,
46+
outfile: 'test/stdout.log',
47+
errfile: 'test/stderr.log',
4648
options: []
4749
});
4850

0 commit comments

Comments
 (0)