Skip to content

Commit

Permalink
taskTimeout #9
Browse files Browse the repository at this point in the history
  • Loading branch information
robrich committed Jan 19, 2014
1 parent 40b08f3 commit a528f91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var Orchestrator = function () {
this.seq = []; // the order to run the tasks
this.tasks = {}; // task objects: name, dep (list of names of dependencies), fn (the task to run)
this.isRunning = false; // is the orchestrator running tasks? .start() to start, .stop() to stop
this.taskTimeout = 20*1000; // ms until the task fails
};
util.inherits(Orchestrator, EventEmitter);

Expand Down Expand Up @@ -270,7 +271,7 @@ util.inherits(Orchestrator, EventEmitter);
this.emit('task_start', task.args);
task.running = true;

runTask(task.fn.bind(this), function (err, meta) {
runTask(task.fn.bind(this), task.name, this.taskTimeout, function (err, meta) {
that._stopTask.call(that, task, meta);
that._emitTaskDone.call(that, task, meta.runMethod, err);
if (err) {
Expand Down
19 changes: 16 additions & 3 deletions lib/runTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

"use strict";

module.exports = function (task, done) {
var that = this, finish, cb, isDone = false, start, r, streamError;
module.exports = function (task, taskName, taskTimeout, done) {
var that = this, finish, cb, isDone = false, start, r, streamError, timeoutHandle, timedOut = false;

finish = function (err, runMethod) {
var hrDuration = process.hrtime(start);

if (timeoutHandle) {
clearTimeout(timeoutHandle);
timeoutHandle = null;
} else if (!err && timedOut) {
return; // it timed out previously, and just now finished successfully
}

if (isDone && !err) {
err = new Error('task completion callback called too many times');
}
Expand All @@ -27,6 +34,12 @@ module.exports = function (task, done) {
};

try {
timeoutHandle = setTimeout(function () {
if (!isDone) {
finish(new Error('task \''+taskName+'\' timed out, waited '+taskTimeout+' ms'), 'timeout');
timedOut = true;
}
}, taskTimeout);
start = process.hrtime();
r = task(cb);
} catch (err) {
Expand Down Expand Up @@ -56,7 +69,7 @@ module.exports = function (task, done) {
});

} else if (task.length === 0) {
// synchronous, function took in args.length parameters, and the callback was extra
// synchronous, function took in no parameters
finish(null, 'sync');

//} else {
Expand Down

0 comments on commit a528f91

Please sign in to comment.