用途
使用該動作可以等待資源進入預期的狀態。通過調用雲產品的OpenAPI去查詢指定資源的狀態,直到資源的狀態和預期的一致。如通過調用ECS的OpenAPIRunInstances
建立資源後,可以通過調用ACS::WaitFor
等待執行個體被建立完成,即其狀態為運行中(Running)。
文法
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: #可選,表示最大重試次數,預設值10,填寫正整數,如5,取值範圍[0,300]。
DelayType: #Exponential(預設),Constant,Linear。Exponential類型的稍候再試為:2 ^ times(重試次數);Constant類型的稍候再試固定為Delay;Linear類型的稍候再試為:Delay + BackOff * times(重試次數)。
Delay: #DelayType為Constant或Linear時需要添加此屬性,表示稍候再試時間,填寫正整數,如10,表示稍候再試為10s,預設值2,取值範圍[1,3600]。
BackOff: #DelayType為Linear時需要添加此屬性,表示稍候再試補償時間,填寫正整數,預設值為2,取值範圍[1,3600]。
MaxRetryInterval: #最大稍候再試, 單位秒,預設值為1800,填寫正整數,取值範圍[1, 1800]。
Outputs: #WaitFor動作所在的任務支援輸出,當任務滿足NotDesiredValues/DesiredValues的條件時,任務執行成功後會返回輸出參數的。
ValueSelector: #jq的選取器文法,以OpenAPI的返回作為JSON輸入,jq的文法請參考 https://stedolan.github.io/jq
Type: String/Boolean/List(Array)/Number/Object
{
"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"
]
},
"Retries": "#可選,表示最大重試次數,預設值10,填寫正整數,如5,取值範圍[0,300]。",
"DelayType": "#Exponential(預設),Constant,Linear。Exponential類型的稍候再試為:2 ^ times(重試次數);Constant類型的稍候再試固定為Delay;Linear類型的稍候再試為:Delay + BackOff * times(重試次數)。",
"Delay": "#DelayType為Constant或Linear時需要添加此屬性,表示稍候再試時間,填寫正整數,如10,表示稍候再試為10s,預設值2,取值範圍[1,3600]。",
"BackOff": "#DelayType為Linear時需要添加此屬性,表示稍候再試補償時間,填寫正整數,預設值為2,取值範圍[1,3600]。",
"MaxRetryInterval": "#最大稍候再試, 單位秒,預設值為1800,填寫正整數,取值範圍[1, 1800]。"
"Outputs": {
"ValueSelector": "# jq的選取器文法,以OpenAPI的返回作為JSON輸入,jq的文法請參考 https://stedolan.github.io/jq",
"Type": "String/Boolean/List(Array)/Number/Object"
}
}
]
}
樣本
如下模板:啟動一台指定執行個體,通過調用WaitFor等待執行個體的狀態變為Running。
---
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
MaxRetryInterval: 900
Properties:
Service: ECS
API: DescribeInstances
Parameters:
InstanceIds:
- '{{ InstanceId }}'
PropertySelector: Instances.Instance[].Status
DesiredValues:
- Running
{
"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",
"MaxRetryInterval": 900,
"Properties": {
"Service": "ECS",
"API": "DescribeInstances",
"Parameters": {
"InstanceIds": [ "{{ InstanceId }}"]
},
"PropertySelector": "Instances.Instance[].Status",
"DesiredValues": [ "Running" ]
}
}
]
}