Skip to content

Commit

Permalink
Add timeout support
Browse files Browse the repository at this point in the history
  • Loading branch information
dvonlehman committed Aug 11, 2016
1 parent 6fbd67b commit 7b02cdc
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Yet another process execution wrapper. Uses `child_process.spawn` to execute an
* Supports optional log filter
* Protects against double callbacks from `error` and `exit` events.
* Invokes callback with an Error if process exits with non-zero code
* Specify an optional timeout. If the process has not exited within the interval the process is force killed and a `TIMEOUT` error is passed in the callback.

### Usage

Expand All @@ -24,12 +25,14 @@ var params = {
executable: 'git',
args: ['clone', 'https://github.com/nodejs/node.git'],
logger: winston,
timeout: 5000, // 5 seconds
logFilter: function(level, msg) {
return level !== 'info';
}
};

yexec(params, function(err) {
// If timeout occurred, err.code will be 'TIMEOUT'
winston.error('Oops, git failed with code %s', err.code);
});
~~~
35 changes: 27 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,11 @@ module.exports = function(params, callback) {

debug('spawning %s %s', params.executable, params.args.join(' '));

var process;
try {
process = spawn(params.executable, params.args, options);
} catch (err) {
return callback(err);
}

var processExited;
var processTimedOut;
var filter;

// If the
// If logFilter is an array
if (isArray(params.logFilter)) {
filter = function(level, msg) {
return !some(params.logFilter, function(pattern) {
Expand All @@ -49,6 +43,13 @@ module.exports = function(params, callback) {
}
};

var process;
try {
process = spawn(params.executable, params.args, options);
} catch (err) {
return callback(err);
}

// Log stdout to the log as info
process.stdout.on('data', function(data) {
log('info', data);
Expand All @@ -70,6 +71,13 @@ module.exports = function(params, callback) {
process.on('exit', function(code) {
if (processExited) return;
processExited = true;

if (processTimedOut === true) {
var error = new Error('Process ' + executableBaseName + ' timed out');
error.code = 'TIMEOUT';
return callback(error);
}

if (isNumber(code) && code !== 0) {
var error = new Error('Process ' + executableBaseName + ' failed with code ' + code);
error.code = code;
Expand All @@ -78,4 +86,15 @@ module.exports = function(params, callback) {
callback();
}
});

if (isNumber(params.timeout)) {
// If the process still has not exited after the timeout period has elapsed,
// force kill it.
setTimeout(function() {
if (!processExited) {
processTimedOut = true;
process.kill();
}
}, params.timeout);
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yexec",
"version": "1.0.3",
"version": "1.1.0",
"description": "Yet another cmd execution wrapper that works the way I like it",
"main": "index.js",
"scripts": {
Expand All @@ -16,6 +16,7 @@
"lodash.isarray": "^4.0.0",
"lodash.isfunction": "^3.0.8",
"lodash.isnumber": "^3.0.3",
"lodash.isstring": "^4.0.1",
"lodash.pick": "^4.2.0",
"lodash.some": "^4.5.1"
},
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/timeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
setTimeout(function() {
process.exit();
}, 100);
14 changes: 14 additions & 0 deletions test/yexec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ describe('yexec', function() {
done();
});
});

it('kills process if not finished within timeout period', function(done) {
var log = new Log();
var params = {
executable: 'node',
args: [path.join(__dirname, './fixtures/timeout.js')],
logger: log,
timeout: 20
};
yexec(params, function(err) {
assert.equal(err.code, 'TIMEOUT');
done();
});
});
});

function Log() {
Expand Down

0 comments on commit 7b02cdc

Please sign in to comment.