So I'm trying to make a scheduled job that will send a reminder e-mail after 24h if the receiver hasn't responded yet on the first mail. I want to use Bull to schedule these jobs and so far everything works except it's not sending the e-mails. In fact when it reaches the function it just does nothing, no error no following code nothing.
'use strict'
const pool = require('../database')
const Mailer = require('../mailer')
module.exports = async (job) => {
const query = `SELECT * FROM confirmations WHERE order_id = ${job.data.order_id} AND location_id = ${job.data.location_id}`
console.log(query)
const result = await pool.query(query)
const order = result[0]
if(!order.has_confirmed){
console.log('Going to send reminders... ')
console.log(order.reminders)
if(order.reminders == 1){
console.log('sending reminder 1') // This is is reached
await Mailer.sendConfirmationMail(order.order_id, order.location_id, true, pool, 2)
// Code placed here is not executed
}else if(order.reminders == 2){
const mail = console.log('sending reminder 2')
await Mailer.sendConfirmationMail(order.order_id, order.location_id, true, pool, 3)
}else if(order.reminders == 3){
console.log('sending reminder 3')
const mail = await Mailer.sendConfirmationMail(order.order_id, order.location_id, true, pool, 4)
}
}else{
// if the receiver has confirmed the order the mail should expire without doing anything.
console.log('This job is expiring without doing anything...')
}
}
Did I do something wrong? The Mailer.sendConfirmationMail(...) is a bit big but works fine as it sends a mail when called from other parts of my app. Help appreciated!
EDIT:
Mailer is a class which has a couple of functions, basic structure is:
class Mailer {
static async sendConfirmationMail(orderId, locationId, reminder, pool, reminder number){
console.log('inside Mailer')
// check some stuff and do a db query
this.addJobConfirmationQueue(orderId, locationId, reminders, pool)
// send the email with the right info
}
static async addJobConfirmationQueue(orderId, locationId, reminders, pool){
let day = 10000;
//const day = 86400000; // milliseconds in a day
const queue = Bull('checkConfrimation')
queue.process(processCheckConfirmation)
const job = await queue.add({
order_id: order_id,
location_id: location_id,
}, { delay: day })
const q = `UPDATE confirmations
SET queue_id = ${pool.escape(job.id)}, reminders = ${pool.escape(reminders + 1)}
WHERE order_id = ${pool.escape(order_id)}
AND location_id = ${pool.escape(location_id)}`
const [rows, fields] = await pool.query(q)
}
}
module.exports = Mailer
Logs I don't really have. I console.log(...) certain phrases so I know it passed a certain point. The chain of console logs are:
- SQL query
- 'Going to send reminders'
- amount of reminders
- 'sending reminder 1'
So instead of different files I fixed it by adding all the code in my Mailer class:
static async addJobConfirmationQueue(order_id, location_id, reminders, pool){
const day = 86400000; // milliseconds in a day
const queue = new Bull('checkConfirmation')
queue.process(async (job) => {
const query = `SELECT * FROM confirmations WHERE order_id = ${job.data.order_id} AND location_id = ${job.data.location_id}`
const result = await pool.query(query)
const order = result[0][0]
if(!order.has_confirmed){
if(order.reminders == 1){
this.sendConfirmationMail(order.order_id, order.location_id, true, pool, 2)
}else if(order.reminders == 2){
this.sendConfirmationMail(order.order_id, order.location_id, true, pool, 3)
}else if(order.reminders == 3){
this.sendConfirmationMail(order.order_id, order.location_id, true, pool, 4)
}
}else{
// if the dealer has confirmed the order the mail should expire without doing anything.
}
})
const job = await queue.add({
order_id: order_id,
location_id: location_id,
}, { delay: day })
const q = `UPDATE confirmations
SET queue_id = ${pool.escape(job.id)}, reminders = ${pool.escape(reminders + 1)}
WHERE order_id = ${pool.escape(order_id)}
AND location_id = ${pool.escape(location_id)}`
const [rows, fields] = await pool.query(q)
}