我们如何使用Bull并发在Node JS队列中一次处理多个作业? 现在,基于下面的代码示例,我们是否可以处理一次添加到队列中的多个作业?因为我当前的实现现在可以一一处理了,
https://github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queueprocess
for(value of results.data) {
if(value.ImageList) {
//since image urls are comma delimited , we will split it to convert into an array
let ImageList = value.ImageList.split(',')
//adding jobs to th queue
queue.add({ ImageList: ImageList });
}
}
const downloadFiles = function () {
queue.process(function processor(job, done) {
let List = job.data.ImageList
let promises = [];
let download = function (url, filename, callback) {
request.get(url)
.pipe(crypto.createHash('md5'))
.on('finish', function () {
let hash = this.read().toString('hex')
gfs.findOne({ md5: hash }, function (err, file) {
if (!file) {
const file = filename
const fileStorage = gfs.createWriteStream({ filename: file, metadata: job.opts.VIN });
request.get(url)
.on('error', function (err) { console.log(err) })
.pipe(fileStorage)
.on('close', callback);
} else {
console.log("File already existed")
}
});
if (callback) {
callback(null, this.read());
}
});
};
List.forEach(function (str) {
let filename = str.split('/').pop();
console.log('Downloading ' + filename);
promises.push(new Promise(function p(resolve, reject) {
download(str, filename, function () {
console.log('Finished Downloading' + "" + filename);
resolve(filename);
});
}
));
});
Promise.all(promises)
.then(function d(filenames) {
done(null, filenames);
})
.catch(function e(error) {
done(error);
})
});
}
答案 0 :(得分:0)
docs you linked to explain this:
注意:如果未指定,则并发默认为1。
/**
* Consider these as overloaded functions. Since method overloading doesn't exist in javacript
* bull recognizes the desired function call by checking the parameters' types. Make sure you
* comply with one of the below defined patterns.
*
* Note: Concurrency defaults to 1 if not specified.
*/
process(processor: ((job, done?) => Promise<any>) | string)
process(concurrency: number, processor: ((job, done?) => Promise<any>) | string)
process(name: string, processor: ((job, done?) => Promise<any>) | string)
process(name: string, concurrency: number, processor: ((job, done?) => Promise<any>) | string)
尝试设置更高的并发性:
const workerConcurrency = 2;
queue.process(workerConcurrency, function processor(job, done) {