Skip to content

Commit

Permalink
[api test] Added core of forever and binary CLI runner
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Sep 27, 2010
1 parent 76467b2 commit ebc87fc
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
60 changes: 60 additions & 0 deletions bin/forever
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);
});
36 changes: 36 additions & 0 deletions lib/forever.js
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);
}
});
};

16 changes: 16 additions & 0 deletions samples/count-timer.js
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);
14 changes: 14 additions & 0 deletions samples/error-on-timer.js
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);
26 changes: 26 additions & 0 deletions samples/spawn-and-error.js
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);
});
8 changes: 8 additions & 0 deletions test/forever-test.js
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
*
*/

0 comments on commit ebc87fc

Please sign in to comment.