AWS Lambda发送部分响应 [英] AWS lambda send partial response

查看:75
本文介绍了AWS Lambda发送部分响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个lambda函数,它执行一系列操作.我有一个触发lambda函数的应用程序.每个动作完成后,有没有办法让我从lambda函数发送部分响应.

I have a lambda function which does a series of actions. I have a react application which triggers the lambda function. Is there a way I can send a partial response from the lambda function after each action is complete.

const testFunction = (event, context, callback) => {
    let partialResponse1 = await action1(event);
    // send partial response to client
    let partialResponse2 = await action2(partialResponse1);
    // send partial response to client  
    let partialResponse3 = await action3(partialResponse2);
    // send partial response to client
    let response = await action4(partialResponse3); 
    // send final response
}

在lambda函数中有可能吗?如果是这样,我们该怎么做.任何参考文档或示例代码都将提供很大的帮助.谢谢.

Is this possible in lambda functions? If so, how we can do this. Any ref docs or sample code would be do a great help. Thanks.

注意:这是在客户端显示带有%的加载程序的相当简单的情况.我不想让SQS或步骤功能变得过于复杂.

Note: This is fairly a simple case of showing a loader with % on the client-side. I don't want to overcomplicate things SQS or step functions.

我仍在寻找答案.

推荐答案

据我了解,您正在使用API​​ Gateway + Lambda,并希望通过UI显示Lambda的进度.

From what I understand you're using API Gateway + Lambda and are looking to show the progress of the Lambda via UI.

由于每个步骤都必须在下一步开始之前完成,所以我认为没有理由不调用lambda 4次或将lambda拆分为4个单独的lambda.

Since each step must finish before the next step begin I see no reason not to call the lambda 4 times, or split the lambda to 4 separate lambdas.

例如:

// Not real syntax!

try {
  res1 = await ajax.post(/process, {stage: 1, data: ... });
  out(stage 1 complete);
  res2 = await ajax.post(/process, {stage: 2, data: res1});
  out(stage 2 complete);
  res3 = await ajax.post(/process, {stage: 3, data: res2});
  out(stage 3 complete);
  res4 = await ajax.post(/process, {stage: 4, data: res3});
  out(stage 4 complete);
  out(process finished);
catch(err) {
  out(stage {$err.stage-number} failed to complete);
}

如果您仍然希望在同一lambda执行期间执行所有4个调用,则可以执行以下操作(如果预期该过程很长,则尤其如此)(并且因为通常不建议执行长时间挂起""http交易).

If you still want all 4 calls to be executed during the same lambda execution you may do the following (this especially true if the process is expected to be very long) (and because it's usually not good practice to execute "long hanging" http transaction).

您可以通过将进度"保存在数据库中来实现它,并且在该过程完成后,也将结果保存到数据库中.

You may implement it by saving the "progress" in a database, and when the process is complete save the results to the database as well.

您需要做的就是每隔X秒查询一次状态.

All you need to do is query the status every X seconds.

// Not real syntax

Gateway-API --> lambda1 - startProcess(): returns ID {
  uuid = randomUUID();
  write to dynamoDB { status: starting }.
  send sqs-message-to-start-process(data, uuid);
  return response { uuid: uuid };
}

SQS --> lambda2 - execute(): returns void {
    try {
      let partialResponse1 = await action1(event);
      write to dynamoDB { status: action 1 complete }.
      // send partial response to client
      let partialResponse2 = await action2(partialResponse1);
      write to dynamoDB { status: action 2 complete }.
      // send partial response to client  
      let partialResponse3 = await action3(partialResponse2);
      write to dynamoDB { status: action 3 complete }.
      // send partial response to client
      let response = await action4(partialResponse3); 
      write to dynamoDB { status: action 4 complete, response: response }.
    } catch(err) {
      write to dynamoDB { status: failed, error: err }.
    }
}

Gateway-API --> lambda3 -> getStatus(uuid): returns status {
  return status from dynamoDB (uuid);
}

Your UI Code:

res = ajax.get(/startProcess);
uuid = res.uuid;

in interval every X (e.g. 3) seconds:
  status = ajax.get(/getStatus?uuid=uuid);
  show(status);
  if (status.error) {
    handle(status.error) and break;
  }
  if (status.response) {
    handle(status.response) and break;
  }
}

请记住,lambda的执行时间不能超过15分钟.因此,您需要100%确保无论该过程做什么,它都不会超出此硬限制.

Just remember that lambda's cannot exceed 15 minutes execution. Therefore, you need to be 100% certain that whatever the process does, it never exceeds this hard limit.

这篇关于AWS Lambda发送部分响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆