Google Cloud Functions Cron作业无法正常工作

我正在尝试在Firebase Cloud Functions中设置scheduled function。作为一个简单的测试,我试图重新创建文档页面上显示的示例:

const functions = require('firebase-functions')

exports.scheduledFunction = functions.pubsub
  .schedule('every 5 minutes')
  .onRun(context => {
    console.log('This will be run every 5 minutes!')
    return null
  })

但是,当我运行firebase serve --only functions时,出现以下错误:

function ignored because the pubsub emulator does not exist or is not running.

知道为什么收到此消息以及如何解决该消息吗?

snoopy717 回答:Google Cloud Functions Cron作业无法正常工作

摘自Firebase's local emulator上的文档:

  

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

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

因此,本地Firebase模拟器当前不支持pubsub,并且错误消息似乎可以确认这一点。因此,目前,您无法在本地运行pubsub触发的Cloud Functions。

已提交adding PubSub support to the emulator的功能请求。您可能想在那里阅读(并可能发表评论),因为所选择的方向可能与您的需求不符。

本地外壳程序确实支持invoking pubsub functions。这当然是完全不同的,但是暂时可以作为解决方法。

,

对于它的价值,您需要在firebase中启用pubsub仿真器。将此添加到您的模拟器块:

{
  "emulators": {
    "pubsub": {
      "port": 8085
    },}
}

即使那样,它也仅创建定义。模拟器不支持按计划运行该功能。

为了模拟这种行为,我定义了一个HTTP触发器,在其中我手动向该主题发送消息。对于计划主题,它是 firebase-schedule- 。在您的情况下,它将是 firebase-schedule-scheduledFunction

示例代码如下:

const pubsub = new PubSub()

export const triggerWork = functions.https.onRequest(async (request,response) => {
  await pubsub.topic('firebase-schedule-scheduledFunction').publishJSON({})
  response.send('Ok')
})

然后在命令行上,按计划触发HTTP功能。

while [ 1 ];
  do wget -o /dev/null -O /dev/null http://localhost:5001/path/to/function/triggerWork;
  sleep 300;
done
本文链接:https://www.f2er.com/2917721.html

大家都在问