1

我正在尝试使用 NodeMailer 和议程安排发送电子邮件,但是,我没有看到以下代码发生任何事情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);

不过,事后我需要作业对象,所以如果有办法通过运行上述命令来获得它,那将是一个可以接受的答案。

4

1 回答 1

0

Agenda 在成功建立它的 mongo 连接时调用 'ready' 事件。我没有看到你设置它。就像是:

 var Agenda = require('agenda');
 var mongoConnectionString = "mongodb://127.0.0.1/agenda";
 var agenda = new Agenda({db: {address: mongoConnectionString}});
于 2017-03-22T20:13:32.560 回答