Firebase Functions V2中能否用onSchedule配置自定义运行时参数?
Firebase Functions V2 onSchedule 指定运行时参数的方法
当然可以在代码中直接指定timeoutSeconds、memory等运行时参数,无需手动去Google Cloud控制台配置。V2的语法和V1不同,不需要链式调用runWith,而是将运行时选项直接包含在onSchedule的配置参数中。
示例TypeScript代码
import { onSchedule } from "firebase-functions/v2/scheduler"; import { logger } from "firebase-functions"; // 假设你已经初始化了adminDb import { adminDb } from "./your-admin-db-path"; // 定义运行时选项 const runtimeOpts = { timeoutSeconds: 540, memory: "1GB" as const, }; exports.cleanupEvents = onSchedule( { schedule: "0 0 * * *", timeZone: "Europe/Berlin", // 合并运行时选项到调度配置中 ...runtimeOpts }, async () => { await cleanupOldEvents(adminDb); logger.log("Event cleanup finished"); } );
关键说明
onSchedule的第一个参数是配置对象,同时包含调度规则(schedule、timeZone)和运行时参数(memory、timeoutSeconds)- 也可以直接把运行时参数写在配置对象里,不用单独定义
runtimeOpts:exports.cleanupEvents = onSchedule( { schedule: "0 0 * * *", timeZone: "Europe/Berlin", timeoutSeconds: 540, memory: "1GB" as const, }, async () => { // 函数逻辑 } ); - 运行时参数的可选值和V1一致,比如
memory支持"128MB"、"256MB"、"512MB"、"1GB"、"2GB"等,timeoutSeconds最大为540(9分钟)
内容的提问来源于stack exchange,提问作者Sophia
相关产品推荐
相关产品推荐

