本文介绍了使用Dynamodb和SNS的Lambda函数-超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

正在尝试使用SNS服务中的DynamoDb Item值发送通知。它因超时错误而失败,但是从Dynamodb提取成功,并且还发送了通知。但是经过多次处理后,它将保持重试和超时。

现在,我还尝试使用参数 context.callbackWaitsForEmptyEventLoop = false 但对您有很大帮助!似乎没有正确使用它。



下面是一段代码,有人可以帮忙吗?

  exports.handler =函数(事件,上下文,回调){

var AWS = require('aws-sdk');
var doc = require(‘dynamodb-doc’);
var dynamodb = new doc.DynamoDB();

var params = {
TableName:'abcmsg',
ProjectionExpression:'Message',
键:{
ind: ABC10
}
};

var MESSAGE_ID = getmsg();

函数getmsg(MESSAGE_ID){
dynamodb.getItem(params,function(err,data){
if(err){
console.log('ERROR :Dynamo失败:',err);
getmsg(err);
} else {
console.log('Dynamo Success:'+ JSON.stringify(data,null,'') );
let MESSAGE_ID = data ['Item']。Message;
console.log('data',data ['Item']。Message);
getmsg(MESSAGE_ID);
}
});


console.log('MESSAGE_ID',MESSAGE_ID); //打印正确的值'ABCD'

var sns = new AWS.SNS({
region:'eu-west-1',
maxRetries:0

});

sns.publish({
消息:MESSAGE_ID,
主题:测试消息,
TopicArn: arn:aws:sns:eu-west-1 :0123456789012:Lambda
},函数(错误,数据){
if(err){
console.log(err.stack);
return;
}
console.log('push send');
});

回调(null,{ statusCode:200});
context.callbackWaitsForEmptyEventLoop = false;

}

};

日志:-----



2018-01-02T10:14:55.005Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo成功:{
Item:{
Message: ABCD
}
}
2018-01-02T10:14:55.005Z c18d142e-efa5-11e7-8671-7f3af13c58c7数据ABCD
2018-01-02T10:14:55.006Z c18d142e-efa5-11e7-8671 -7f3af13c58c7 MESSAGE_ID ABCD
2018-01-02T10:14:55.028Z c18d142e-efa5-11e7-8671-7f3af13c58c7推送已发送
2018-01-02T10:14:55.086Z c18d142e-efa5-11e7-8671 -7f3af13c58c7 Dynamo成功:{
Item:{
Message: ABCD
}
}
2018-01-02T10:14:55.105Z c18d142e-efa5-11e7-8671-7f3af13c58c7数据ABCD
2018-01-02T10:14:55.106Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD
2018-01-02T10:14:55.127Z c18d142e-efa5-11e7-8671-7f3af13c58c7推送已发送
2018-01-02T10:14:55.167Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo成功:{
Item:{
消息: A BCD
}
}
2018-01-02T10:14:55.185Z c18d142e-efa5-11e7-8671-7f3af13c58c7数据ABCD
2018-01-02T10:14:55.186 Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD
2018-01-02T10:14:55.226Z c18d142e-efa5-11e7-8671-7f3af13c58c7推送已发送
2018-01-02T10:14:55.253 Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo成功:{
项目:{
消息: ABCD
}
}
2018- 01-02T10:14:55.265Z c18d142e-efa5-11e7-8671-7f3af13c58c7数据ABCD
2018-01-02T10:14:55.285Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD
2018- 01-02T10:14:55.306Z c18d142e-efa5-11e7-8671-7f3af13c58c7推送已发送
2018-01-02T10:14:55.345Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo成功:{
项目:{
消息: ABCD
}
}
2018-01-02T10:14:55.365Z c18d142e-efa5-11e7-8671-7f3af13c58c7数据ABCD
2018-01-02T10:14:55.366Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD
2018-01-02T10:14:55.386Z c18d142e-efa5-11e7-8671-7f3af13c58c7推送已发送
2018-01-02T10:14:55.406Z c18d142e-efa5-11e7-8671-7f3af13c58c7推送发送
2018-01-02T10:14:55.445Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo成功:{
项目:{
消息: ABCD
}
}
2018-01-02T10:14:55.445Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD
END RequestId:c18d142e-efa5-11e7-8671-7f3af13c58c7
报告RequestId:c18d142e-efa5-11e7-8671-7f3af13c58c7持续时间:10010.15 ms计费持续时间:10000 ms内存大小:128 MB使用的最大内存:44 MB

2018-01-02T10:14: 55.463Z c18d142e-efa5-11e7-8671-7f3af13c58c7任务在10.01秒后超时

解决方案

看起来像您正在尝试获取键值为 ind的项目: ABC10 并使用SNS进行通知。



在这种情况下, sns.publish 应该在成功完成 getItem 即ins时调用 getmsg(MESSAGE_ID)之后,应调用 sns发布功能(即您需要创建一个以发布消息)或应该执行 sns发布代码块(即,只需将sns发布代码块复制并粘贴到getItem else部分中)。 / p>

我不明白为什么您要调用getmsg(err)(即在getItem错误时)或getmsg(MESSAGE_ID)(即在getItem成功时)?我认为这不正确。基本上是在循环中调用getItem,这会导致超时错误。


Am trying to use a DynamoDb Item value in the SNS service to send the notification. Its failing with the timeout error however fetch from Dynamodb is successful and notification is also sent. But it keep retrying and timeout after processing multiple times.

Now, I also tried using the parameter context.callbackWaitsForEmptyEventLoop = false but dint help much!! Seems am not using it correctly.

Below is the piece of code, can someone please assist:

exports.handler = function (event, context, callback) {

var AWS = require('aws-sdk');
var doc = require('dynamodb-doc');
var dynamodb = new doc.DynamoDB();

var params = {
TableName : 'abcmsg',
ProjectionExpression: 'Message',
  Key : {
   "ind" : "ABC10"
    }
};

var MESSAGE_ID = getmsg();

function getmsg(MESSAGE_ID) {
dynamodb.getItem(params, function (err, data) {
        if (err) {
             console.log('ERROR: Dynamo Failed:', err);
             getmsg(err);
        } else {
               console.log('Dynamo Success: ' + JSON.stringify(data, null, '  '));
               let MESSAGE_ID = data['Item'].Message;
               console.log('data',data['Item'].Message);
               getmsg(MESSAGE_ID);
        }
});


console.log('MESSAGE_ID',MESSAGE_ID); //print the correct value 'ABCD'

var sns = new AWS.SNS({
      region:'eu-west-1',
      maxRetries: 0

});

sns.publish({
Message: MESSAGE_ID,
Subject:"Test Message",
TopicArn:"arn:aws:sns:eu-west-1:0123456789012:Lambda"
}, function (err, data) {
if (err) {
console.log(err.stack);
return;
}
console.log('push sent');
});

callback(null,{ "statusCode": 200 });
context.callbackWaitsForEmptyEventLoop = false;

}

};

Logs:-----

2018-01-02T10:14:55.005Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" }}2018-01-02T10:14:55.005Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD2018-01-02T10:14:55.006Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD2018-01-02T10:14:55.028Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent2018-01-02T10:14:55.086Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" }}2018-01-02T10:14:55.105Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD2018-01-02T10:14:55.106Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD2018-01-02T10:14:55.127Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent2018-01-02T10:14:55.167Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" }}2018-01-02T10:14:55.185Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD2018-01-02T10:14:55.186Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD2018-01-02T10:14:55.226Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent2018-01-02T10:14:55.253Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" }}2018-01-02T10:14:55.265Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD2018-01-02T10:14:55.285Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD2018-01-02T10:14:55.306Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent2018-01-02T10:14:55.345Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" }}2018-01-02T10:14:55.365Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCD2018-01-02T10:14:55.366Z c18d142e-efa5-11e7-8671-7f3af13c58c7 MESSAGE_ID ABCD2018-01-02T10:14:55.386Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent2018-01-02T10:14:55.406Z c18d142e-efa5-11e7-8671-7f3af13c58c7 push sent2018-01-02T10:14:55.445Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Dynamo Success: { "Item": { "Message": "ABCD" }}2018-01-02T10:14:55.445Z c18d142e-efa5-11e7-8671-7f3af13c58c7 data ABCDEND RequestId: c18d142e-efa5-11e7-8671-7f3af13c58c7REPORT RequestId: c18d142e-efa5-11e7-8671-7f3af13c58c7 Duration: 10010.15 ms Billed Duration: 10000 ms Memory Size: 128 MB Max Memory Used: 44 MB
2018-01-02T10:14:55.463Z c18d142e-efa5-11e7-8671-7f3af13c58c7 Task timed out after 10.01 seconds

解决方案

Looks like you are trying to get the item with key value "ind" : "ABC10" and notify using SNS.

In that case, the sns.publish should be called on successful of getItem i.e. instead of getmsg(MESSAGE_ID), the sns publish function should be called (i.e. you need to create one to publish the message) or sns publish code block should be executed (i.e. just copy and paste the sns publish code block inside the getItem else part).

I don't understand why you are calling the getmsg(err) (i.e. during error on getItem) or getmsg(MESSAGE_ID) (i.e. on success of getItem)? I think this is not correct. It is basically calling the getItem in a loop which causes time out error.

这篇关于使用Dynamodb和SNS的Lambda函数-超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 03:12