2017-03-11 77 views
0

我有以下代码上kueKue没有正确处理事件?

require('dotenv').load(); 
const mailer = require('../helper/mailer'); 
const kue = require('kue'), 
    queue = kue.createQueue(); 

console.log("Starting server"); 
queue.process('email',function(job,done){ 
    console.log(job.data); 
    mailer 
    .prepareContext(job.data) 
    .then(mailer.prepareBody) 
    .then(mailer.prepareMail) 
    .then(mailer.sendMail) 
    .then((data)=>{ 
     console.log("Mail sent"); 
    }) 
    .catch((err)=>{ 
     console.log(err.message); 
    }); 
}); 

queue.on('error',(err)=>{ 
    console.log(err); 
}); 

发送邮件的问题是,它仅响应于第一事件。我必须重新启动脚本才能发送另一个脚本。我在这里做错了什么? Processing Concurrency 处理并发

默认情况下调用queue.process()将一次只接受一个任务:

我使用

helper.sendVerificationMail = function(data){ 
    return new Promise(function(fullfill,reject){ 
    try{ 
     var ctx = {}; 
     ctx.from = "account"; 
     ctx.to_email = data.email; 
     ctx.subject = "Verifiy your email address"; 
     ctx.template = "signup"; 
     ctx.ctx = {}; 
     ctx.ctx.verification = data.verification; 
     queue.create('email',ctx).save(); 
     fullfill(data); 
    }catch(err){ 
     reject(err); 
    } 
    }); 
}; 

回答

0

我发现这个文档上添加事件进行处理。对于像发送电子邮件这样的小任务,这并不理想,因此我们可以通过传递一个数字来指定此类型的最大活动作业:

queue.process('email', 20, function(job, done){ 
    // ... 
});