首页 > 解决方案 > 我可以使用函数触发器来调度函数吗?

问题描述

在对集合中的一个文档进行了一些更改后,我需要执行一个 cronjob 函数

例如,当我的用户商店打开时,我需要启动一个 cronjob,它会在用户指定的时间关闭该商店,我当时的想法是这样的

exports.updateUser = functions.firestore
    .document('stores/{storeId}')
    .onUpdate((change, context) => {

     //Logic to get the store close time here, lets say I get close in 4 hours
     //Now here I plan to run a cronjob like this

     exports.scheduledFunctionCrontab = functions.pubsub.schedule('* 4 * * *')
  .timeZone('America/New_York') // Users can choose timezone - default is America/Los_Angeles
  .onRun((context) => {

   //Close my store in 4 hours

});


    });

我可以这样做吗?有没有办法做这样的事情?

谢谢 !

标签: javascriptandroidfirebasegoogle-cloud-functions

解决方案


您不能从另一个云功能更改计划的云功能的配置。

就您而言,一种解决方案是:

  1. 在商店文档的特定字段中写下关闭时间;
  2. 安排一个云函数,它 (a) 例如每分钟运行一次,(b) 检查是否有任何打开的商店文档的关闭时间低于当前运行时间,以及 (c) 是否有任何此类商店文档将其状态修改为关闭。

推荐阅读