1

I am trying to schedule to sent out an email using NodeMailer and Agenda, however, I don't see anything happening with the below code in 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...');

When I run the above code using node index.js, i see the following console message

Wait 10 seconds...

Basically, I am trying to schedule the email functionality to run for every 5 secs.

Where am I doing wrong?

EDIT: It seems to be with the job implementation versus:

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

I need the job object after the fact though, so if there is a way to get that from running the above command, that would be an acceptable answer.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
WonderHeart
  • 678
  • 10
  • 28

1 Answers1

0

Agenda calls the 'ready' event when it successfully establishes it's mongo connection. I don't see you setting that up. Something like:

 var Agenda = require('agenda');
 var mongoConnectionString = "mongodb://127.0.0.1/agenda";
 var agenda = new Agenda({db: {address: mongoConnectionString}});
ReddShepherd
  • 467
  • 1
  • 11
  • 24
  • I am not referring to mongo here, just smtp server – WonderHeart Mar 24 '17 at 21:22
  • Ignore my other comment, I thought I was in an entirely different question. Agenda relies on mongo. It's where it stores all of it's jobs. So if you aren't setting up mongo, they aren't going to run. – ReddShepherd Mar 25 '17 at 15:04