如何使用Firebase模拟器pubsub在本地测试定时功能? [英] How to use firebase emulators pubsub to test timed functions locally?

查看:58
本文介绍了如何使用Firebase模拟器pubsub在本地测试定时功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using firebase for a project and am working on creating a schedule function using the following code. I want to log a message every minute this runs.

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分钟,也没有任何输出.是否可以在本地测试计划的功能?

I have the main logic of the code working under an http function and would like to see if this works locally before deploying to production. Going through firebase docs I've downloaded all of the firebase emulators and use "firebase emulators:start" to get them running. From the logs it looks like my pubsub emulator starts successfully at localhost:8085 and pubsub function is initialized however even after waiting for 2 - 3 minutes nothing prints out. Is it possible to test scheduled functions locally?

我也是在Firebase上,因此我没有使用Google Cloud Scheduler就创建了它.

Also I created this without using google cloud scheduler since I'm only on firebase.

推荐答案

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

The Firebase local emulator currently doesn't simulate the actual scheduled functions. The documentation says:

Firebase CLI包括一个可以模拟的Cloud Functions模拟器以下功能类型:

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

  • HTTPS函数
  • 可调用函数
  • Cloud Firestore功能

我建议向 Firebase支持提交功能请求.

I suggest filing a feature request with Firebase support.

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

When you deploy a scheduled function, you are actually using Google Cloud Scheduler behind the scenes. The details are managed for you. As stated in the documentation:

如果您要安排函数在指定时间运行,请使用functions.pubsub.schedule().onRun()这种便捷的方法可创建Google Cloud Pub/Sub主题,并使用Google Cloud Scheduler触发有关该主题的事件,确保您的功能按预期的时间表运行.

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函数中,然后以这种方式调用.

I suggest refactoring your function's code into a method that you can test by invoking it directly using a test framework of your choice. You could also temporarily wrap it in an HTTP function and invoke it that way.

这篇关于如何使用Firebase模拟器pubsub在本地测试定时功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆