Firebase的Cloud功能是否按时触发?

ahs*_*san 87 firebase google-cloud-platform google-cloud-functions

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

Fra*_*len 97

目前还没有内置的runat/cron类型触发器.

目前,最好的选择是使用外部服务定期触发HTTP功能.有关详细信息,请参阅functions-samples repo中的此示例.或者使用最近推出的Google Cloud Scheduler通过PubSub或HTTPS触发云功能:

在此输入图像描述

我还强烈建议您阅读Firebase博客上的这篇文章:如何使用Cloudbase和此视频的云功能安排(Cron)工作:使用HTTP触发器和Cron为Firebase定时云功能.

  • 来自Jen的视频已被标记为已弃用.那还有另一种方法吗? (2认同)
  • 这种方法在今天和视频(和博客文章)制作时一样有效.语法略有改变,但我不认为这个具体情况会受到影响.如果您在完成这项工作时遇到问题,请打开一个显示您已完成工作的问题. (2认同)
  • 从 Cloud Scheduler 页面:“每个 Cloud Scheduler 作业每月花费 0.10 美元”,假设“作业”并不意味着每次计划的事物触发,而是指每个计时器的成本?即每分钟运行的作业只需花费 0.10 美元?(不包括它调用的任何云函数)。 (2认同)

mha*_*ski 11

您可以做的是,启动由cron作业触发的AppEngine实例并发送到PubSub.我特意写了一篇博文,你可能想看看:

https://mhaligowski.github.io/blog/2017/05/25/scheduled-cloud-function-execution.html

  • @EhteshamHasan看起来可能是免费的:https://cloud.google.com/free/。目前每天有28个实例小时免费;此外,还有带有Linux的Google Compute Engine的f1-micro实例,其中有运行免费atm的crons的Linux。 (3认同)

Kag*_*oka 5

首先需要注意的是,根据文档,您的函数将执行的默认时区是标准UCT。如果您想在不同时区触发函数,您可以在此处找到时区列表。

注意!:这是一个有用的网站,可以帮助设置cron 表格式(我发现它非常有用)

以下是您的操作方法:(假设您想使用非洲/约翰内斯堡作为时区)

对于第二代(v2) 函数:

import { onSchedule } from "firebase-functions/v2/scheduler";

export const executeFunction = onSchedule({ schedule: '10 23 * * *', timeZone: 'Africa/Johannesburg' }, () => {
    //your function logic goes here
    console.log("successfully executed at 23:10 Johannesburg Time!");
});
Run Code Online (Sandbox Code Playgroud)

对于第一代函数:

export const executeFunction = functions.pubsub.schedule("10 23 * * *")
    .timeZone('Africa/Johannesburg').onRun(() => { 
       console.log("successfully executed at 23:10 Johannesburg Time!");
});
Run Code Online (Sandbox Code Playgroud)

如果您想坚持使用默认时区,只需省略时区即可:

第二代

import { onSchedule } from "firebase-functions/v2/scheduler";

export const executeFunction = onSchedule({ schedule: '10 23 * * *' }, () => {
    //your function logic goes here
    console.log("successfully executed at 23:10 UCT - Coordinated Universal Time!");
});
Run Code Online (Sandbox Code Playgroud)

第一代

export const executeFunction = functions.pubsub.schedule("10 23 * * *")
    .onRun(() => { 
       console.log("successfully executed at 23:10 UCT - Coordinated Universal Time!");
});
Run Code Online (Sandbox Code Playgroud)