首页 > 解决方案 > 如何在任何集合的onWrite之后部署cron作业云功能

问题描述

根据以下文档是运行 cron 作业的代码

exports.cronJob = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});

但是我想在检测到任何更改时运行 cron 作业

exports.changeDetected = functions.firestore
  .document('/myTable/{myID}')
  .onWrite((snapshot, context) => {
    const dataAfterWrite = snapshot.after.data();
    const myID = context.params.myID;
    /*I want to deploy a cron job cloud function from here with the name of myID. something like this:

  exports.{$`myID`} = functions.pubsub.schedule('5 11 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {
  console.log('This will be run every day at 11:05 AM Eastern!');
  return null;
});
 */      
    return true;
  });

是否可以在 Firebase 云功能中做到这一点?当检测到任何集合中的任何更改时,我们可以部署 firebase cron 作业云功能吗?

标签: javascriptgoogle-cloud-firestoregoogle-cloud-functionsscheduled-taskscron-task

解决方案


让一个函数动态部署另一个函数是不可行的。我建议改为创建一个定期运行的函数,并让它检查在它触发时应该完成什么工作。可以使用 Firestore 中的文档来描述这项工作,因此您所要做的就是查询这些文档,并根据您找到的内容采取行动。


推荐阅读