我需要运行一个AWS Step函数,该函数运行一个并行状态机,例如两个状态机。我的要求是检查并行计算机的最终执行状态,如果有任何故障,请调用SNS服务以发送电子邮件。很标准的东西,但对我来说,我无法弄清楚如何捕获并行步进机的综合误差。此示例并行机运行
如果我执行此计算机,则此计算机正确显示passtask成功,failtask被取消,Total Parallel Task成功(?????),Notify Failure任务被取消以及状态机的整体执行“ failed”也一样
我希望密码任务成功,失败任务失败,整体并行任务失败,通知失败任务成功。
{
"Comment": "Parallel Example",
"StartAt": "Parallel Task",
"TimeoutSeconds": 120,
"States": {
"Parallel Task": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "passtask",
"States": {
"passtask": {
"Type": "Task",
"Resource":"arn:xxxxxxxxxxxxxxx:function:passfunction",
"End": true
}
}
},
{
"StartAt": "failtask",
"States": {
"failtask": {
"Type": "Task",
"Resource":"arn: xxxxxxxxxxxxxxx:function:failfunction",
"End": true
}
}
}
],
"ResultPath": "$.status",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "Notify Failure"
}
],
"Next": "Notify Success"
},
"Notify Failure": {
"Type": "Pass",
"InputPath": "$.input.Cause",
"End": true
},
"Notify Success": {
"Type": "Pass",
"Result": "This is a fallback from a task success",
"End": true
}
}
}
答案 0 :(得分:1)
根据您的要求“我的要求是检查并行计算机的最终执行状态,如果有任何故障,请调用SNS服务以发送电子邮件。”,我了解到“ failtask”仅用于调试目的,并在将来不会失败。因此,问题是,在步进功能检测到分支中的故障时,所有其他分支都将终止,并且其输出将被丢弃,仅使用故障分支的输出。因此,如果要保留每个分支的输出并检查是否发生了故障,则需要处理每个分支中的错误,而不要将整个分支报告为失败。另外,您将需要向每个分支添加一个输出字段,该字段说明是否失败(如果字段不存在,则选择状态将给出错误)。还要记住,ParralelState的输出是一个数组,其中包含每个Branch的输出,例如,此状态机应让每个分支完成执行并正确处理错误:
{
"Comment": "Parallel Example",
"StartAt": "Parallel Task",
"TimeoutSeconds": 120,
"States": {
"Parallel Task": {
"Type": "Parallel",
"Branches": [{
"StartAt": "passtask",
"States": {
"passtask": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXXXXXXX",
"Next": "SuccessBranch1",
"Catch": [{
"ErrorEquals": ["States.ALL"],
"Next": "FailBranch1"
}]
},
"SuccessBranch1": {
"Type": "Pass",
"Result": {
"Error": false
},
"ResultPath": "$.Status",
"End": true
},
"FailBranch1": {
"Type": "Pass",
"Result": {
"Error": true
},
"ResultPath": "$.Status",
"End": true
}
}
},
{
"StartAt": "failtask",
"States": {
"failtask": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:XXXXXXXXXXXXXXXXX",
"Next": "SuccessBranch2",
"Catch": [{
"ErrorEquals": ["States.ALL"],
"Next": "FailBranch2"
}]
},
"SuccessBranch2": {
"Type": "Pass",
"Result": {
"Error": false
},
"ResultPath": "$.Status",
"End": true
},
"FailBranch2": {
"Type": "Pass",
"Result": {
"Error": true
},
"ResultPath": "$.Status",
"End": true
}
}
}
],
"ResultPath": "$.ParralelOutput",
"Catch": [{
"Comment": "This catch should never catch any errors, as the error handling is done in the individual Branches",
"ErrorEquals": ["States.ALL"],
"ResultPath": "$.ParralelOutput",
"Next": "ChoiceStateX"
}],
"Next": "ChoiceStateX"
},
"ChoiceStateX": {
"Type": "Choice",
"Choices": [{
"Or": [{
"Variable": "$.ParralelOutput[0].Status.Error",
"BooleanEquals": true
},
{
"Variable": "$.ParralelOutput[1].Status.Error",
"BooleanEquals": true
}
],
"Next": "Notify Failure"
}],
"Default": "Notify Success"
},
"Notify Failure": {
"Type": "Pass",
"End": true
},
"Notify Success": {
"Type": "Pass",
"Result": "This is a fallback from a task success",
"End": true
}
}
}
关于控制台报告的内容(成功完成Parrarel状态,并且在任务状态上未显示错误),看起来控制台有点坏,如果您在Parralel State中发现任何错误,它将报告为成功,并且同样,如果您的任何分支机构失败,它也不会由于某种原因将该分支报告为失败。因此,不必担心控制台显示的内容,并确保流程正常运行。可以通过用FailState替换Task State来测试:
{
"Comment": "Parallel Example",
"StartAt": "Parallel Task",
"TimeoutSeconds": 120,
"States": {
"Parallel Task": {
"Type": "Parallel",
"Branches": [
{
"StartAt": "passtask",
"States": {
"passtask": {
"Type": "Succeed"
}
}
},
{
"StartAt": "failtask",
"States": {
"failtask": {
"Type": "Fail"
}
}
}
],
"ResultPath": "$.status",
"Catch": [
{
"ErrorEquals": ["States.ALL"],
"Next": "Notify Failure"
}
],
"Next": "Notify Success"
},
"Notify Failure": {
"Type": "Pass",
"End": true
},
"Notify Success": {
"Type": "Pass",
"Result": "This is a fallback from a task success",
"End": true
}
}
}