Created
February 15, 2021 17:31
-
-
Save evilbuck/84836a0cb2f63448b18084ad19717fd5 to your computer and use it in GitHub Desktop.
rate limiting bull queue example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Bull = require('bull'); | |
const queue = new Bull('test', { | |
limiter: { | |
max: 1, | |
duration: 1000 | |
} | |
}); | |
queue.process(async job => { | |
let { data } = job; | |
let { fail } = data; | |
console.log(`x: ${data.x} - ${job.id} - processing`); | |
if (fail) { | |
throw new Error('test error'); | |
} | |
return { tada: job.id }; | |
}); | |
queue.on('failed', (job, error) => { | |
console.log(`x: ${job.data.x} job ${job.id} failed ${error}`); | |
}); | |
(async () => { | |
let myQueue = queue; | |
await myQueue.clean({ grace: 9000000 }); | |
myQueue.add({ fail: true, x: 200 }, { attempts: 2 }); | |
for (let i = 0; i < 10; i++) { | |
myQueue.add({ x: i }); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment