简体   繁体   English

制作无限期运行的云http函数

[英]Making a cloud http function that runs indefinitely

Answered in the comments by Chris G: Use setInterval instead, and remove the while loop;在 Chris G 的评论中回答:改为使用 setInterval,并删除 while 循环; this code will call clearData in rapid succession, infinitely.此代码将无限快速地连续调用 clearData。 (and a cronjob is definitely preferable to this setup, btw) (并且 cronjob 绝对比这个设置更可取,顺便说一句)

I need to make a http function that only calls one time (at launch) and after that runs every 5 minutes to check data and if that data is 2 hours old we clear it.我需要创建一个 http 函数,它只调用一次(在启动时),之后每 5 分钟运行一次以检查数据,如果该数据是 2 小时前的,我们将其清除。

A few things before i show the code:在我展示代码之前的一些事情:

  1. We know of google scheduler but are choosing not to use it.我们知道谷歌调度程序,但选择不使用它。
  2. We know we can use cron-jobs to call our html whenever we need but that would mean another service and my goal is to have the function run itself without any service like calling it.我们知道我们可以在需要时使用 cron-jobs 来调用我们的 html,但这意味着另一个服务,我的目标是让该函数自行运行,而无需任何服务,例如调用它。

Code:代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp(functions.config().firebase);

exports.methodCaller = functions.https.onRequest((request, response) => {
  setTimeout(() => {
    while (true) {
      clearData();
      console.log('methodCaller execution')
    }
  }, 30000); // currently 30 seconds for testing purposes but will be 5 hours later on
});

function dataChecked() {
  admin.database().ref('/logs/')
    .once("value")
    .then(snapshot => {
      console.log(snapshot.val());
      //code that checks data
    });
}
  1. Is my way of timing out the function correct.我超时功能的方式是否正确。 I want it to manually fire every 5 minutes checking some data.我希望它每 5 分钟手动触发一次检查一些数据。

  2. The http request wants a response but if i put it inside of the while(true) statement it only logs 2 times. http 请求需要响应,但如果我将它放在 while(true) 语句中,它只会记录 2 次。

  3. Is my code correct at all ?我的代码完全正确吗?

I need to make a http function that only calls one time (at launch) and after that runs every 5 minutes to check data and if that data is 2 hours old we clear it.我需要创建一个 http 函数,它只调用一次(在启动时),之后每 5 分钟运行一次以检查数据,如果该数据是 2 小时前的,我们将其清除。

For that you should use schedule functions, which are now supported under Firebase.为此,您应该使用 Firebase 现在支持的 schedule 函数。 See the blog post Scheduling Cloud Functions for Firebase (cron) , which contains this very relevant example for your use-case:请参阅博客文章Scheduling Cloud Functions for Firebase (cron) ,其中包含与您的用例非常相关的示例:

 export scheduledFunctionPlainEnglish = functions.pubsub.schedule('every 5 minutes').onRun((context) => { console.log('This will be run every 5 minutes!'); });

The way you're now trying to implement the use-case won't work.您现在尝试实现用例的方式行不通。 Cloud Functions has a hard upper limit of 9 minutes before it kills a function instance. Cloud Functions 在杀死函数实例之前有 9 分钟的硬性上限。

What you are wanting to do is impossible, the maximum time you can set for a cloud function in general is 540 seconds(9 Minutes).您想要做的事情是不可能的,您可以为云功能设置的最长时间一般为 540 秒(9 分钟)。

I have one function that has that as the maximum timeout, but generally the timeout is 60 seconds.我有一个函数将其作为最大超时,但通常超时为 60 秒。 If you want to run something every 5 hours, just run it in a cron job, or set up a VPS and start the program there.如果你想每 5 小时运行一次,只需在 cron 作业中运行它,或者设置一个 VPS 并在那里启动程序。

Cloud functions is not what you are looking for for this, plus you do not want to have to pay for the compute time for this.云函数不是您想要的,而且您不想为此支付计算时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM