Firebase Functions部署8秒间隔pubsub定时函数报错问题
Firebase Pub/Sub 定时函数秒级配置部署失败问题解答
结论:Firebase Pub/Sub 定时触发器不支持秒级粒度的调度配置,最小支持的调度间隔为1分钟,这是配置every 8 seconds时部署报错的核心原因。
- 该定时能力底层依赖Google Cloud Scheduler服务,无论用自然语言格式还是标准cron表达式配置规则,都不支持小于1分钟的调度间隔。提交秒级周期配置时,调度规则创建接口会直接校验不通过返回错误,日志中
Failed to upsert schedule function的报错,就是调度规则创建失败的直接表现,和代码业务逻辑、区域选择、账号权限没有关系。 - 将调度周期修改为
every 8 hours时,间隔在平台支持的范围内,因此可以正常部署无报错。
问题代码片段
const otherMatchBotModule = require("./otherMatchBotActive"); exports.otherMatchBotActive = functions.pubsub.schedule("every 8 seconds").onRun(otherMatchBotModule.otherMatchBot);
部署报错核心日志
i functions: creating Node.js 14 function otherMatchBotActive(us-central1)... Functions deploy had errors with the following functions: otherMatchBotActive(us-central1) i functions: cleaning up build files... Error: There was an error deploying functions
[debug] [2022-06-28T01:36:49.114Z] Error: Failed to upsert schedule function otherMatchBotActive in region us-central1 at /Users/durak/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:38:11 at processTicksAndRejections (internal/process/task_queues.js:97:5) at async Fabricator.upsertScheduleV1 (/Users/durak/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:378:9) at async Fabricator.setTrigger (/Users/durak/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:347:17) at async Fabricator.createEndpoint (/Users/durak/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:124:9) at async handle (/Users/durak/.cache/firebase/tools/lib/node_modules/firebase-tools/lib/deploy/functions/release/fabricator.js:75:17) [error] [error] Error: There was an error deploying functions
秒级触发需求的替代实现方案
如果业务确实需要8秒间隔的高频执行,不要使用Pub/Sub定时触发器,可选择以下两种成熟方案:
- 方案1:部署一个配置最小常驻实例数为1的云函数/Cloud Run服务,在服务内部通过
setInterval实现8秒间隔的逻辑执行,注意做好异常捕获、进程重启后的任务恢复处理,避免漏执行。 - 方案2:配置最小粒度1分钟的定时触发器,函数被触发后内部循环执行任务,每轮间隔8秒,累计执行满1分钟后退出等待下一次调度,该方案不需要常驻实例,使用成本更低。
内容的提问来源于stack exchange,提问作者ursan526
相关产品推荐
相关产品推荐

