繁体   English   中英

使用 Boto3 的 AWS 步骤函数任务成功回调

[英]AWS step functions task success callback using Boto3

概述

When using the start_execution method for an AWS Step Function with the SDK for Python (Boto3) I have added a 'time.sleep(6)' call to allow the step function execution to complete as a temporary fix.

如果我不添加此 function 执行将失败,因为 state 机器尚未完成。

代码

def runStateMachine(dictInput):
    response = client.start_execution(
        stateMachineArn=arn,
        input=json.dumps(dictInput))
    time.sleep(6)
    executionArn = response["executionArn"]
    desc_exec_resp = client.describe_execution(
        executionArn=executionArn)
    return json.loads(desc_exec_resp["output"])

问题

我可以在没有睡眠调用的情况下使用某种回调或令牌来完成此操作吗?

如果我理解正确,这个SendTaskSuccess API 调用可能是我需要的,但我找不到任何代码示例。

我很好奇您如何无需等待即可在同一个 lambda 调用中恢复状态。 即使您使用回调,它也会以完全独立的 lambda 调用结束,具体取决于 lambda 并发设置。

API 调用SendTaskSuccess回调模式中使用,其中工作流暂停,直到返回任务令牌。 该任务将暂停,直到它通过SendTaskSuccessSendTaskFailure调用接收到该任务令牌。

我会将这个句柄执行 output 部分分解为单独的 lambda,节省金钱和时间。

为此,您可以为 Step Functions 执行状态更改配置一个 EventBridge (CloudWatch Events),它可以调用您的 lambda 关于完整详细信息。 下面是步骤 function 执行的成功SUCCEEDED的示例,您可以将 lambda 挂接到它。

{
  "source": ["aws.states"],
  "detail-type": ["Step Functions Execution Status Change"],
  "detail": {
    "status": ["SUCCEEDED"],
    "stateMachineArn": ["arn:aws:states:eu-central-1:1234567890:stateMachine:Mapstate", ""]
  }
}

回调可能不适合此流程。 下面是send_task_success的示例

回调模式示例(Amazon SQS、Amazon SNS、Lambda)

import boto3

step_functions = boto3.client('stepfunctions')

def handler(event, context):
    # extract token from the event

    task_token = event['taskToken']
    step_functions.send_task_success(
        taskToken=task_token,
        output='json output of the task'
    )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM