forked from foreversd/forever
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api test] Added core of forever and binary CLI runner
- Loading branch information
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#!/usr/bin/env node | ||
|
||
var path = require('path'), | ||
eyes = require('eyes'), | ||
sys = require('sys'), | ||
argv = require('optimist').argv; | ||
|
||
require.paths.unshift(path.join(__dirname, '..', 'lib')); | ||
|
||
var forever = require('forever'); | ||
|
||
var help = [ | ||
"usage: forever [FILE, ...] [options]", | ||
"", | ||
"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" | ||
].join('\n'); | ||
|
||
var mappings = { | ||
'm': 'max', | ||
's': 'silent', | ||
'silent': 'silent' | ||
}; | ||
|
||
// Show help prompt if requested | ||
if (argv.h || argv.help) { | ||
sys.puts(help); | ||
process.exit(0); | ||
} | ||
|
||
var options = {}; | ||
|
||
// Setup pass-thru options for child-process | ||
options.options = []; | ||
|
||
Object.keys(argv).forEach(function (key) { | ||
if (key !== '_') { | ||
if (typeof mappings[key] !== 'undefined') { | ||
options[mappings[key]] = argv[key]; | ||
} | ||
else { | ||
options.options.push('-' + key); | ||
options.options.push(argv[key]); | ||
} | ||
} | ||
}); | ||
|
||
// If max isn't specified set it to run forever | ||
if (typeof options['max'] === 'undefined') { | ||
options.forever = true; | ||
} | ||
|
||
// Run all of the files forever | ||
argv._.forEach(function (file) { | ||
options.times = 0; | ||
options.options.unshift(path.join(__dirname, file)); | ||
forever.run(options); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* forever.js: Top level include for the forever module | ||
* | ||
* (C) 2010 and Charlie Robbins | ||
* MIT LICENCE | ||
* | ||
*/ | ||
|
||
var sys = require('sys'), | ||
eyes = require('eyes'), | ||
spawn = require('child_process').spawn; | ||
|
||
var forever = exports; | ||
|
||
forever.run = function (options) { | ||
var child = spawn('node', options.options); | ||
child.stdout.on('data', function (data) { | ||
if (!options.silent) { | ||
process.stdout.write(data); | ||
} | ||
}); | ||
|
||
child.stderr.on('data', function (data) { | ||
if (!options.silent) { | ||
process.stdout.write(data); | ||
} | ||
}); | ||
|
||
child.on('exit', function (code) { | ||
options.times++; | ||
if (options.forever || options.times < options.max) { | ||
forever.run(options); | ||
} | ||
}); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* | ||
* count-timer.js: Counts forever on a timer | ||
* | ||
* (C) 2010 and Charlie Robbins | ||
* MIT LICENCE | ||
* | ||
*/ | ||
|
||
var sys = require('sys'); | ||
|
||
var count = 0; | ||
|
||
var id = setInterval(function () { | ||
sys.puts('Count is ' + count + '. Incrementing now.'); | ||
count++; | ||
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* error-on-timer.js: Sample script that errors on a timer | ||
* | ||
* (C) 2010 and Charlie Robbins | ||
* MIT LICENCE | ||
* | ||
*/ | ||
|
||
var sys = require('sys'); | ||
|
||
setTimeout(function () { | ||
sys.puts('Throwing error now.'); | ||
throw new Error('User generated fault.'); | ||
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* spawn-and-error.js: Sample script that spawns a simple child process and errors | ||
* | ||
* (C) 2010 and Charlie Robbins | ||
* MIT LICENCE | ||
* | ||
*/ | ||
|
||
var sys = require('sys'), | ||
path = require('path'), | ||
spawn = require('child_process').spawn; | ||
|
||
var child = spawn('node', [path.join(__dirname, 'count-timer.js')]); | ||
|
||
child.stdout.on('data', function (data) { | ||
sys.puts(data); | ||
throw new Error('User generated fault.'); | ||
}); | ||
|
||
child.stderr.on('data', function (data) { | ||
sys.puts(data); | ||
}); | ||
|
||
child.on('exit', function (code) { | ||
sys.puts('Child process exited with code: ' + code); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* forever-test.js: Tests for forever module | ||
* | ||
* (C) 2010 and Charlie Robbins | ||
* MIT LICENCE | ||
* | ||
*/ | ||
|