如何使用 pubsub 模拟器在本地调用 firebase Schedule 函数
How to invoke firebase Schedule functions locally using pubsub emulator
我正在研究云功能,尤其是计划功能。我需要每 5 分钟定期触发一个函数,但仅在测试步骤中。我需要在 pubsub 模拟器上 运行 它而不部署它。
怎么做?
我尝试使用 firebase shell,但只触发了一次
exports.scheduledFunctionPlainEnglish =functions.pubsub.schedule('every 2 minutes')
.onRun((context) => {
functions.logger.log("this runs every 2 minutes")
return null;
})
预定函数目前不支持此功能。 documentation 状态:
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.
计划函数是不受支持的 pubsub 触发器扩展。
如您所说,您可以使用 firebase shell 来 运行 您的函数一次。
在 firebase shell 中,您可以使用 NodeJS 命令。
使用 setInterval
在 firebase functions:shell
中,每 2 分钟使用 setInterval
到 运行 您的函数。
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.
在 Bash 中,您甚至可以将 setInterval
命令通过管道传输到 firebase shell
user@laptop:~$ echo "setInterval(() => myScheduledFunction(), 120000)" | firebase functions:shell
计划的函数加载到 Cloud Functions 模拟器运行时并绑定到 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):
我正在研究云功能,尤其是计划功能。我需要每 5 分钟定期触发一个函数,但仅在测试步骤中。我需要在 pubsub 模拟器上 运行 它而不部署它。
怎么做?
我尝试使用 firebase shell,但只触发了一次
exports.scheduledFunctionPlainEnglish =functions.pubsub.schedule('every 2 minutes')
.onRun((context) => {
functions.logger.log("this runs every 2 minutes")
return null;
})
预定函数目前不支持此功能。 documentation 状态:
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.
计划函数是不受支持的 pubsub 触发器扩展。
如您所说,您可以使用 firebase shell 来 运行 您的函数一次。 在 firebase shell 中,您可以使用 NodeJS 命令。
使用 setInterval
在 firebase functions:shell
中,每 2 分钟使用 setInterval
到 运行 您的函数。
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.
在 Bash 中,您甚至可以将 setInterval
命令通过管道传输到 firebase shell
user@laptop:~$ echo "setInterval(() => myScheduledFunction(), 120000)" | firebase functions:shell
计划的函数加载到 Cloud Functions 模拟器运行时并绑定到 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):