简体   繁体   English

如何在我的 Lambda Function 中使用带有回调返回步骤 Function 的 Heartbeat?

[英]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.我的 Lambda function 需要将令牌发送回步骤 function 才能继续,因为这是 state 机器中的一项任务。

Looking at my try/catch block of the lambda function, I am contemplating:看着我的 lambda function 的try/catch块,我正在考虑:

  1. The order of SendTaskHeartbeatCommand and SendTaskSuccessCommand SendTaskHeartbeatCommandSendTaskSuccessCommand的顺序
  2. The required parameters of SendTaskHeartbeatCommand SendTaskHeartbeatCommand所需参数
  3. Whether I should add the SendTaskHeartbeatCommand to the catch block, and then if yes, which order they should go in.我是否应该将SendTaskHeartbeatCommand添加到catch块,然后如果是,它们应该按哪个顺序添加 go。

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 .我已阅读 SendTaskHeartbeatCommand 的 AWS SendTaskHeartbeatCommand V3 文档,但对所需的input感到困惑。

The SendTaskHeartbeat and SendTaskSuccess API actions serve different purposes. SendTaskHeartbeat 和 SendTaskSuccess API 操作有不同的用途。

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.当您的任务完成时,您调用 SendTaskSucces 以将此报告回 Step Functions 并提供您的工作流随后可以处理的任务结果。 You do not need to call SendTaskHeartbeat before SendTaskSuccess and the usage you have in the code above seems unnecessary.您不需要在 SendTaskSuccess 之前调用 SendTaskHeartbeat 并且上面代码中的用法似乎没有必要。

SendTaskHeartbeat is optional and you use it when you've set "HeartbeatSeconds" on your Task. SendTaskHeartbeat 是可选的,当您在任务上设置“HeartbeatSeconds”时可以使用它。 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.当您这样做时,您需要您的工作人员(即本例中的 Lambda function)在处理工作发回定期心跳。 I'd expect that to be running asynchronously while your code above was running the first line in the try block.我希望在上面的代码运行 try 块中的第一行时异步运行。 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.有心跳的原因是您可以设置比 HeartbeatSeconds 更长的 TimeoutSeconds(或动态使用 TimeoutSecondsPath),因此当工作人员死亡(心跳超时)时快速失败/重试,同时您仍然允许您的任务花费更长的时间来完成。

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.也就是说,不清楚为什么要使用 Lambda 的 .waitForTaskToken。通常,您可以只使用默认的请求响应集成模式和 Lambda。这使用 Lambda 的同步调用模式,并且无需您即可将响应返回给您在您的 Lambda 代码中与 Step Functions 集成。 Possibly you are reading these off of an SQS queue for concurrency control or something.可能您正在从 SQS 队列中读取这些内容以进行并发控制或其他操作。 But if not, just use Request Response.但如果没有,只需使用 Request Response。

暂无
暂无

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

相关问题 如何从Lambda内的回调function返回json? - How to return json from callback function within the Lambda? 如何为 Cloudfront 创建 HttpOrigin 以使用 Lambda function url? - How do I create a HttpOrigin for Cloudfront to use a Lambda function url? 为什么我的 Lambda Function 不是 `startExecution` 我的步骤 Function - Why Does My Lambda Function Not `startExecution` My Step Function 如何指定从步骤 function 调用 lambda function 的方法和路径? - How can I specify method and path for calling lambda function from step function? AWS Lambda 步骤 function 集成 - AWS Lambda step function Integration 在步骤 Function 中,如何将标量数组转换为字典数组? - In a Step Function, how do I convert an array of scalars into an array of dictionaries? 如何在 AWS 中删除或重命名步骤 function 执行 - how do i delete or rename a step function execution in AWS 如何在lambda function中使用pyhive? - how to use pyhive in lambda function? 我可以在成功完成另一个 Lambda function 后触发一个 Lambda function(不使用 Step Function)吗? - Can I trigger one Lambda function after successful completion of another Lambda function (without using Step Function)? 如何使用无服务器框架通过 AWS API 网关在 Node.js 中编写的 AWS Lambda function 上返回错误? - How do I return errors on an AWS Lambda function written in Node.js through the AWS API Gateway using the Serverless framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM