【问题标题】:Firebase scheduled function not triggeringFirebase计划功能未触发
【发布时间】:2021-06-22 15:18:27
【问题描述】:

我有一个计划在美国东部标准时间上午 11 点触发。

正如您在下面的代码中看到的,我已将其设置为在东部时间时区 .timeZone("America/New_York") 上午 11 点 .schedule("0 11 * * *") 运行。

我目前在这个时区,等待函数触发,什么也没发生。

该函数已正确部署到我的 Firebase 项目中。只是好奇我是否在这里做错了什么。谢谢。

代码:

const admin = require("firebase-admin");
const functions = require("firebase-functions");

// function expires all EST timezone passes
exports.expirePassesEST = functions.pubsub
  .schedule("0 11 * * *")
  .timeZone("America/New_York")
  .onRun(async () => {
    // get all passes (passes & covers) that have a timezone offset of 240 (EST offset)
    const fastPassesRef = await admin
      .firestore()
      .collection("fastpasses")
      .where("timezoneOffset", "===", 240)
      .get();

    const coversRef = await admin
      .firestore()
      .collection("covers")
      .where("timezoneOffset", "==", 240)
      .get();

    // loop thorugh each fast pass and cover, and expire it
    fastPassesRef.forEach((fastPass) =>
      fastPass.ref.update({ status: "expired", expiredTimestamp: new Date() })
    );

    coversRef.forEach((cover) =>
      cover.ref.update({ status: "expired", expiredTimestamp: new Date() })
    );
  });

【问题讨论】:

  • "我等待函数触发,但什么也没发生" => 你怎么知道它没有运行?你在日志中看到了什么?有什么错误吗?
  • 另外,这是您完整的云函数代码吗?请注意,您不是initialize an admin app instance。这很可能是问题所在,但由于您没有共享任何日志或错误,我们无法确认。另一个问题是您错误地终止了 Cloud Function。

标签: javascript firebase google-cloud-functions


【解决方案1】:

您没有在问题中分享任何错误日志,因此很难为您提供帮助。但是,通过阅读您的代码,我们可以发现两个主要问题:

  1. 你没有initialize an admin app instance。你应该这样做admin.initializeApp();
  2. 您错误地终止了 Cloud Function:您需要等待所有异步操作完成,然后才能向 Cloud Functions 平台指示它可以关闭 Cloud Function 实例。更多详情请见doc。为此,由于您使用循环来调用多个异步更新,因此您需要使用Promise.all() 或批量写入。

以下应该有效:

const functions = require("firebase-functions"); 
const admin = require("firebase-admin");
admin.initializeApp();

exports.expirePassesEST = functions.pubsub
    .schedule("0 11 * * *")
    .timeZone("America/New_York")
    .onRun(async () => {
        // get all passes (passes & covers) that have a timezone offset of 240 (EST offset)
        const fastPassesSnapshot = await admin
            .firestore()
            .collection("fastpasses")
            .where("timezoneOffset", "===", 240)
            .get();

        const coversSnapshot = await admin
            .firestore()
            .collection("covers")
            .where("timezoneOffset", "==", 240)
            .get();

        const promises = [];

        fastPassesSnapshot.forEach((fastPass) =>
            promises.push(fastPass.ref.update({ status: "expired", expiredTimestamp: new Date() }))
        );

        coversSnapshot.forEach((cover) =>
            promises.push(cover.ref.update({ status: "expired", expiredTimestamp: new Date() }))
        );

        return Promise.all(promises);

    });

【讨论】:

  • 嘿@devon66h,您有时间查看建议的解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-27
  • 2020-10-20
  • 1970-01-01
  • 2023-01-11
相关资源
最近更新 更多