Skip to content

Instantly share code, notes, and snippets.

@cincodenada
Last active Sep 27, 2021
Embed
What would you like to do?
A version of my Bull demo script that attempts to clear jobs during their run, used to demonstrate bug #2167
const Queue = require('bull')
const delay = require('delay')
const shouldReAdd = process.argv.includes('--readd')
const timelog = (...args) => { console.log(+Date.now(), ...args) }
;(async () => {
timelog('Creating queue')
const queue = new Queue('example')
queue.process(async job => {
timelog(`Started job ${job.id}`)
job.progress(42)
await delay(300)
timelog(`Finished job ${job.id}`)
})
const existing = await queue.getRepeatableJobs()
if (existing.length && !shouldReAdd) {
timelog('Using existing repeatable job', existing[0].key)
} else {
const job = await queue.add({}, { repeat: { cron: '* * * * * *' } })
timelog('Added repeatable job', job.opts.repeat.key)
}
// Let some jobs run...
await delay(3000)
// Try to cancel in the middle of a run...
queue.on('progress', () => {
timelog('Clearing delayed jobs during run...')
queue.empty().then(timelog)
//queue.clean(0, 'delayed').then(timelog)
})
// Let them run more...
await delay(3000)
// Switch to cancelling after the job is done
queue.on('completed', () => {
timelog('Clearing delayed jobs after completion...')
queue.empty().then(timelog)
//queue.clean(0, 'delayed').then(timelog)
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment