Oracle-Sentry/docs/cn/script/groovy/注册并使用触发器.md

24 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 注册并使用触发器
触发器可以在适当的时候执行脚本所注册的运行代码,使用触发器,哨兵可以在某项事件发生时,触发脚本执行某些操作,而无需脚本手动检查。
### 注册触发器
注册触发器的方法非常简单:
```groovy
trigger("timer") {
// timer 触发器的参数 "time", 填写 Cron 时间表达式.
time "0 0 12 * * ? *"
run {
// 这里编写当到达时间时所需执行的动作.
}
}
```
其中,`"timer"`是触发器名称,`time` 是触发器参数,不同的触发器有不同的参数,也可能没有参数(比如 Once 触发器),`run` 代码块是每个触发器必须有的当满足条件时run 代码块将会被执行。
run 代码块有个隐藏参数 `it`,如果触发器有参数,将通过该参数传递至 run 代码块,供脚本使用。
> 注意:每个触发器所能提供的东西并不一样,具体信息见[触发器文档](可用的触发器.md)。
当注册好了触发器后,只需要等待触发器,在合适的时机触发执行任务即可!