全部产品
Search
文档中心

运维编排服务:ACS::WaitFor

更新时间:Mar 07, 2022

用途

使用该动作可以等待资源进入预期的状态。通过调用云产品的OpenAPI去查询指定资源的状态,直到资源的状态和预期的一致。如通过调用ECS的OpenAPIRunInstances创建资源后,可以通过调用ACS::WaitFor等待实例被创建完成,即其状态为运行中(Running)。

语法

  • YAML格式:

    Tasks:
      - Name: waitForTaskExample
        Action: ACS::WaitFor
        Properties: 
          # 以下属性和ACS::ExecuteAPI一致,请参考ACS::ExecuteAPI语法说明。
          Service: ECS
          API: DescribeInstances
          Parameters: 
            InstanceId: i-12345678abcedfg
          # 以下属性为ACS::WaitFor的特殊属性
          PropertySelector: "jq selector" # 必填,需要等待的具体的API返回值,参考ACS::ExecuteAPI中的ValueSelector说明
          StopRetryValues: # 可选,一旦任务PropertySelector结果为status1状态时终止任务等待及API重试。比如status1为Stopped时,一旦API返回结果筛选后为Stopped,则该任务执行停止。
            - status1
          DesiredValues: # DesiredValue,NotDesiredValues 必填其一,指上面截取的值要匹配到以下值之一,匹配则表示成功,否则会超时然后失败。
            - value1
            - value2
          NotDesiredValues: # DesiredValue,NotDesiredValues 必填其一,指上面截取的值要不匹配到以下的所有值,全都不匹配则表示成功,否则会超时然后失败。
            - value3
            - value4
        Retries: #可选,表示最大重试次数,填写正整数,如5。
        DelayType: #Exponential(默认),Constant,Linear。Exponential类型的重试间隔为:2 ^ times(重试次数);Constant类型的重试间隔固定为Delay;Linear类型的重试间隔为:Delay + BackOff * times(重试次数)。
        Delay: #DelayType为Constant或Linear时需要添加此属性,表示重试间隔时间,填写正整数,如10,表示重试间隔为10s,
        Backoff: #DelayType为Linear时需要添加此属性,表示重试间隔补偿时间,填写正整数。
        Outputs:  #WaitFor动作所在的任务支持输出,当任务满足NotDesiredValues/DesiredValues的条件时,任务执行成功后会返回输出参数的。
          ValueSelector: # jq的选择器语法,以OpenAPI的返回作为JSON输入,jq的语法请参考 https://stedolan.github.io/jq
          Type: String/Boolean/List(Array)/Number/Object
  • J常SON格式(请参考YAML注释说明):

    {
      "Tasks": [
        {
          "Name": "waitForTaskExample",
          "Action": "ACS::WaitFor",
          "Properties": {
            "Service": "ECS",
            "API": "DescribeInstances",
            "Parameters": {
              "InstanceId": "i-12345678abcedfg"
            },
            "PropertySelector": "jq selector",
            "StopRetryValues":["status1"],
            "DesiredValues": [
              "value1",
              "value2"
            ],
            "NotDesiredValues": [
              "value3",
              "value4"
            ]
          },
        "Outputs": {
            "OutputParameter1": {
              "ValueSelector": "jq selector",
              "Type": "String/Boolean/List(Array)/Number/Object"
            }
          }
        }
      ]
    }

示例

如下模板:启动一台指定实例,通过调用WaitFor等待实例的状态变为Running。

  • YAML格式:

    ---
    FormatVersion: OOS-2019-06-01
    Parameters:
      InstanceId:
        Description: the instance id that you will start.
        Type: String
    Tasks:
    - Name: startInstance
      Action: ACS::ExecuteAPI
      Description: start instance with specified parameters
      Properties:
        Service: ECS
        API: StartInstance
        Parameters:
          InstanceId: '{{ InstanceId }}'
    - Name: untilInstanceReady
      Action: ACS::WaitFor
      Description: describe instances with specified parameters
      Properties:
        Service: ECS
        API: DescribeInstances
        Parameters:
          InstanceIds:
          - '{{ InstanceId }}'
        PropertySelector: Instances.Instance[].Status
        DesiredValues:
        - Running
  • JSON格式:

    {
        "FormatVersion": "OOS-2019-06-01",
        "Parameters": {
            "InstanceId": {
                "Description": "the instance id that you will start.",
                "Type": "String"
            }
        },
        "Tasks": [
            {
                "Name": "startInstance",
                "Action": "ACS::ExecuteAPI",
                "Description": "start instance with specified parameters",
                "Properties": {
                    "Service": "ECS",
                    "API": "StartInstance",
                    "Parameters": {
                        "InstanceId": "{{ InstanceId }}"
                    }
                }
            },
            {
                "Name": "untilInstanceReady",
                "Action": "ACS::WaitFor",
                "Description": "describe instances with specified parameters",
                "Properties": {
                    "Service": "ECS",
                    "API": "DescribeInstances",
                    "Parameters": {
                        "InstanceIds": [ "{{ InstanceId }}"]
                    },
                    "PropertySelector": "Instances.Instance[].Status",
                    "DesiredValues": [ "Running" ],
                }
            }
        ]
    }