Skip to content

Commit

Permalink
Merge pull request #67 from joelgriffith/bugfix/job-based-opt-out
Browse files Browse the repository at this point in the history
Ensuring jobs can opt-out of the "global" timeout
  • Loading branch information
jessetane committed Aug 10, 2019
2 parents 691f70e + 0edff8c commit 9ca37f0
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
11 changes: 11 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ function extraSlowJob (cb) {
extraSlowJob.timeout = 500
q.push(extraSlowJob)

// jobs can also opt-out of the timeout altogether
function superSlowJob (cb) {
setTimeout(function () {
console.log('super slow job finished')
cb()
}, 1000)
}

superSlowJob.timeout = null
q.push(superSlowJob)

// get notified when jobs complete
q.on('success', function (result, job) {
console.log('job finished processing:', job.toString().replace(/\n/g, ''))
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Queue.prototype.start = function (cb) {
var timeoutId = null
var didTimeout = false
var resultIndex = null
var timeout = job.timeout || this.timeout
var timeout = job.hasOwnProperty('timeout') ? job.timeout : this.timeout

function next (err, result) {
if (once && self.session === session) {
Expand Down
11 changes: 11 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ function extraSlowJob (cb) {
extraSlowJob.timeout = 500
q.push(extraSlowJob)

// jobs can also opt-out of the timeout altogether
function superSlowJob (cb) {
setTimeout(function () {
console.log('super slow job finished')
cb()
}, 1000)
}

superSlowJob.timeout = null
q.push(superSlowJob)

// get notified when jobs complete
q.on('success', function (result, job) {
console.log('job finished processing:', job.toString().replace(/\n/g, ''))
Expand Down
26 changes: 26 additions & 0 deletions test/timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,32 @@ tape('job timeout', function (t) {
q.start()
})

tape('job-based opt-out of timeout', function (t) {
t.plan(1)

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

wontTimeout.timeout = null

q.on('timeout', function (next) {
t.fail('Job should not have timed-out')
timeouts++
next()
})

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

q.push(wontTimeout)

q.start()
})

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

Expand Down

0 comments on commit 9ca37f0

Please sign in to comment.