AWS Step Functions 是否具有超时功能?

mat*_*c-7 2 amazon-web-services aws-step-functions

现在我有一个 AWS Step Function 来创建、运行和终止 EMR 集群作业。我想添加一个超时功能来停止作业并在集群卡住或运行时间过长的情况下终止集群(例如,将输入变量"TIMEOUT_AFTER_X_HOURS": 12与将自动停止的集群配置一起传递到状态机中如果集群在 12 小时后仍在运行,则终止该作业)。有谁知道如何做到这一点?

Joe*_*Joe 6

不幸的是,您不能动态指定状态的超时时间,但您可以动态地告诉等待状态它应该等待多长时间。话虽如此,我建议您使用具有两个分支和一个 catch 块的并行状态。第一个分支包含等待状态和失败状态(您的超时)。另一个分支包含您的正常状态机逻辑和失败状态。

每当一个分支在 Parallel 状态内失败时,它就会中止其他分支中的所有运行状态。幸运的是,您能够在并行状态中捕获这些错误,并根据哪个分支失败将其重定向到另一个状态。这是我的意思的示例(更改 HardCodedInputs 状态中的值以控制哪个分支失败)。

{
"StartAt": "HardCodedInputs",
"States": {
    "HardCodedInputs": {
        "Type": "Pass",
        "Parameters": {
            "WaitBranchInput": {
                "timeout": 5,
                "Comment": "Change the value of timeout"
            },
            "WorkerBranchInput": {
                "SecondsPath": 3,
                "Comment": "SecondsPath is used for testing purposes to simulate how long the worker will run"
            }
        },
        "Next": "Parallel"
    },
    "Parallel": {
        "Type": "Parallel",
        "End": true,
        "Catch": [{
            "ErrorEquals": ["TimeoutExpired"],
            "ResultPath": "$.ParralelStateOutput",
            "Next": "ExecuteIfTimedOut"
        }, {
            "ErrorEquals": ["WorkerSuccess"],
            "ResultPath": "$.ParralelStateOutput",
            "Next": "ExecuteIfWorkerSuccesfull"
        }],
        "Branches": [{
                "StartAt": "DynamicTimeout",
                "States": {
                    "DynamicTimeout": {
                        "Type": "Wait",
                        "InputPath": "$.WaitBranchInput",
                        "SecondsPath": "$.timeout",
                        "Next": "TimeoutExpired"
                    },
                    "TimeoutExpired": {
                        "Type": "Fail",
                        "Cause": "TimeoutExceeded.",
                        "Error": "TimeoutExpired"
                    }
                }
            },
            {
                "StartAt": "WorkerState",
                "States": {
                    "WorkerState": {
                        "Type": "Wait",
                      "InputPath": "$.WorkerBranchInput",
                        "SecondsPath": "$.SecondsPath",
                        "Next": "WorkerSuccessful"
                    },
                    "WorkerSuccessful": {
                        "Type": "Fail",
                        "Cause": "Throw Worker Success Exception",
                        "Error": "WorkerSuccess"
                    }
                }
            }
        ]
    },
    "ExecuteIfTimedOut": {
        "Type": "Pass",
        "End": true
    },
    "ExecuteIfWorkerSuccesfull": {
        "Type": "Pass",
        "End": true
    }
 }
}
Run Code Online (Sandbox Code Playgroud)

  • 在一个非常复杂的状态机上成功地使用了这种方法,其中我有 4 个可能的分支可以停止给定的处理。不幸的是,本机并行没有“任何时候”选项,因为这可以避免使用异常作为控制流。 (2认同)

小智 5

您可以将输入变量的路径(例如,"$.TIMEOUT_AFTER_X_HOURS"来自原始示例)传递给TimeoutSecondsPath任何任务的参数。这将允许您根据状态机输入或先前步骤的输出动态设置步骤超时。

您可以在此处找到该TimeoutSecondsPath参数的官方文档: https: //docs.aws.amazon.com/step-functions/latest/dg/amazon-states-language-task-state.html

  • 我认为这是截至 2021 年的正确且最新的答案。 (2认同)