繁体   English   中英

Node.js 12.x AWS lambda 不使用 firebase-admin 实时数据库返回

[英]Node.js 12.x AWS lambda does not return using firebase-admin realtime database

我已经在这个问题上卡了几天了,几乎没有进展,如果可以的话,请帮忙!

I have a Node.js (v12) AWS Lambda that needs to pull data from my Firebase realtime database and process each record into a Redis cache if it doesn't exist already. function 启动但从未完成,相反我收到来自 AWS Task timed out after 180.10 seconds

我尝试过的事情:

  • 使用exports.handler = async function(event)exports.handler = function(event, context, callback) ;
  • 对于上面的同步尝试,我尝试使用context.callbackWaitsForEmptyEventLoop = false不是;
  • 使用承诺级联函数将一堆.then()拼接在一起;
  • 将 firebase firebase-adminhttps模块与 Firebase REST ZDB97477ACE41 结合使用
  • 使用settimeout稍后触发回调不是不使用;
  • GOOGLE_APPLICATION_CREDENTIALS环境变量设置为我的服务帐户凭据,而不是直接在代码中引用文件;
  • I've even beefed-up the memory and timeout of the Lambda itself to the maximum it can go, as well as cut down the data I want to pull from Firebase to just 1 record.

根据上述尝试,我得到的回应:

  • AWS(最频繁): Task timed out after 180.10 seconds
  • AWS( .then拼接方式): Function completed successfully (但实际没有处理数据);
  • 节点 HTTPS(REST API 方法): ETIMEDOUTECONNREFUSED

下面是我的目标,但仍然没有运气。 我已经删除了缓存代码,因为我知道它可以正常工作。 你看到的settimeout是我到达这里之前的最后手段。

const admin = require("firebase-admin");
admin.initializeApp({
    credential: admin.credential.applicationDefault(),
    databaseURL: "https://{projectName}.firebaseio.com"
});
var result = [];
exports.handler = (event, context, callback) => {
    context.callbackWaitsForEmptyEventLoop = false;
    try {
        admin.database().ref("data").orderByChild("timestamp").limitToLast(1).once("value", snapshot => {
            if (snapshot.exists()) {
                console.log('snapshot exists...');
                let posts = snapshot.val();
                result = Object.keys(posts);
            }
            setTimeout(() => {
                admin.database().goOffline();
                admin.app().delete();
                callback(null, `Success! ${JSON.stringify(result)}`); // <-- NEVER RETURNS
            }, 2000);
        }, error => { 
            setTimeout(() => {
                admin.database().goOffline();
                admin.app().delete();
                callback(error); // <-- NEVER RETURNS
            }, 2000);
        });
    } catch (error) {
        setTimeout(() => {
            admin.database().goOffline();
            admin.app().delete();
            callback(error); // <-- NEVER RETURNS
        }, 2000);
    }
};

您似乎没有在 function 的根级别上存储或使用 setTimeout。 您应该存储它,以便回调 function 可以继续运行,因为它只存在于 scope 中。 这样做还需要您绑定 object 以便在决定将其推入数组以进行多个回调时拥有自引用

var result = [];
var timeOut;
//...
timeOut = setTimeout(() => {
            admin.database().goOffline();
            admin.app().delete();
            callback(error);
        }.bind(this), 2000);

来源: MSDN Function.prototype.bind()

如果 Binding 不是解决方案并且您想要一种阻塞方法,您可能对延迟 function 感兴趣,它的行为与 setTimeout 相同,但适用于 Promise

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

// Async function solution
await sleep(2000);
  console.log('Two seconds later, showing sleep in a loop...');

// non-Async solution
sleep(2000)
.then(()=> {
            admin.database().goOffline();
            admin.app().delete();
            callback(error);
        })
.catch(e => console.log(e));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM