简体   繁体   中英

How do I use Heartbeat with a Callback Return Step Function in my Lambda Function?

My Lambda function is required to send a token back to the step function for it to continue, as it is a task within the state machine.

Looking at my try/catch block of the lambda function, I am contemplating:

  1. The order of SendTaskHeartbeatCommand and SendTaskSuccessCommand
  2. The required parameters of SendTaskHeartbeatCommand
  3. Whether I should add the SendTaskHeartbeatCommand to the catch block, and then if yes, which order they should go in.

Current code:

  try {
    const magentoCallResponse = await axios(requestObject);
    await stepFunctionClient.send(new SendTaskHeartbeatCommand(taskToken));
    await stepFunctionClient.send(new SendTaskSuccessCommand({output: JSON.stringify(magentoCallResponse.data), taskToken}));
    return magentoCallResponse.data;
  } catch (err: any) {
    console.log("ERROR", err);
    await stepFunctionClient.send(new SendTaskFailureCommand({error: JSON.stringify("Error Sending Data into Magento"), taskToken}));
    return false;
  }

I have read the documentation for AWS SDK V3 for SendTaskHeartbeatCommand and am confused with the required input .

The SendTaskHeartbeat and SendTaskSuccess API actions serve different purposes.

When your task completes, you call SendTaskSucces to report this back to Step Functions and to provide the results from the Task that your workflow can then process. You do not need to call SendTaskHeartbeat before SendTaskSuccess and the usage you have in the code above seems unnecessary.

SendTaskHeartbeat is optional and you use it when you've set "HeartbeatSeconds" on your Task. When you do this, you then need your worker (ie the Lambda function in this case) to send back regular heartbeats while it is processing work. I'd expect that to be running asynchronously while your code above was running the first line in the try block. The reason for having heartbeats is that you can set a longer TimeoutSeconds (or dynamically using TimeoutSecondsPath) than HeartbeatSeconds, therefore failing / retrying fast when the worker dies (Heartbeat timeout) while you still allow your tasks to take longer to complete.

That said, it's not clear why you are using.waitForTaskToken with Lambda. Usually, you can just use the default Request Response integration pattern with Lambda. This uses the synchronous invoke mode for Lambda and will return the response back to you without you needing to integrate back with Step Functions in your Lambda code. Possibly you are reading these off of an SQS queue for concurrency control or something. But if not, just use Request Response.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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