我已附上我的状态机的外观,并共享定义以供引用。
Service Status Retry Wait
状态在调用 Service Status Retry
状态之前等待 60 秒,该状态调用 lambda 来检查服务是否已在 Service Created? 中创建?
状态。
{
"Comment": "Step Function for Deployment",
"StartAt": "Start",
"States": {
"Start": {
"Type": "Pass",
"Next": "Service Status"
},
"Service Status": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "Service Status Check",
"States": {
"Service Status Check": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-west-2:<acc-id>:function:<fn-name>",
"Payload": {
"stepFunctionState.$": "$$.State.Name",
"requestContext": {
"stepFunction": true,
"accountId": "<acc-id>"
},
"resource.$": "States.Format('{}/srvc', $$.Execution.Input.resource)",
"path.$": "States.Format('{}/srvc', $$.Execution.Input.path)",
"httpMethod": "POST",
"pathParameters.$": "$$.Execution.Input.pathParameters",
"body.$": "$$.Execution.Input.body",
"isBase64Encoded": false,
"headers": {},
"queryStringParameters": null
}
},
"ResultPath": "$.taskresult",
"Next": "Service Created?"
},
"Service Created?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.taskresult.Payload.body",
"StringMatches": "*\"isCreated\": \"false\"*",
"Next": "Service Status Retry Wait"
},
{
"Variable": "$.taskresult.Payload.body",
"StringMatches": "*\"isCreated\": \"true\"*",
"Next": "Service Status Check End"
}
],
"Default": "Service Status Retry Wait"
},
"Service Status Retry Wait": {
"Type": "Wait",
"Seconds": 60,
"Next": "Service Status Retry"
},
"Service Status Retry": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-west-2:<acc-id>:function:<func-name>",
"Payload": {
"stepFunctionState.$": "$$.State.Name",
"requestContext": {
"stepFunction": true,
"accountId": "<acc-id>"
},
"resource.$": "States.Format('{}/srvc', $$.Execution.Input.resource)",
"path.$": "States.Format('{}/srvc', $$.Execution.Input.path)",
"httpMethod": "POST",
"pathParameters.$": "$$.Execution.Input.pathParameters",
"body.$": "$$.Execution.Input.body",
"isBase64Encoded": false,
"headers": {},
"queryStringParameters": null
}
},
"ResultPath": "$.taskresult",
"Next": "Service Created?"
},
"Service Status Check End": {
"Type": "Pass",
"End": true
}
}
}
],
"Next": "End"
},
"End": {
"Type": "Pass",
"End": true
}
}
}
直到并且除非创建服务,否则此状态机将继续循环运行。我想知道是否有办法打破循环并在重试 5 次后退出。它可能是在服务状态重试等待
或服务状态重试
被调用5次之后发生。
或者是否有办法知道它现在已经循环了 1 小时并据此退出。上述两个选项中的任何一个都应该适合我。
最佳答案
您有 2 个选择:
{
"Comment": "Step Function for Deployment",
"StartAt": "Start",
"States": {
"Start": {
"Type": "Pass",
"Next": "Service Status"
},
"Service Status": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "Service Status Check",
"States": {
"Service Status Check": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-west-2:<acc-id>:function:<fn-name>",
"Payload": {
"stepFunctionState.$": "$$.State.Name",
"requestContext": {
"stepFunction": true,
"accountId": "<acc-id>"
},
"resource.$": "States.Format('{}/srvc', $$.Execution.Input.resource)",
"path.$": "States.Format('{}/srvc', $$.Execution.Input.path)",
"httpMethod": "POST",
"pathParameters.$": "$$.Execution.Input.pathParameters",
"body.$": "$$.Execution.Input.body",
"isBase64Encoded": false,
"headers": {},
"queryStringParameters": null
}
},
"ResultPath": "$.taskresult",
"Next": "Service Created?"
},
"Service Created?": {
"Type": "Choice",
"Choices": [
{
"Variable": "$.taskresult.Payload.body",
"StringMatches": "*\"isCreated\": \"false\"*",
"Next": "Throw Error"
},
{
"Variable": "$.taskresult.Payload.body",
"StringMatches": "*\"isCreated\": \"true\"*",
"Next": "Succeed"
}
],
"Default": "Throw Error"
},
"Throw Error": {
"Type": "Fail"
},
"Succeed": {
"Type": "Pass",
"End": true
}
}
}
],
"Next": "End",
"Retry": [
{
"ErrorEquals": [
"States.ALL"
],
"IntervalSeconds": 3,
"MaxAttempts": 5,
"BackoffRate": 1.5
}
]
},
"End": {
"Type": "Pass",
"End": true
}
}
}
关于amazon-web-services - AWS Step Function - 根据条件停止状态机执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66874953/