Firebase 的云函数按时触发? [英] Cloud Functions for Firebase trigger on time?

查看:29
本文介绍了Firebase 的云函数按时触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来为 Firebase 安排 Cloud Functions,或者换句话说,在特定时间触发它们.

解决方案

更新2019-04-18

现在有一种非常简单的方法可以通过 Firebase 在 Cloud Functions 上部署计划代码.

您可以使用简单的文本语法:

export scheduleFunctionPlainEnglish =functions.pubsub.schedule('每 5 分钟').onRun((context) => {console.log('这将每 5 分钟运行一次!');})

或者更灵活的 cron 表格式:

export scheduleFunctionCrontab =functions.pubsub.schedule('5 11 * * *').onRun((context) => {console.log('这将在每天 11:05 AM UTC 运行!');});

要了解更多信息,请参阅:

  • 我还强烈建议您阅读 Firebase 博客上的这篇文章:如何使用 Cloud Functions for Firebase 安排 (Cron) 作业 和此视频:使用 HTTP 触发器和 Cron 为 Firebase 计时云函数.

    最后一个链接使用 cron-job.org 来触发 Cloud Functions,适用于正在进行的项目免费计划.请注意,这允许任何人在未经授权的情况下调用您的函数,因此您可能希望在代码本身中包含一些滥用保护机制.

    I am looking for a way to schedule Cloud Functions for Firebase or in other words trigger them on a specific time.

    解决方案

    Update 2019-04-18

    There is now a very simple way to deploy scheduled code on Cloud Functions through Firebase.

    You can either use a simple text syntax:

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

    Or the more flexible cron table format:

    export scheduledFunctionCrontab =
    functions.pubsub.schedule('5 11 * * *').onRun((context) => {
        console.log('This will be run every day at 11:05 AM UTC!');
    });
    

    To learn more about this, see:

    Note that your project needs to be on a Blaze plan for this to work, so I'm leaving the alternative options below for reference.

    If you want to schedule a single invocation of a Cloud Function on a delay from within the execution of another trigger, you can use Cloud Tasks to set that up. Read this article for an extended example of how that can work.

    Original answer below...


    There is no built-in runat/cron type trigger yet.

    For the moment, the best option is to use an external service to trigger a HTTP function periodically. See this sample in the functions-samples repo for more information. Or use the recently introduced Google Cloud Scheduler to trigger Cloud Functions through PubSub or HTTPS:

    I also highly recommend reading this post on the Firebase blog: How to Schedule (Cron) Jobs with Cloud Functions for Firebase and this video: Timing Cloud Functions for Firebase using an HTTP Trigger and Cron.

    That last link uses cron-job.org to trigger Cloud Functions, and works for projects that are on a free plan. Note that this allows anyone to call your function without authorization, so you may want to include some abuse protection mechanism in the code itself.

    这篇关于Firebase 的云函数按时触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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