Skip to content

Email sent but callback is failed #9

Open
@endroca

Description

@endroca

Package version

NPM 6.12.1

Node.js and npm version

v10.16.0

Adonis version

4.1

Sample Code (to reproduce the issue)

JOB

/** @type {typeof import('adonisjs-queue/src/Job')} */
const Job = use('Job');

/** @type {typeof import('@adonisjs/mail/src/Mail')} */
const Mail = use('Mail');

class SendEmail extends Job {
  get queue() {
    return 'low';
  }

  constructor(emailAddress, emailFrom, emailSubject, emailTemplate, emailBody) {
    super(arguments);

    this.timeOut = 50; // seconds
    this.retryCount = 1;
    this.retryUntil = 200; // seconds
    // this.delayUntil = Date.parse('2038-01-19T03:14:08.000Z'); // optional, omit this line if not required
  }

  async handle(link, done) {
    console.log(
      `Job [${this.constructor.name}] - handler called: status=running; id=${this.id} `
    );

    link.reportProgress(1);

    let data = link.data; // arguments passed into the constructor
    let error = null;
    let result = null;

    try {
      result = await Mail.send(data.emailTemplate, data.emailBody, message => {
        message.to(data.emailAddress);
        message.from(data.emailFrom);
        message.subject(data.emailSubject);
      });
      link.reportProgress(45);
    } catch (err) {
      link.reportProgress(65);
      throw err;
    } finally {
      link.reportProgress(100);
    }

    return result;
  }

  failed(link, error) {
    console.log(
      `Job [${this.constructor.name}] - status:failed; id=${this.id} `,
      error
    );

    this.detach(); // remove the job from the queue (when the job fails after 3 retries)
  }

  retrying(link, error) {
    console.log(
      `Job [${this.constructor.name}] - status:retrying; id=${this.id} `,
      error
    );
  }

  succeeded(link) {
    console.log(
      `Job [${this.constructor.name}] - status:succeeded; id=${this.id} `
    );
  }
}

module.exports = SendEmail;

Event

/** @type {typeof import('@adonisjs/framework/src/Event')} */
const Event = use('Event');

/** @type {typeof import('adonisjs-queue/src/Queue')} */
const Queue = use('Queue');

const SendEmail = use('App/Jobs/SendEmail');

Event.on('user_registered', async (_email, _body) => {
  await Queue.dispatch(
    new SendEmail(
      _email,
      'support@....com.br',
      'Registro',
      'emails.UserStore',
      _body
    )
  );
});

Response

@@adonisjs/Queue: [Redis] Queue [low] now ready
Job [SendEmail] - handler called: status=running; id=1 
Job [SendEmail] - status:retrying; id=1  undefined
Job [SendEmail] - handler called: status=running; id=1 
Job [SendEmail] - status:failed; id=1  undefined

The email is sent successfully but the callback that is made is invalid

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions