定时任务触发器用于定时执行一个函数。腾讯云 Timer 触发器目前只支持 cron 格式。
:::info 温馨提醒,测试函数后请及时关闭触发器自动执行,避免超额扣费。 :::
使用方式
在 f.yml 中配置函数和触发器。
service:name: midway-faas-examplesprovider:name: tencentfunctions:cronTimerTrigger:handler: cron.handlerevents:- timer:type: cronvalue: '*/5 * * * *'payload: 'test'package:artifact: code.zip
在 f deploy 后,即可。
开发支持
针对 Timer 触发器,我们提供了传入的事件类型定义。
import { FaaSContext, FC } from '@midwayjs/faas';import { Func, Inject, Provide } from '@midwayjs/decorator';@Provide()export class TimerTriggerTest {@Inject()ctx: FaaSContext; // context@Func('cron.handler')async cronHandler(event: SCF.TimerEvent) {// TODO}}
本地开发
使用 f invoke 命令进行触发。
f invoke -f [你的函数名]
本地测试
这里没有传 data,会默认通过 f.yml 获取触发器类型传入模拟数据。
// testdescribe('/test/index.test.ts', () => {it('invoke', async () => {await invoke({functionName: 'cronTimerTrigger',});});});
