Skip to content

Commit

Permalink
support timeout option in in-process strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrmatos committed Feb 24, 2017
1 parent f460290 commit 25bc7fa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/in-process.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var ScriptsManager = module.exports = function (options) {
this.options = options
this.options.timeout = this.options.timeout || 10000
}

ScriptsManager.prototype.start = function (cb) {
Expand All @@ -10,7 +12,24 @@ ScriptsManager.prototype.ensureStarted = function (cb) {
}

ScriptsManager.prototype.execute = function (inputs, options, cb) {
require(options.execModulePath)(inputs, options.callback, cb)
var resolved = false

var timeout = setTimeout(function () {
cb(new Error('Timeout error during executing script'))
}, options.timeout || this.options.timeout)

timeout.unref()

require(options.execModulePath)(inputs, options.callback, function (err, res) {
if (resolved) {
return
}

resolved = true
clearTimeout(timeout)

cb(err, res)
})
}

ScriptsManager.prototype.kill = function () {
Expand Down
23 changes: 22 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('scripts manager', function () {
})

describe('in process', function () {
var scriptsManager = new ScriptManagerInProcess()
var scriptsManager = new ScriptManagerInProcess({})

beforeEach(function (done) {
scriptsManager.ensureStarted(done)
Expand All @@ -165,6 +165,27 @@ describe('scripts manager', function () {
})

common(scriptsManager)

it('should handle timeouts', function (done) {
var timeouted = false

scriptsManager.execute({ foo: 'foo' },
{
execModulePath: path.join(__dirname, 'scripts', 'timeout.js'),
timeout: 10
}, function (err) {
if (err) {
timeouted = true
done()
}
})

setTimeout(function () {
if (!timeouted) {
done(new Error('It should timeout'))
}
}, 500)
})
})

function commonForSafeExecution (scriptsManager) {
Expand Down

0 comments on commit 25bc7fa

Please sign in to comment.