简体   繁体   English

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

[英]AWS step functions task success callback using Boto3

Overview概述

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. 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.

If I don't add this the function execution fails as the state machine has not completed.如果我不添加此 function 执行将失败,因为 state 机器尚未完成。

Code代码

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"])

Question问题

Can I use some kind of callback or token to complete this without the sleep call?我可以在没有睡眠调用的情况下使用某种回调或令牌来完成此操作吗?

If I understand correctly this SendTaskSuccess API call may be what I need, but I can't find any code examples.如果我理解正确,这个SendTaskSuccess API 调用可能是我需要的,但我找不到任何代码示例。

I am curious how you can catch the status back in the same lambda invocation without waiting.我很好奇您如何无需等待即可在同一个 lambda 调用中恢复状态。 Even if you are using a callback, it will end up in a totally separate invocation of lambda depending on the lambda concurrency settings.即使您使用回调,它也会以完全独立的 lambda 调用结束,具体取决于 lambda 并发设置。

API Call SendTaskSuccess used in Callbacks pattern where a workflow is paused until a task token is returned. API 调用SendTaskSuccess回调模式中使用,其中工作流暂停,直到返回任务令牌。 The task will pause until it receives that task token back with a SendTaskSuccess or SendTaskFailure call.该任务将暂停,直到它通过SendTaskSuccessSendTaskFailure调用接收到该任务令牌。

I would break this handle execution output part into a separate lambda, save money and time.我会将这个句柄执行 output 部分分解为单独的 lambda,节省金钱和时间。

For that, you can have a EventBridge (CloudWatch Events) for Step Functions execution status changes configured which can invoke your lambda about the full details.为此,您可以为 Step Functions 执行状态更改配置一个 EventBridge (CloudWatch Events),它可以调用您的 lambda 关于完整详细信息。 Below is an example of SUCCEEDED state of step function execution, and you can hook your lambda to it.下面是步骤 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", ""]
  }
}

Callbacks are might not be good for this flow.回调可能不适合此流程。 Below is an example of send_task_success下面是send_task_success的示例

Callback Pattern Example (Amazon SQS, Amazon SNS, Lambda) 回调模式示例(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