typescript - 如何使用Firebase模拟器pubsub在本地测试定时功能?

标签 typescript firebase google-cloud-functions localhost google-cloud-scheduler

我正在为项目使用Firebase,并且正在使用以下代码来创建计划功能。我想在运行的每一分钟记录一条消息。

export const timedQuery = functions.pubsub.schedule('1 * * * *').onRun((context) => {
console.log("I am running")
return null;
})

我具有在http函数下工作的代码的主要逻辑,并希望在部署到生产环境之前查看它是否在本地工作。通过Firebase文档,我已经下载了所有firebase模拟器,并使用“firebase emulators:start”使其运行。从日志中看来,我的pubsub仿真器已在localhost:8085成功启动,并且pubsub函数已初始化,但是即使等待了2-3分钟,也没有任何输出。是否可以在本地测试计划的功能?

另外,由于我只是在Firebase上,所以我没有使用Google Cloud Scheduler就创建了它。

最佳答案

Firebase本地仿真器当前不模拟实际的预定功能。 documentation说:

The Firebase CLI includes a Cloud Functions emulator which can emulate the following function types:

  • HTTPS functions
  • Callable functions
  • Cloud Firestore functions


我建议使用Firebase support提交功能请求。

部署预定功能时,实际上是在后台使用Google Cloud Scheduler。详细信息由您管理。如documentation中所述:

If you want to schedule functions to run at specified times, use functions.pubsub.schedule().onRun() This convenience method creates a Google Cloud Pub/Sub topic and uses Google Cloud Scheduler to trigger events on that topic, ensuring that your function runs on the desired schedule.



我建议将函数的代码重构为一种方法,您可以通过使用选择的测试框架直接调用它来进行测试。您也可以将其临时包装在HTTP函数中,然后以这种方式调用它。

关于typescript - 如何使用Firebase模拟器pubsub在本地测试定时功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61253788/

相关文章:

javascript - 如何在firebase.auth(js,ts)中更新用户电话号码

firebase - 检测到与预期实例不匹配的 "Timestamp"类型的对象

firebase - 当存储在firestore数据库中的日期是今天的日期时如何触发功能?

javascript - Angular8 firebase 配置错误 '@angular/fire/angularfire2'

Angular在调用函数时分配ngIf类 bool 属性以检查不同的数据类型

node.js - Nest 无法解析 PhotoService 的依赖项(?)

javascript - 从 javascript 中访问 firebaseUser 'created' 时间戳

json - 如何通过 firebase 托管将我的主页重定向到随机 url?

Android Firebase 在用户之间实现FCM(firebase cloud messaging)的步骤(user to user communication)

javascript - Web api - 使用 Gulp 任务动态设置端点地址