# 定时触发

If a cloud function needs to be executed periodically/periodically, that is, a timed trigger, you can use a cloud function timed trigger. Cloud functions configured with timed triggers will be automatically triggered at the corresponding time point, and the return result of the function will not be returned to the caller.

使用定时触发可以执行一些跑批任务,支付宝小程序定时触发最大超时时间为3小时,阿里云可以在使用定时触发时将云函数最高超时时间设置为600秒,腾讯云定时触发最大超时时间为900秒。

In the uniCloud web console, click the details of the cloud function that needs to add a trigger to create a cloud function trigger. The format is as follows:

腾讯云&支付宝小程序云

// The parameter is an array of triggers, currently only one trigger is supported, that is, only one array can be filled, and multiple cannot be added
// Be sure to remove the comment when actually adding
[
  {
    // name: The name of the trigger, see the rules below
    "name": "myTrigger",
    // type: trigger type, currently only supports timer (ie timing trigger)
    "type": "timer",
    // config: Trigger configuration, under the timing trigger, the config format is cron expression, the rules are described below
    "config": "0 0 2 1 * * *"
  }
]

Ali Cloud

["cron:0 0 * * * *"]

在package.json内配置定时触发时统一了云厂商的写法,请参考:云函数package.json

注意

  • 阿里云正式版最低触发间隔为1分钟,支付宝小程序云与腾讯云最低触发间隔为1秒
  • 支付宝小程序云与阿里云的cron表达式为6位,腾讯云为7位。相比腾讯云,支付宝小程序云与阿里云缺少代表年份的第7位
  • The timing trigger uses the time of utc+8
  • 定时执行的时间选在较为常见集中的时刻有极低概率出现执行失败的情况。建议避免整点(特别是0点),错开定时触发高峰期进行执行
  • 目前阿里云定时任务在运行时长超出配置的时间时会重试3次,此行为后续可能会调整。建议开发者将开启定时触发的云函数超时时间配置为600秒

# 字段规则

  • Timing trigger name (name): supports up to 60 characters, supports a-z, A-Z, 0-9, - and _. Must start with a letter, and multiple timing triggers with the same name are not supported under one function.
  • Timing trigger trigger cycle (config): The specified function trigger time. Fill in custom standard Cron expressions to decide when to trigger the function. For more information on Cron expressions, please refer to the following.

# Cron 表达式

Cron expressions have seven required fields, separated by spaces. Among them, each field has a corresponding value range:

sort field value wildcard
first bit seconds integer 0-59 ,-*/
second place minute integer 0 - 59 , - * /
third digit hour integer 0 - 23 , - * /
4th digit day Integer from 1 to 31 (need to consider the number of days in the month) , - * /
Fifth digit month integer from 1 to 12 , - * /
第六位 星期 0 - 6的整数,其中0指星期日,1指星期一,以此类推 , - * /
第七位 1970 - 2099的整数(阿里云和支付宝小程序云不支持第七位) , - * /

# 通配符

Wildcard Meaning
, (comma) represents the union of characters separated by commas. For example: in the "hour" field 1, 2, 3 means 1 o'clock, 2 o'clock and 3 o'clock
- (dash) Contains all values in the specified range. Example: In the "Day" field, 1 - 15 contains the 1st to the 15th of the specified month
* (asterisk) means all values. In the Hours field, * means every hour
/ (forward slash) Specifies an increment. In the Minutes field, enter 1/10 to specify that it repeat every ten minutes starting with the first minute. For example, the 11th minute, the 21st minute, and the 31st minute, and so on. Values are required before and after the forward slash, which cannot be omitted
  • Tencent Cloud: When the "day" and "week" fields in the cron expression specify values at the same time, the two are in an "or" relationship, that is, the conditions of both take effect.
  • Alibaba Cloud: An error will be reported when the "day" and "week" fields in the cron expression are specified at the same time, and "second" does not support special characters.

# 辅助工具

# 示例

Here are some Cron expressions and their associated meanings:

示例 说明
* * * * * * * 每1秒触发一次(阿里云不支持,阿里云最短1分钟触发一次)
*/5 * * * * * * 每5秒触发一次(阿里云不支持,阿里云最短1分钟触发一次)
0 * * * * * * 每1分钟触发一次
0 */10 * * * * * 每10分钟触发一次
0 0 * * * * * 每1小时触发一次(整点触发)
0 20 * * * * * 每1小时触发一次(每小时的20分触发,如08:20:00、09:20:00)
0 0 */2 * * * * 每2小时触发一次(整点触发)
0 0 18 * * * * 每天的下午6点触发一次(整点触发,18:00:00)
0 0 10,14,16 * * * * 每天上午10点,下午2点,4点触发(整点触发,10:00:00、14:00:00、16:00:00)
0 */30 9-17 * * * * 每天上午9点到下午5点内每半小时触发
0 0 2 1 * * * 每月的1日的凌晨2点触发
0 15 10 * * 1-5 * 周一到周五每天上午10:15触发

# 云函数、云对象入参说明

Cloud functions receive specific parameters when they are called using timed triggers. The parameters for the two platforms are as follows:

腾讯云、支付宝小程序云

{	
	"Time":"2020-04-08T10:22:31Z", //调用的云函数的时间
	"TriggerName":"myTrigger", //触发器名
	"Type":"Timer" //触发器类型,目前只有Timer
}

Ali Cloud

{
  "triggerName": "TIMER_LATEST", //触发云函数的定时器配置内容,注意阿里云不会使用package.json内配置的触发器名称
  "triggerTime": "2020-04-08T10:22:31Z", //触发云函数时的时间戳,可能略晚于cron表达式时间
  // 以下三个属性新增于2023年7月14日
  "Time":"2020-04-08T10:22:31Z", //调用的云函数的时间
  "TriggerName":"TIMER_LATEST", //触发器名
  "Type":"Timer" //触发器类型,目前只有Timer
}

2023年7月14日起阿里云入参对齐腾讯云,保留上述triggerNametriggerTime(不再推荐使用这两个属性),增加TimeTriggerNameType

# Cloud objects use timing trigger

Added in HBuilderX 3.5.2

2023年7月14日起_timing方法可以获取定时触发参数

The configuration method is the same as that of cloud functions, please refer to the above chapter

After the configuration is completed, the built-in special method _timing of the cloud object will be triggered periodically

Cloud object code example:

module.exports = {
	_timing: function (param) {
    console.log('触发时间:', param.Time)
		console.log('triggered by timing')
	}
}

Notice

  • 定时触发云对象时,_before_after均不执行