【问题标题】:What is the right way to create a consumer in Bull NodeJS?在 Bull NodeJS 中创建消费者的正确方法是什么?
【发布时间】:2020-03-12 21:28:39
【问题描述】:

因此,我正在尝试制定一个计划的工作,如果收件人尚未对第一封邮件做出回应,它将在 24 小时后发送一封提醒电子邮件。我想用 Bull 来安排这些工作,到目前为止一切正常,除了它不发送电子邮件。事实上,当它到达函数时,它什么也不做,没有错误,没有以下代码。

'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...')
    }
}

我做错了吗? Mailer.sendConfirmationMail(...) 有点大,但工作正常,因为它在从我的应用程序的其他部分调用时会发送邮件。感谢您的帮助!

编辑:

Mailer 是一个有几个功能的类,基本结构是:

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

我没有的日志。我 console.log(...) 某些短语,所以我知道它通过了某个点。控制台日志链是:

  1. SQL 查询
  2. '要发送提醒'
  3. 提醒数量
  4. '发送提醒 1'

【问题讨论】:

  • 即使我删除 Mailer.sendConfirmationMail(...) 前面的'await',它下面的代码也不会被执行。
  • 能否也提供sendConfirmationMail?一个最小的工作示例。既然你有日志,你能否提供在卡住之前打印出来的日志链?
  • 你有没有尝试调试到它到底是哪一行?它可能在sendConfirmationMail 内。由于我们看不到源代码,因此无法确定那里发生了什么。
  • 在sendConfirmationMail(...) 的开头是一个console.log(...) 但是进去的时候没有登录。

标签: javascript node.js


【解决方案1】:

因此,我通过在我的 Mailer 类中添加所有代码来修复它,而不是使用不同的文件:

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)
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 2014-04-12
    • 2023-03-31
    相关资源
    最近更新 更多