我正在为项目使用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/