不幸的是,您无法动态指定状态的超时,但您可以动态地告诉等待状态应该等待多长时间。话虽如此,我建议您使用具有两个分支和一个 catch 块的并行状态。第一个分支包含等待状态和失败状态(超时)。另一个分支包含正常的状态机逻辑和失败状态。
每当并行状态中的一个分支失败时,它就会中止其他分支中的所有正在运行的状态。幸运的是,您能够在并行状态中捕获这些错误,并根据哪个分支失败将其重定向到另一个状态。这是我的意思的一个示例(更改 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
}
}
}