Automattic / kue

Kue is a priority job queue backed by redis, built for node.js.
http://automattic.github.io/kue
MIT License
9.45k stars 862 forks source link

Job Missing when application server restarts #1112

Open sunojvijayan opened 6 years ago

sunojvijayan commented 6 years ago

Hi I ran the simple code to actually send email. in between i restarted my node server using pm2 restart. I noticed two things.

  1. when it restarted one job went missing (is this normal). See code below I had 10 jobs to send email. But only 9 emails were send. Is there a way to prevent it.

  2. After restart complete event never got fired even though emails were send.

0|| Message sent: undefined 0|| Job 46 with name Send Invitation Email is done 0|| Message sent: undefined 0|| Job 47 with name Send Invitation Email is done 0|| Message sent: undefined 0|| Job 48 with name Send Invitation Email is done 0|| info: Server started at port 4000 0|| Message sent: undefined 0|| Message sent: undefined 0|| Message sent: undefined 0|| Message sent: undefined 0|| Message sent: undefined 0|| Message sent: undefined`

my full code is below

`var kue = require('kue'), jobs = kue.createQueue(), nodemailer = require('nodemailer'), smtpTransport = require('nodemailer-smtp-transport');

var smtpTransport = nodemailer.createTransport(smtpTransport({ service: 'gmail', // host:'smtp.gmail.com', // port:465, // secure:true, auth: { user: "email", pass: "pass" } }));

var sendInvitationEmail = function (data, user) { //name = name || 'Default_Name'; var job = jobs.create('send_invite_email', { name: 'Send Invitation Email', invitation: data, username: user.name, email: user.email }); job .on('complete', function () { console.log('Job', job.id, 'with name', job.data.name, 'is done'); }) .on('failed', function () { console.log('Job', job.id, 'with name', job.data.name, 'has failed'); }); job.save(); }

jobs.process('send_invite_email', function (job, done) { / carry out all the job function here / smtpTransport.sendMail({ //email options from: "xxxxxxxxxx", // sender address. Must be the same as authenticated user if using Gmail. to: "xxxxxx@gmail.com", // receiver subject: "xxxx ", // subject text: "Text comes here. Title --- " + job.data.invitation.name // body

}, function (error, response) { //callback
    if (error) {
        console.log(error);
        done && done();
    } else {
        console.log("Message sent: " + response.message);
        done && done();
    }
    smtpTransport.close(); // shut down the connection pool, no more messages.  Comment this line out to continue sending emails.
});

});

module.exports = { sendInvitationEmail: sendInvitationEmail }; `

sunojvijayan commented 6 years ago

Suggestions please