创建google云功能

问题描述 投票:0回答:1

我创建了一个带有时间表的云函数。 (我这样做是为了回答我之前发布的问题)。但是,我意识到时间表对函数的运行方式没有影响。因此,我不想导出计划,而是想删除计划导出并在简单的导出基础上运行它。

这是我的代码:

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const database = admin.firestore();
const min = "*/5 * * * *";
exports.scheduled = functions.pubsub.schedule(min).onRun(async (context) => {
  const batch = database.batch();
  const {docs} = await database.collection("ADSGHANA").get();
  docs.map((doc) => {
    const {
      AdsId,
      UID,
      TimestampArrival,
      TimestampDeparture,
      PostStatus,
      TimeToDelete,
      StartingTime,
      EndingTime,
    } = doc.data();
    const now = admin.firestore.Timestamp.now();
    const starting = now > TimestampDeparture && now <= StartingTime;
    const ending = now > TimestampArrival && now <= EndingTime;
    const deleting = now > TimeToDelete;
    let val = PostStatus;
    if (starting && PostStatus !== "Cancelled") val = "Started";
    if (ending && PostStatus !== "Cancelled") val = "Completed";
   //.....the rest of my code
  await batch.commit();
});

我的功能运行良好

const now = admin.firestore.Timestamp.now();
,因此不再需要 15 分钟的时间表。如何在没有时间表的情况下导出我的函数?

picture

我在不同链接上看到的示例

node.js firebase google-cloud-functions
1个回答
0
投票

如果您想在由动态值(例如 Firestore 文档中的 ``)确定的特定时间运行代码,则需要使用 通过 Cloud Tasks 将函数入队,而不是使用 CRON 计划.

© www.soinside.com 2019 - 2024. All rights reserved.