【问题标题】:AWS Lambda not able to update all recordsAWS Lambda 无法更新所有记录
【发布时间】:2020-06-12 20:03:54
【问题描述】:

我正在使用带有 nodemailer 的 AWS SES 邮件。发送电子邮件时,为该电子邮件创建的消息 ID 存储在我的数据库中。收件人收到电子邮件后,在 SNS 的帮助下,我会收到一封通知电子邮件并触发 lambda。我正在使用这个 lambda 来获取电子邮件的更新状态并在我的数据库中更新它。我通过在 AWS 中上传包 zip 文件夹创建了 lambda 函数。

当我发送一封电子邮件时,会发送电子邮件并且 lambda 能够更新数据库。 但是如果我一次发送 10 封邮件,所有邮件都会发送,但 lambda 只更新 5-8 条记录。

Mysql:2.18.1 节点:12.x

Lambda的PFB代码,

const config = require("./config.json");


const getConnectionPool = ( dbname ) => {
  const params = {
    host: config.dbhost,
    user: config.dbuser,
    password: config.dbpassword,
    database: dbname,
    multipleStatements: true,
    // debug:true
  }
  const pool = mysql.createPool( params );
  return pool;
}

const getParameters = ( event ) => {
  const data =event.Records[0];
  let eventType = 0;
  const record = JSON.parse(data.Sns.Message);

  const mail =  record.mail;
  const headers = mail.headers[3];
  let dbname  = headers.value;
  dbname = dbname.replace("<", "");
  dbname = dbname.replace(">", "");

  if( record.eventType == "Delivery" ) {
    eventType = 2;
  } else if( record.eventType == "Bounce" ) {
    eventType = 3;
  }else if( record.eventType == "Complaint" ) {
    eventType = 4;
  }else if( record.eventType == "Reject" ) {
    eventType = 5;
  }
  return {
    eventType: eventType,
    messageId: mail.messageId,
    dbname: dbname
  }
}

exports.handler = (event, context, callback) => {

  let params  = getParameters( event );


  //prevent timeout from waiting event loop
  context.callbackWaitsForEmptyEventLoop = false;

  const pool = getConnectionPool( params.dbname );

  pool.getConnection( (err, connection) => {
    // Use the connection
     connection.query(
      "START TRANSACTION;SET SQL_SAFE_UPDATES = 0;update email_logs set bool_is_sent = " + mysql.escape( params.eventType ) + " where message_id= " + mysql.escape( params.messageId ) + ";SET SQL_SAFE_UPDATES = 1;COMMIT;", 
       (error, results ) => {

        // And done with the connection.
        connection.release();
        // Handle error after the release.
        if (error) callback(error);
        else callback(null, results[0]);
      }
    );
  });
};

只是 cloudwatch 日志的 sn-p

OkPacket {
    fieldCount: 0,
    affectedRows: 0,
    insertId: 0,
    serverStatus: 43,
    warningCount: 0,
    message: '(Rows matched: 0  Changed: 0  Warnings: 0',
    protocol41: true,
    changedRows: 0
  },

这里有什么问题。

【问题讨论】:

    标签: mysql node.js aws-lambda nodemailer amazon-ses


    【解决方案1】:

    如果 SNS 同时收到不同电子邮件的多个更新,则您的 lambda 函数可能会在单个事件中接收捆绑记录(因此您的 lambda 可能会被触发 3 次,例如 10 次更新)。

    您只访问第一条记录 (event.Records[0]),但可能有多个。

    【讨论】:

      【解决方案2】:

      所以问题是,首先调用 lambda,然后将消息 Id 存储在 Db 中。由于这个原因,lambda 没有获取更新记录,我得到了 0 个受影响的行。

      【讨论】:

        猜你喜欢
        • 2017-08-23
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-08
        • 1970-01-01
        • 2018-07-17
        • 1970-01-01
        相关资源
        最近更新 更多