Created
March 20, 2019 19:30
-
-
Save nicoayala/87f1e835b5032e3afcea3225dc3c250d to your computer and use it in GitHub Desktop.
Bull queue concurrency 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
import Queue from 'bull'; // Using https://github.com/OptimalBits/bull/tree/v3.4.8 | |
const url = 'redis://127.0.0.1:6379'; | |
const prefix = 'queue-app'; | |
export const createQueue = name => new Queue(name, url, { | |
prefix: `${prefix}:jobs`, | |
defaultJobOptions: { | |
backoff: { type: 'linear' }, | |
attempts: 20, | |
}, | |
settings: { | |
backoffStrategies: { | |
linear: attemptsMade => attemptsMade * 10 * 1000, | |
}, | |
}, | |
}); | |
const queue = createQueue('ConcurrentQueue'); | |
const doSomethingThatTakesLong = async (params, log) => | |
new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log(log); | |
resolve(params); | |
}, 5000); | |
}); | |
// Concurrency === 1 (default) | |
queue.process('func1', async ({ data: { params } }) => { | |
console.log('Start func1'); | |
await doSomethingThatTakesLong(params, 'End func1'); | |
return true; | |
}); | |
// Concurrency === 1 (default) | |
queue.process('func2', async ({ data: { params } }) => { | |
console.log('Start func2'); | |
await doSomethingThatTakesLong(params, 'End func2'); | |
return true; | |
}); | |
queue.add('func1', { params: 'hi one' }); | |
queue.add('func2', { params: 'hi two' }); | |
queue.add('func1', { params: 'hi one two' }); | |
queue.add('func2', { params: 'hi two two' }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment