English
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
注意
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.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 |
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日起
阿里云入参对齐腾讯云,保留上述triggerName
和triggerTime
(不再推荐使用这两个属性),增加Time
、TriggerName
、Type
。
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
均不执行