Skip to content

Commit

Permalink
Merge pull request #60 from joelgriffith/job-timeouts
Browse files Browse the repository at this point in the history
POC for job-specific timeouts
  • Loading branch information
jessetane committed Feb 24, 2019
2 parents dccd818 + 8124352 commit 1278a75
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Queue.prototype.start = function (cb) {
var timeoutId = null
var didTimeout = false
var resultIndex = null
var timeout = job.timeout || this.timeout

function next (err, result) {
if (once && self.session === session) {
Expand Down Expand Up @@ -122,15 +123,15 @@ Queue.prototype.start = function (cb) {
}
}

if (this.timeout) {
if (timeout) {
timeoutId = setTimeout(function () {
didTimeout = true
if (self.listeners('timeout').length > 0) {
self.emit('timeout', next, job)
} else {
next()
}
}, this.timeout)
}, timeout)
this.timers[timeoutId] = timeoutId
}

Expand Down
30 changes: 30 additions & 0 deletions test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ tape('timeout', function (t) {
q.start()
})

tape('job timeout', function (t) {
t.plan(2)

var timeouts = 0
var q = queue({ timeout: 5 })
function willTimeout (cb) {
setTimeout(cb, 8)
}
function wontTimeout (cb) {
setTimeout(cb, 8)
}

wontTimeout.timeout = 10

q.on('timeout', function (next) {
t.ok(q)
timeouts++
next()
})

q.on('end', function () {
t.equal(timeouts, 1)
})

q.push(willTimeout)
q.push(wontTimeout)

q.start()
})

tape('timeout auto-continue', function (t) {
t.plan(3)

Expand Down

0 comments on commit 1278a75

Please sign in to comment.