简体   繁体   English

如何使用 pubsub 模拟器在本地调用 firebase 计划函数

[英]How to invoke firebase Schedule functions locally using pubsub emulator

I am working on cloud functions especially schedule functions.我正在研究云功能,尤其是计划功能。 I need to trigger a function periodically each 5 minutes, but in only test step.我需要每 5 分钟定期触发 function,但仅在测试步骤中。 I need to run it on pubsub emulator without deploying it.我需要在 pubsub 模拟器上运行它而不部署它。

How to do it?怎么做?

I tried to use firebase shell, but it triggered only once我试过用firebase shell,但是只触发了一次

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

Scheduled functions are loaded to the Cloud Functions emulator runtime and are bound to the PubSub emulator topic.计划函数加载到 Cloud Functions 模拟器运行时并绑定到 PubSub 模拟器主题。

But as @samstern said ( https://github.com/firebase/firebase-tools/issues/2034 ):但正如@samstern 所说( https://github.com/firebase/firebase-tools/issues/2034 ):

you'd have to manually trigger them using a Pub/Sub message.您必须使用 Pub/Sub 消息手动触发它们。

You can do it like this:你可以这样做:

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

Additional info about this concept (thanks to @kthaas):关于这个概念的附加信息(感谢@kthaas):

  1. https://github.com/firebase/firebase-tools/pull/2011/files#diff-6b2a373d8dc24c4074ee623d433662831cadc7c178373fb957c06bc12c44ba7b https://github.com/firebase/firebase-tools/pull/2011/files#diff-6b2a373d8dc24c4074ee623d433662831cadc7c178373fb957c06bc12c44ba7b
  2. https://github.com/firebase/firebase-tools/pull/2011/files#diff-73f0f0ab73ffbf988f109e0a4c8b3b8a793f30ef33929928a892d605f0f0cc1f https://github.com/firebase/firebase-tools/pull/2011/files#diff-73f0f0ab73ffbf988f109e0a4c8b3b8a793f30ef33929928a892d605f0f0cc1f

As you said, you can use firebase shell to run your function once.正如您所说,您可以使用 firebase shell 来运行您的 function 一次。 And in firebase shell, you can use NodeJS commands.而在firebase shell中,可以使用NodeJS命令。

Use setInterval使用 setInterval

Inside firebase functions:shell , use setInterval to run your function every 2 minutes.firebase functions:shell ,使用setInterval每 2 分钟运行一次 function。

user@laptop:~$ firebase functions:shell

✔  functions: functions emulator started at http://localhost:5000
i  functions: Loaded functions: myScheduledFunction
firebase > setInterval(() => myScheduledFunction(), 120000)

> this runs every 2 minutes

Single line script单行脚本

Since version 8.4.3 of firebase-tools, and especially this PR , the pipe solution does not work anymore.由于 firebase-tools 的 8.4.3 版本,尤其是这个 PR ,pipe 解决方案不再起作用。

In Bash, you can even pipe the setInterval command to firebase shell在 Bash 中,您甚至可以在 pipe 中将setInterval命令设置为 firebase Z2591C98B1B1119FE62EB4289

user@laptop:~$ echo "setInterval(() => myScheduledFunction(), 120000)" | firebase functions:shell

For those of you seeing this in 2023, it's still not supported.对于那些在 2023 年看到这一点的人来说,它仍然不受支持。

My solution was to abstract the code that does the "work" out of functions.pubsub.schedule and into their own functions.我的解决方案是将执行“工作”的代码从functions.pubsub.schedule中抽象出来并放入它们自己的函数中。 Then create a separate file (i added it at the top of the functions folder) with a setInterval inside it that fires the aforementioned abstracted function.然后创建一个单独的文件(我将其添加到函数文件夹的顶部),其中包含一个setInterval ,用于触发上述抽象的 function。

For example, somewhere in your code:例如,在您的代码中的某处:

exports.myScheduledFunctionCode = () => {
  console.log('why, hello there interval');
}

And in the timers.js (for example) file at the top of the /functions directory:/functions目录顶部的timers.js (例如)文件中:

setInterval(() => {
  myScheduledFunctionCode();
}, 60000);

Then, you can fire up your Firebase Emulator suite.然后,您可以启动 Firebase 模拟器套件。 In another Terminal session, just run a vanilla $ node functions/timers.js .在另一个终端 session 中,只需运行 vanilla $ node functions/timers.js Now your scheduled function code is running, and your whole emulator suite too.现在您预定的 function 代码正在运行,您的整个模拟器套件也在运行。

Hope this helps someone!希望这对某人有帮助!

This is currently not supported for scheduled functions.计划功能目前不支持此功能。 The documentation states:文档指出:

Using the shell, you mock data and perform function calls to simulate interaction with products that the Emulator Suite does not currently support: Storage, PubSub , Analytics, Remote Config, Storage, Auth, and Crashlytics.使用 shell,您可以模拟数据并执行 function 调用以模拟与 Emulator Suite 当前不支持的产品的交互:存储、 PubSub 、分析、远程配置、存储、身份验证和 Crashlytics。

Scheduled functions are an unsupported extension of pubsub triggers.计划函数是 pubsub 触发器不受支持的扩展。

Feel free to file a feature request with Firebase support .随时向 Firebase 支持提出功能请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 安排 firebase 身份验证:使用 pubsub 导出到存储桶 - Schedule firebase auth:export to bucket using pubsub Firebase PubSub 模拟器未收到消息 - Firebase PubSub Emulator not recieving messages 如何让 Firebase 托管模拟器在本地为 Vite 开发服务器提供服务? - How to get Firebase hosting emulator to serve a Vite dev server locally? 使用“firebase init”安装 Firebase 功能模拟器后如何卸载它 - How to uninstall Firebase functions emulator after installing it with "firebase init" 使用测试容器 pubsub 模拟器的反序列化问题 - Deserialization issue using test container pubsub emulator Firebase 用户设置的功能时间表 - Firebase Functions Schedule Set by User 如何使用 WebStorm 和 Ngrok 在本地运行 Firebase Firestore Cloud Functions 并对其进行调试? - How to run Firebase Firestore Cloud Functions locally and debuging it, with WebStorm and Ngrok? 使用/不使用模拟器时如何获取 firebase 的当前 https 函数端点 - How to get the current https functions endpoint for firebase when using/not using emulator 在本地运行时出现 firebase 模拟器 UI 问题 - Issue with firebase emulator UI when run locally 在 firebase 云函数 flutter 中发送计划 email - Send schedule email in firebase cloud functions flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM