简体   繁体   English

Firebase:批量更新用户

[英]Firebase: update users in batch

I'm trying the update infos emailVerified and password of a list of users, however I am struggling to make this work properly.我正在尝试更新信息 emailVerified 和用户列表的密码,但是我正在努力使其正常工作。 I'm getting the request completed with timeout status, without been executed for all users in array.我正在以超时状态完成请求,但没有为数组中的所有用户执行。

Here is a snippet of my code:这是我的代码片段:

exports.updateUserInfo = functions.https.onRequest(async (req, res) => {
    let users = getUIDsToUpdateInfo();    
    let timeout = 0;

    for(let uid of users){
      console.log(`${uid}: Preparing to update user at ${timeout} ms` )

      // Once firebase not handle well multiple calls, create a delay for each call
      setTimeout(() => {
        let newPass = getNewPassForUser(uid);
        
        admin.auth().updateUser(uid, {
          password: newPass,
          emailVerified: false
        })
        .then(() => {
          console.log(`uid: ${uid} - User info has been changed.`);
        }).catch((error) => {
          console.log(`uid: ${uid} - Fail to change user info`, error);
        }); 
      }, timeout)
      timeout += 200;
    }

    setTimeout(() => {
      res.status(200).send("OK!");
    }, timeout)
});

When uid's array is too big, (eg: more than 250), the function finish with: Function execution took 60004 ms, finished with status: 'timeout' without execute to all uids.当 uid 的数组太大时(例如:超过 250),函数完成: Function execution took 60004 ms, finished with status: 'timeout'没有对所有 uid 执行。

Is there a way to execute all this calls in background and send response of request immediately?有没有办法在后台执行所有这些调用并立即发送请求响应?

You can increase the timeout of a function if it's going to take longer than the default timeout of 60 seconds.如果函数的超时时间超过 60 秒的默认超时时间,您可以增加该函数的超时时间。

If you have more work to perform than can't be finished with the maximum timeout of 9 minutes, you will have to find some way to queue up work and execute it in batches so that the http function can send a response before the timeout expires.如果您要执行的工作多于 9 分钟的最大超时无法完成的工作,则必须找到某种方法将工作排队并分批执行,以便 http 函数可以在超时到期之前发送响应. You might do this by sending a messages to a pubsub function that for each account to modify.您可以通过向pubsub 函数发送消息来为每个帐户进行修改来实现此目的。

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

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