使用议程

时间:2017-02-02 19:36:15

标签: javascript node.js gmail nodemailer agenda

我打算安排使用NodeMailer和Agenda发送电子邮件,但是,我在index.js中看不到以下代码发生的任何事情:

var Agenda = require('agenda');
var agenda = new Agenda();
const nodemailer = require('nodemailer');

agenda.define('send email', {priority: 'high', concurrency: 10}, function(job, done) {
    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'xxxx@gmail.com',
            pass: 'xxxx'
        }
    });

    // setup email data with unicode symbols
    let mailOptions = {
        from: '"Test" <xxxx@gmail.com>', // sender address
        to: 'xxxx@gmail.com', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world ?', // plain text body
        html: '<b>Hello world ?</b>' // html body
        };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function (error, response) {
        console.log('Message sent: ' + response.message);
        transporter.close();
        done();
    });
});

agenda.on('ready', function() {
    agenda.every('5 seconds', 'send email'); 
    agenda.start();
});

agenda.on('start', function (job) {
    console.log("Job %s starting", job.attrs.name);
});

agenda.on('complete', function (job) {
    console.log("Job %s finished", job.attrs.name);
});

console.log('Wait 10 seconds...');

当我使用node index.js运行上述代码时,我看到以下控制台消息

Wait 10 seconds...

基本上,我正在尝试安排电子邮件功能每5秒运行一次。

我在哪里做错了?

编辑:似乎与工作实施相对:

 agenda.every('5 seconds', jobName);

我事后需要作业对象,所以如果有办法从运行上面的命令中得到这个,那将是一个可以接受的答案。

1 个答案:

答案 0 :(得分:0)

议程在成功建立mongo连接时调用'ready'事件。我没有看到你设置它。类似的东西:

 var Agenda = require('agenda');
 var mongoConnectionString = "mongodb://127.0.0.1/agenda";
 var agenda = new Agenda({db: {address: mongoConnectionString}});