1

如果我没有在无服务器函数结束时关闭数据库连接,我的脚本将挂起并超时。

我希望有一种方法可以在我的任何无服务器功能结束时运行清理功能,这将关闭活动的数据库连接等。

module.exports.create = (event, context, callback) => {
  user.insert({
    //user details
  }).then((results) => {
    responseHelper.success(JSON.stringify(results), callback);
  }, (error) => {
    // Connection error
    responseHelper.error(error, callback);
  });

  // I don't want to have this at the end of every function
  // I'd rather run it in a cleanup step which runs on all functions
  db.closeConnections();
}
4

1 回答 1

1

首先,user.insert()返回一个 Promise 并db.closeConnections()在它可能会在您仍然需要它时关闭连接后立即调用。要实现你想要做的,db.closeConnections()应该在参数之前callback调用。

也许您可以在执行参数之前调用db.closeConnections();您的辅助函数 responseHelper.success()和。我猜这些函数只写一次,并由你的所有 lambda 处理程序共享。responseHelper.error()callback

于 2017-10-27T13:38:49.373 回答