使用Cloud Functions太早超时

时间:2019-02-05 00:33:25

标签: firebase google-cloud-functions

我有一个获取所有用户令牌的代码,并向所有人发送通知,并且我有火力爆发,而我的限制是从540秒超时,这就是我的代码:

function listAll(){
  return new Promise((resolve) => { 
  usersRef.once('value', (snapShot) => {
  let valor = [];
  for( let key in snapShot){
    valor.push(key); //Becuase in my ref, key its the uid!
  }
  resolve({valor})
})
})
}


exports.updateNotification = functions.database.ref('userNotification/{push_Id}').onCreate(event =>{ 
let original = event.val();
let titulo = original.titulo;
let body = original.body;

let pushId = event.params.push_Id;
listAll().then((res) => {
let valor = (res.valor);
let valorDiv = valor.length / 10;
return admin.database().ref('user_notification/${pushId}').update({
  title: titulo,
  body: body,
  vezes: 10,
  valorDividido: valorDiv,
})

})

})

exports.sendNotification= functions.database.ref('userNotification/{push_Id}').onUpdate(event = {
let original = event.val();
let vezes = original.vezes;
let titulo = origina.title;
let body = original.body;
let pushId = event.params.push_Id;  
let valorDividido = original.valorDividido;
if(vezes < 0){
 return sendNotToAll(vezes,titulo,body,valorDividido);
}
})

function sendNotToAll(vezes,titulo,body,valorDividido,pushId){
 listAll().then((res) => {
 let allUserUid = (res.valor);
 let mult = vezes - 10;
 let array = AllUserUid.splice(valorDividido * mult, valorDividio); //for get to 0 to valorDivido or more if its not the first time.
 array.forEarch(element =>{
 admin.ref('users/' + element).child('token').once('value', (snapShot) => {
if(snapShot.val()){
  let token = snapShot.val();
  let body //messagem
  let etc .... // 
  return admin.messaging().sendToDevice(token, body, option);
} 
}) 
})
 //HERE I CAN UPDATE AGAIN FROM CALL THE FUNCTION AGAIN
 return admin.database().ref('userNotification/' + pushId).update({
  title: titulo,
  body: body,
  vezes: 10 - 1,
  valorDividido: valorDiv,
})
})
}

我编写所有这些代码是因为它有很多用户,因此我需要对所有用户执行10次操作,但是由于他第三次运行该数组(例如3分钟)的原因,他超时了!!!有人可以帮助我,告诉我我的代码是否有问题,或者我需要与firebase谈谈?

0 个答案:

没有答案