4 stackstorm定时器基础

4 定时器基础
定时器从本质上来说就是stackstorm中的rule。
规则对trigger触发器到action(或工作流)做了映射。应用匹配的criteria并且
映射trigger payloads到动作的输入。
说白了rules包含trigger,action,criteria(可以不需要)3个部分,来将事件
的触发映射到动作的执行。
trigger是前置条件,如果触发,则继续查看criteria(如果存在的化)条件是否匹配,
如果匹配,则触action。
rule的主要格式如下:
name: 规则名称
pack: 规则的属于方,如果没有指定pack则为default
description: 规则的描述
enabled: 规则是否开启(true或者false)
trigger:emitted from sensors to monitor, and optionally parameters associated with that trigger.
criteria:可选择的集合,包含
    一个trigger的payload的属性
    criteria比较的类型
    pattern: to match against.
action:当一个规则被满足时,执行的动作,包含:
    ref:动作或工作流的名称
    parameters: 执行动作的参数(可选)

管理规则
通过命令
st2 rule create ${PATH_TO_RULE}
来部署规则
例如:
st2 rule create /usr/share/doc/st2/examples/rules/sample_rule_with_webhook.yaml

加载所有规则,使用:
st2ctl reload --register-rules
自定义规则被放在:
/opt/stackstorm/packs/<pack_name>/rules
目录。
例如:
/opt/stackstorm/packs/examples/rules

2 编写一个规则文件
print_rule.yaml
内容如下:
--------------------------------------------------------------------------------
name: "print_rule"
pack: "default"
description: "print rule"
enabled: true

trigger:
    type: "core.st2.CronTimer"
    parameters:
        timezone: "Asia/Shanghai"
        day_of_week: ""hour: ""
        minute: "*"
        second: 0
    action:
        ref: "default.write_content_action"
        parameters:
        content: "first_rule"
注意:
这里action下面的ref一定是实际的action名称,
不要写错成action执行的脚本文件名
将规则文件放在对应目录下面
关键是规则存放在哪里,
最终存放在:
/opt/stackstorm/packs/default/rules
3 创建规则
st2 rule create /opt/stackstorm/packs/default/rules/write_rule.yaml
输出结果:
+---------------+----------------------------------------------------------+
| Property      | Value                                                    |
+---------------+----------------------------------------------------------+
| id            | 5c6559a59dc6d60729fc00ad                                 |
| name          | rule_write_content                                       |
| pack          | default                                                  |
| description   | write content rule                                       |
| action        | {                                                        |
|               |     "ref": "default.write_content.sh",                   |
|               |     "parameters": {                                      |
|               |         "content": "chaos_first_rule"                    |
|               |     }                                                    |
|               | }                                                        |
| context       | {                                                        |
|               |     "user": "st2admin"                                   |
|               | }                                                        |
| criteria      |                                                          |
| enabled       | True                                                     |
| metadata_file |                                                          |
| ref           | default.rule_write_content                               |
| tags          |                                                          |
| trigger       | {                                                        |
|               |     "type": "core.st2.CronTimer",                        |
|               |     "ref": "core.51b8a980-8c60-4898-baba-5c10c332e80c",  |
|               |     "parameters": {                                      |
|               |         "timezone": "Asia/Shanghai",                     |
|               |         "second": 0,                                     |
|               |         "minute": "",                                   ||               |         "hour": "",                                     |
|               |         "day_of_week": "*"                               |
|               |     }                                                    |
|               | }                                                        |
| type          | {                                                        |
|               |     "ref": "standard",                                   |
|               |     "parameters": {}                                     |
|               | }                                                        |
| uid           | rule:default:rule_write_content                          |
+---------------+----------------------------------------------------------+

4 创建规则中指定的动作
4.1 先定义action的metadata文件
具体是创建一个文件名字为:
write_content_metadata.yaml
具体内容如下:
name: "write_content_action"
description: "write content"
runner_type: "python-script"
enabled: true
entry_point: "write_content.py"
parameters:
    content:
        type: "string"
        description: "content parameter"
        required: true
        position: 0

4.2 创建action对应的脚本文件
文件名字如下: write_content.py
具体内容如下:
from datetime import datetime
from st2common.runners.base_action import Action
class CounterAction(Action):
# append content into file
def run(self, content):
    fileName = "/home/counter.txt"
    try:
        currTime = str(datetime.now())
        # note, a+ means append instead of w+
        with open(fileName, "a+") as fr:
            realContent = "content: "  + str(content) + ", time: " + currTime + "\n"
            fr.write(realContent)
        return (True, content)
    except Exception as ex:
        return (False, content)

4.3 将action脚本文件write_content.py拷贝到目录
/opt/stackstorm/packs/default/actions
4.4 创建action
执行如下命令: st2 action create write_content_metadata.yaml
创建动作后的输出结果:
+---------------+--------------------------------------------+
| Property      | Value                                      |
+---------------+--------------------------------------------+
| id            | 5c655abf9dc6d60729fc00ae                   |
| name          | write_content_action                       |
| pack          | default                                    |
| description   | write content                              |
| enabled       | True                                       |
| entry_point   | write_content.py                           |
| metadata_file |                                            |
| notify        |                                            |
| output_schema |                                            |
| parameters    | {                                          |
|               |     "content": {                           |
|               |         "position": 0,                     |
|               |         "required": true,                  |
|               |         "type": "string",                  |
|               |         "description": "content parameter" |
|               |     }                                      |
|               | }                                          |
| ref           | default.write_content_action               |
| runner_type   | python-script                         |
| tags          |                                            |
| uid           | action:default:write_content_action        |
+---------------+--------------------------------------------+
5 检测定时器是否生效
根据步骤4.2中会生成/home/counter.txt,如果看到该文件,并且文件内容类似如下:
content: first_rule, time: 2018-02-04 22:26:22.712110
说明定时器生效。
6 总结
编写规则的完整过程
1 编写规则,主要包含两部分:
触发器和动作。
动作需要提供:动作元数据文件用于动作注册,还需要提供具体执行的动作脚本,将该动作执行脚本放到/opt/stackstorm/packs/<pack_name>/actions目录下。
先注册动作,然后可以执行: st2 action <action_name> 来手动先验证一下动作本身是否正确
然后注册规则。
然后可以打开: /var/log/st2rulesengine.log,查看规则校验结果,如果校验成功,
然后打开: /var/log/st2actionrunner.<xxx>.log 查看动作具体执行情况。

 

posted @ 2022-08-04 00:10  Sky-wings  阅读(153)  评论(0编辑  收藏  举报