gpt4 book ai didi

javascript - 如何使用 pubsub 模拟器在本地调用 firebase Schedule 函数

转载 作者:行者123 更新时间:2023-12-04 00:55:34 25 4
gpt4 key购买 nike

我正在研究云功能,尤其是日程安排功能。我需要每 5 分钟定期触发一个功能,但仅在测试步骤中。我需要在 pubsub 模拟器上运行它而不部署它。
怎么做?
我尝试使用firebase shell,但它只触发了一次

 exports.scheduledFunctionPlainEnglish =functions.pubsub.schedule('every 2 minutes')
.onRun((context) => {
functions.logger.log("this runs every 2 minutes")
return null;
})

最佳答案

计划函数加载到 Cloud Functions 模拟器运行时并绑定(bind)到 PubSub 模拟器主题。
但正如@samstern 所说( https://github.com/firebase/firebase-tools/issues/2034 ):

you'd have to manually trigger them using a Pub/Sub message.


你可以这样做:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { PubSub } from '@google-cloud/pubsub';

if (!admin.apps.length) {
admin.initializeApp();
}

const pubsub = new PubSub({
apiEndpoint: 'localhost:8085' // Change it to your PubSub emulator address and port
});

setInterval(() => {
const SCHEDULED_FUNCTION_TOPIC = 'firebase-schedule-yourFunctionName';
console.log(`Trigger sheduled function via PubSub topic: ${SCHEDULED_FUNCTION_TOPIC}`);
const msg = await pubsub.topic(SCHEDULED_FUNCTION_TOPIC).publishJSON({
foo: 'bar',
}, { attr1: 'value1' });
}, 5 * 60 * 1000); // every 5 minutes
关于这个概念的附加信息(感谢@kthaas):
  • https://github.com/firebase/firebase-tools/pull/2011/files#diff-6b2a373d8dc24c4074ee623d433662831cadc7c178373fb957c06bc12c44ba7b
  • https://github.com/firebase/firebase-tools/pull/2011/files#diff-73f0f0ab73ffbf988f109e0a4c8b3b8a793f30ef33929928a892d605f0f0cc1f
  • 关于javascript - 如何使用 pubsub 模拟器在本地调用 firebase Schedule 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62759093/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com