Skip to content

Instantly share code, notes, and snippets.

@cincodenada
Last active September 27, 2021 23:49
Show Gist options
  • Save cincodenada/c3028b5341727eb2757350a5b0d9a046 to your computer and use it in GitHub Desktop.
Save cincodenada/c3028b5341727eb2757350a5b0d9a046 to your computer and use it in GitHub Desktop.
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