[英]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):
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命令。
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
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.