AWS Lambda - MongoDB resource optimization - mongodb

I'm building facebook chatbot using AWS Lambda and MongoDB. At the moment, my application is pretty simple but I'm trying to nail down the basics before I move onto the complex stuff.
I understand AWS Lambda is stateless but I've read adding below line in handler along with variables initialized outside handler, I don't have to establish DB connection on every request.
context.callbackWaitsForEmptyEventLoop = false;
(I've read this from this article; https://www.mongodb.com/blog/post/optimizing-aws-lambda-performance-with-mongodb-atlas-and-nodejs)
I'm adding my entire code below
'use strict'
const
axios = require('axios'),
mongo = require('mongodb'),
MongoClient = mongo.MongoClient,
assert = require('assert');
var VERIFY_TOKEN = process.env.VERIFY_TOKEN;
var PAGE_ACCESS_TOKEN = process.env.PAGE_ACCESS_TOKEN;
var MONGO_DB_URI = process.env.MONGO_DB_URI;
let cachedDb = null;
let test = null;
exports.handler = (event, context, callback) => {
var method = event.context["http-method"];
context.callbackWaitsForEmptyEventLoop = false;
console.log("test :: " + test);
if (!test) {
test = "1";
}
// process GET request --> verify facebook webhook
if (method === "GET") {
var queryParams = event.params.querystring;
var rVerifyToken = queryParams['hub.verify_token']
if (rVerifyToken === VERIFY_TOKEN) {
var challenge = queryParams['hub.challenge'];
callback(null, parseInt(challenge))
} else {
var response = {
'body': 'Error, wrong validation token',
'statusCode': 403
};
callback(null, response);
}
// process POST request --> handle message
} else if (method === "POST") {
let body = event['body-json'];
body.entry.map((entry) => {
entry.messaging.map((event) => {
if (event.message) {
if (!event.message.is_echo && event.message.text) {
console.log("BODY\n" + JSON.stringify(body));
console.log("<<MESSAGE EVENT>>");
// retrieve message
let response = {
"text": "This is from webhook response for \'" + event.message.text + "\'"
}
// facebook call
callSendAPI(event.sender.id, response);
// store in DB
console.time("dbsave");
storeInMongoDB(event, callback);
}
} else if (event.postback) {
console.log("<<POSTBACK EVENT>>");
} else {
console.log("UNHANDLED EVENT; " + JSON.stringify(event));
}
})
})
}
}
function callSendAPI(senderPsid, response) {
console.log("call to FB");
let payload = {
recipient: {
id: senderPsid
},
message: response
};
let url = `https://graph.facebook.com/v2.6/me/messages?access_token=${PAGE_ACCESS_TOKEN}`;
axios.post(url, payload)
.then((response) => {
console.log("response ::: " + response);
}).catch(function(error) {
console.log(error);
});
}
function storeInMongoDB(messageEnvelope, callback) {
console.log("cachedDB :: " + cachedDb);
if (cachedDb && cachedDb.serverConfig.isConnected()) {
sendToAtlas(cachedDb.db("test"), messageEnvelope, callback);
} else {
console.log(`=> connecting to database ${MONGO_DB_URI}`);
MongoClient.connect(MONGO_DB_URI, function(err, db) {
assert.equal(null, err);
cachedDb = db;
sendToAtlas(db.db("test"), messageEnvelope, callback);
});
}
}
function sendToAtlas(db, message, callback) {
console.log("send to Mongo");
db.collection("chat_records").insertOne({
facebook: {
messageEnvelope: message
}
}, function(err, result) {
if (err != null) {
console.error("an error occurred in sendToAtlas", err);
callback(null, JSON.stringify(err));
} else {
console.timeEnd("dbsave");
var message = `Inserted a message into Atlas with id: ${result.insertedId}`;
console.log(message);
callback(null, message);
}
});
}
I did everything as instructed and referenced a few more similar cases but somehow on every request, "cachedDb" value is not saved from previous request and the app is establishing the connection all over again.
Then I also read that there is no guarantee the Lambda function is using the same container on multiple requests so I made another global variable "test". "test" variable value is logged "1" from the second request which means it's using the same container but again, "cachedDb" value is not saved.
What am I missing here?
Thanks in advance!

In short AWS Lambda function is not a permanently running service of any kind.
So, far I know AWS Lambda works on idea - "one container processes one request at a time".
It means when request comes and there is available running container for the Lambda function AWS uses it, else it starts new container.
If second request comes when first container executes Lambda function for first request AWS starts new container.
and so on...
Then there is no guarantee in what container (already running or new one) Lambda function will be executed, so... new container opens new DB connection.
Of course, there is an inactivity period and no running containers will be there after that. All will start over again by next request.

Related

Issue Connecting to MongoDB collections

I am using axios and express.js API to connect to my mongo DB. I have a .get() request that works for one collection and doesn't work for any other collection. This currently will connect to the database and can access one of the collections called users. I have another collection setup under the same database called tasks, I have both users and tasks setup the same way and being used the same way in the code. The users can connect to the DB (get, post) and the tasks fails to connect to the collection when calling the get or the post functions. When viewing the .get() API request in the browser it just hangs and never returns anything or finishes the request.
any help would be greatly appreciated!
The project is on GitHub under SCRUM-150.
API connection
MONGO_URI=mongodb://localhost:27017/mydb
Working
methods: {
//load all users from DB, we call this often to make sure the data is up to date
load() {
http
.get("users")
.then(response => {
this.users = response.data.users;
})
.catch(e => {
this.errors.push(e);
});
},
//opens delete dialog
setupDelete(user) {
this.userToDelete = user;
this.deleteDialog = true;
},
//opens edit dialog
setupEdit(user) {
Object.keys(user).forEach(key => {
this.userToEdit[key] = user[key];
});
this.editName = user.name;
this.editDialog = true;
},
//build the alert info for us
//Will emit an alert, followed by a boolean for success, the type of call made, and the name of the
//resource we are working on
alert(success, callName, resource) {
console.log('Page Alerting')
this.$emit('alert', success, callName, resource)
this.load()
}
},
//get those users
mounted() {
this.load();
}
};
Broken
methods: {
//load all tasks from DB, we call this often to make sure the data is up to date
load() {
http
.get("tasks")
.then(response => {
this.tasks = response.data.tasks
})
.catch(e => {
this.errors.push(e);
});
},
//opens delete dialog
setupDelete(tasks) {
this.taskToDelete = tasks;
this.deleteDialog = true;
},
//opens edit dialog
setupEdit(tasks) {
Object.keys(tasks).forEach(key => {
this.taskToEdit[key] = tasks[key];
});
this.editName = tasks.name;
this.editDialog = true;
},
//build the alert info for us
//Will emit an alert, followed by a boolean for success, the type of call made, and the name of the
//resource we are working on
alert(success, callName, resource) {
console.log('Page Alerting')
this.$emit('alert', success, callName, resource)
this.load()
}
},
//get those tasks
mounted() {
this.load();
}
};
Are you setting any access controls in the code?
Also refer to mongoDB's documentation here:
https://docs.mongodb.com/manual/core/collection-level-access-control/
Here is my solution:
In your app.js, have this:
let mongoose = require('mongoose');
mongoose.connect('Your/Database/Url', {
keepAlive : true,
reconnectTries: 2,
useMongoClient: true
});
In your route have this:
let mongoose = require('mongoose');
let db = mongoose.connection;
fetchAndSendDatabase('yourCollectionName', db);
function fetchAndSendDatabase(dbName, db) {
db.collection(dbName).find({}).toArray(function(err, result) {
if( err ) {
console.log("couldn't get database items. " + err);
}
else {
console.log('Database received successfully');
}
});
}

Sails.js can't access data model in the middleware: Unexpected token

sails.js newbie here.
I can't access my User model within my middleware. It says unexpected token.
Here's my middleware,
isAuthenticated: (function(){
return function authHandler(req, res, next) {
let payload;
try {
payload = decode(req);
let expTime = moment.tz(payload.exp, 'GMT').toDate();
let currentTIme = moment.tz('GMT').toDate();
if (currentTIme > expTime) {
return res.status(401).json({message: 'JWT token expired.'});
} else {
>> const user = await User.findOne({id: payload.id});
if (user) {
req.payload = {
userId: user.id
};
return next()
} else {
return res.status(401).json({message: 'User doesn\'t exist.'});
}
}
} catch (err) {
return res.serverError();
}
}
})()
}
I am trying to setup a authentication middleware. In my global settings models is set to true.
I tried, sails.models.user but even for that I get unexpected token.
You need to put the async keyword, async function(..){....await....}.
The await keyword is only valid inside async functions.

Angular2 Stripe integration stripeResponseHandler cannot access this

I'm integrating Stripe payments with Angular2 (actually Ionic but the code is the same)
the call to Stripe.card.createToken is successful and returns a token
but in stripeResponseHandler which is an async callback, I cannot access any of the "this" variables. for example I cannot set this.amount = 10 and I cannot call this._http.post
how can I access the "this" variables ? I'm trying to http post the token and the amount to an API to make the payment
constructor(private _navController: NavController,
private _http: Http) { }
submitPayment() {
Stripe.setPublishableKey(this.key);
this.card = new Card();
this.card.number = this.cardNumber;
this.card.cvc = this.cardCVC;
this.card.exp_month = this.cardExpMonth;
this.card.exp_year = this.cardExpYear;
this.card.address_zip = this.cardAddressZip;
try {
Stripe.card.createToken(this.card, this.stripeResponseHandler);
}
catch (e) {
alert(e.message);
}
// Prevent the form from being submitted:
return false;
}
stripeResponseHandler(status, response) {
if (response.error) { // Problem!
alert(response.error);
} else { // Token was created!
// Get the token ID:
alert(response.id);
try {
this.amount = 10;
let payment = new Payment();
payment.token = response.id;
payment.amount = this.amount;
let body = JSON.stringify(payment);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this._http.post(this.url, body, options)
.map(res => res.json())
.catch(this.handleError);
}
catch (e) {
alert(e.message);
}
}
}
handleError(error: Response) {
// may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.error(error);
alert('error' + error.text + " " + error.statusText);
return Observable.throw(error.json().error || 'Server error');
}
If you just pass the function reference, then JavaScript doesn't keep the this reference. You have to take care of this explicitely:
Instead of
Stripe.card.createToken(this.card, this.stripeResponseHandler);
use
Stripe.card.createToken(this.card, (status, person) => this.stripeResponseHandler(status, person));
See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions
or
Stripe.card.createToken(this.card, this.stripeResponseHandler.bind(this));

Issue with the submitAdapterAuthentication() method of the ChallengeHandler in MobileFirst v.6.3

We have an issue with the submitAdapterAuthentication() method of the ChallengeHandler in IBM MobileFirst v.6.3.
We assign callback functions to the properties 'onSuccess' and 'onFailure' in the options object.
We then provide the options object to submitAdapterAuthentication(invocationData, options) and execute it.
var ch = WL.Client.createChallengeHandler(securityTest);
//////////////////
function login (user, pass) {
tempUser = {username: user, password: pass};
userObj.user = user;
var auth = "Basic " + window.btoa(user + ":" + pass);
var invocationData = {
parameters: [auth, user],
adapter: "SingleStepAuthAdapter",
procedure: "submitLogin"
};
var options = {
onSuccess: iWon,
onFailure: iLost,
invocationContext: {invocationData: invocationData},
timeout: 10000
};
ch.submitAdapterAuthentication(invocationData, options);
});
function iWon(response) {
WL.Logger.debug('Login success! Response: ' + JSON.stringify(response));
//update user info, as somehow isUserAuthenticated return false without it
WL.Client.updateUserInfo(function(response) {
WL.Logger.debug('Updated User Info success! Response: ' + JSON.stringify(response));
});
}
function iLost(response) {
WL.Logger.debug('ERROR. Login failed! Response: ' + JSON.stringify(response));
}
Neither the onSuccess (iWon) or the onFailure (iLost) is called after executing submitAdapterAuthentication(invocationData, options).
How do we know if the authentication was successful?
Which options, events, callbacks or promises shall we look for and use?
We have also posted the issue here:
submitAdapterAuthentication not working
You are missing the definition of the functions
ch.isCustomResponse = function(response){...}
ch.handleChallenge = function(response){...}
Your code should look more like this
var ch = WL.Client.createChallengeHandler(securityTest);
ch.isCustomResponse = function(response) {
if (!response||!response.responseJSON||response.responseText === null) {
return false;
}
if (typeof(response.responseJSON.authRequired) !== 'undefined'){
return true;
} else {
return false;
}
};
ch.handleChallenge = function(response){
var authRequired = response.responseJSON.authRequired;
if (authRequired == true){
// handle the case when authentication is needed, i.e., show login form etc.
if (response.responseJSON.errorMessage) {
// authentication failed, show a message to the user indicating what went wrong
// call the login failed function or move it's contents here
iLost(response);
}
} else if (authRequired == false){
// no authentication is needed
ch.submitSuccess();
// call the login success function or move it's contents here
iWon(response);
}
};
//////////////////
function login (user, pass) {
tempUser = {username: user, password: pass};
userObj.user = user;
// is the first parameter expected by submitLogin the username or the
// Basic Authentication encoded string ???
var auth = "Basic " + window.btoa(user + ":" + pass);
var invocationData = {
parameters: [auth, user],
adapter: "SingleStepAuthAdapter",
procedure: "submitLogin"
};
ch.submitAdapterAuthentication(invocationData, {});
});
function iWon(response) {
WL.Logger.debug('Login success! Response: ' + JSON.stringify(response));
//update user info, as somehow isUserAuthenticated return false without it
WL.Client.updateUserInfo(function(response) {
WL.Logger.debug('Updated User Info success! Response: ' + JSON.stringify(response));
});
}
function iLost(response) {
WL.Logger.debug('ERROR. Login failed! Response: ' + JSON.stringify(response));
}
For more information on adapter-based authentication visit http://www-01.ibm.com/support/knowledgecenter/SSHS8R_6.3.0/com.ibm.worklight.dev.doc/devref/t_adapter_based_authenticator.html?lang=en
You should also check the getting started module on adapter-based authentication for hybrid applications https://developer.ibm.com/mobilefirstplatform/documentation/getting-started-6-3/authentication-security/adapter-based-authentication/adapter-based-authentication-hybrid-applications/

node.js socket exception read ETIMEDOUT - how do I catch it properly? what about write timeouts?

I have a proxy server that manages a bunch of clients and also talks with another http server. Messages get posted back and forth and directed to clients. Clients can and do timeout, and the server has a heartbeat function (that repeats every n seconds) that sends a heartbeat to all clients that are in a map of clientId to socket connection.
I get a 'read ETIMEDOUT' exception when the heartbeat tries to talk to a client that is no longer connected but who's socket is still active. I tried temporarily setting the timeout of the socket connection to 2000ms with the theory that my socket event handler for timeout would catch this (the event handler is in the tcp server portion), but this didn't happen. It takes several heartbeats to die.
Part of the problem is definitely my lack of understanding of how to structure node.js code, so if you have any suggestions, I'd very much appreciate them.
Another question is whether it is possible to handle read and write timeouts separately, or at least break them out. What I'd really like to do is have my heartbeat function be part of the tcp server and only send a heartbeat if it hasn't heard from the client in say n seconds, and only send this heartbeat once. If we get a timeout, then we kill the socket, otherwise we wait again.
Thanks!
>>$ node --harmony-weakmaps server.js
Heartbeat: Sat Feb 18 2012 08:34:40 GMT+0000 (UTC)
{
sending keep_alive to id:00:00:00:00:00:10 socket:[object Object]
}
socket:received data: {"id":"00:00:00:00:00:10","m":"keep_alive","success":"true"}
Heartbeat: Sat Feb 18 2012 08:35:40 GMT+0000 (UTC)
{
sending keep_alive to id:00:00:00:00:00:10 socket:[object Object]
}
socket:received data: {"id":"00:00:00:00:00:10","m":"keep_alive","success":"true"}
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: read ETIMEDOUT
at errnoException (net.js:642:11)
at TCP.onread (net.js:375:20)
Heartbeat function that triggers the timeout:
console.log("Starting heartbeat");
var beat_period = 60;
setInterval(function() {
if(Object.keys(id2socket).length != 0){
console.log("Heartbeat: " + new Date());
//for (var key in id2socket) {
// console.log("\t"+key+"->"+id2socket[key]);
//}
console.log("{");
for(var id in id2socket) {
var socket = id2socket[id];
// don't want sockets to time out
socket.setTimeout(2000); // for heartbeat, set the timeout
try {
console.log("\tsending keep_alive to id:"+id+" socket:"+id2socket[id]);
socket.write('{"m":"keep_alive"}\r\n');
} catch(Error) {
console.log("Heartbeat:Cannot find id:"+id);
removeSocketFromMap(id,socket);
// TODO: send message to API
}
socket.setTimeout(0); // no timeout
}
console.log("}");
}
}, beat_period * 1000);
server.js:
// Launch Instructions
// node --harmony-weakmaps server.js
var net = require('net'); // tcp-server
var http = require("http"); // http-server
var querystring = require('querystring');
// Map of sockets to clients
var id2socket = new Object;
var socket2id = new WeakMap; // allows us to use object as key to hash
// Test for client:
// {"id":"123","m":"add"} // establishes connection and puts client into id2socket map
// {"id":"123","m":"test"} // sends a message through to API
// HTTP:POST outbound function
// http://www.theroamingcoder.com/node/111
function postOut(dataToPost){
try{
console.log("postOut msg:"+JSON.stringify(dataToPost));
} catch (Error) {
console.log("postOut error:"+Error);
}
var post_domain = '127.0.0.1';
var post_port = 80;
var post_path = '/cgi-bin/index3.py';
var post_data = querystring.stringify({
'act' : 'testjson',
'json' : JSON.stringify(dataToPost)
});
console.log("post_data:"+post_data);
var post_options = {
host: post_domain,
port: post_port,
path: post_path,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response:data: ' + chunk);
});
});
// Handle various issues
post_req.on('error', function(error) {
console.log('ERROR' + error.message);
// If you need to go on even if there is an error add below line
//getSomething(i + 1);
});
post_req.on("response", function (response) {
console.log("Response:response:"+response);
});
// write parameters to post body
post_req.write(post_data);
post_req.end();
}
function removeSocketFromMap(id,socket){
console.log("removeSocketFromMap socket:"+socket+" id:"+id);
delete id2socket[id];
socket2id.delete(socket);
//TODO: print map???
console.log("socketmap {");
for (var key in id2socket) {
console.log("\t"+key+"->"+id2socket[key]);
}
console.log("}");
}
// Setup a tcp server
var server_plug = net.createServer(
function(socket) {
// Event handlers
socket.addListener("connect", function(conn) {
console.log("socket:connection from: " + socket.remoteAddress + ":" + socket.remotePort + " id:"+socket.id );
});
socket.addListener("data", function(data) {
console.log("socket:received data: " + data);
var request = null;
try {
request = JSON.parse(data);
} catch (SyntaxError) {
console.log('Invalid JSON:' + data);
socket.write('{"success":"false","response":"invalid JSON"}\r\n');
}
if(request!=null){
response = request; // set up the response we send back to the client
if(request.m=="keep_alive"){ // HACK for keep alive
// Do nothing
} else if(request.m !== undefined && request['id'] !== undefined){ // hack on 'id', id is js obj property
if(request.m == 'connect_device' || request.m == 'add'){
console.log("associating uid " + request['id'] + " with socket " + socket);
id2socket[request['id']] = socket;
socket2id.set(socket, request['id']);
}
postOut(request);
socket.write(JSON.stringify(response)+"\r\n");
} else if(request['id'] !== undefined){
postOut(request);
socket.write(JSON.stringify(response)+"\r\n");
} else {
response['content'] = "JSON doesn't contain m or id params";
socket.write(JSON.stringify(response)+"\r\n");
}
} else {
console.log("null request");
}
});
socket.on('end', function() {
id = socket2id.get(socket);
console.log("socket:disconnect by id " + id);
removeSocketFromMap(id,socket);
socket.destroy();
});
socket.on('timeout', function() {
id = socket2id.get(socket);
console.log('socket:timeout by id ' + id);
removeSocketFromMap(id,socket);
socket.destroy();
});
// handle uncaught exceptions
socket.on('uncaughtException', function(err) {
id = socket2id.get(socket);
console.log('socket:uncaughtException by id ' + id);
removeSocketFromMap(id,socket);
socket.destroy();
});
}
);
server_plug.on('error', function (error) {
console.log('server_plug:Error: ' + error);
});
// Setup http server
var server_http = http.createServer(
// Function to handle http:post requests, need two parts to it
// http://jnjnjn.com/113/node-js-for-noobs-grabbing-post-content/
function onRequest(request, response) {
request.setEncoding("utf8");
request.content = '';
request.on('error', function(err){
console.log("server_http:error: "+err);
})
request.addListener("data", function(chunk) {
request.content += chunk;
});
request.addListener("end", function() {
console.log("server_http:request_received");
try {
var json = querystring.parse(request.content);
console.log("server_http:received_post {");
for(var foo in json){
console.log("\t"+foo+"->"+json[foo]);
}
console.log("}");
// Send json message content to socket
if(json['json']!=null && json['id']!=null){
id = json['id'];
try {
var socket = id2socket[id];
socket.write(json['json']+"\r\n");
} catch (Error) {
console.log("Cannot find socket with id "+id);
} finally {
// respond to the incoming http request
response.end();
// TODO: This should really be in socket.read!
}
}
} catch(Error) {
console.log("JSON parse error: "+Error)
}
});
request.on('end', function () {
console.log("http_request:end");
});
request.on('close', function () {
console.log("http_request:close");
});
}
);
server_http.on('error', function (error) {
console.log('server_http:Error: ' + error);
});
// Heartbeat function
console.log("Starting heartbeat");
var beat_period = 60;
setInterval(function() {
if(Object.keys(id2socket).length != 0){
console.log("Heartbeat: " + new Date());
//for (var key in id2socket) {
// console.log("\t"+key+"->"+id2socket[key]);
//}
console.log("{");
for(var id in id2socket) {
var socket = id2socket[id];
// don't want sockets to time out
socket.setTimeout(2000); // for heartbeat, set the timeout
try {
console.log("\tsending keep_alive to id:"+id+" socket:"+id2socket[id]);
socket.write('{"m":"keep_alive"}\r\n');
} catch(Error) {
console.log("Heartbeat:Cannot find id:"+id);
removeSocketFromMap(id,socket);
// TODO: send message to API
}
socket.setTimeout(0); // no timeout
}
console.log("}");
}
}, beat_period * 1000);
// Fire up the servers
//var HOST = '127.0.0.1'; // just local incoming connections
var HOST = '0.0.0.0'; // allows access to all external IPs
var PORT = 5280;
var PORT2 = 9001;
// accept tcp-ip connections
server_plug.listen(PORT, HOST);
console.log("TCP server listening on "+HOST+":"+PORT);
// accept posts
server_http.listen(PORT2);
console.log("HTTP server listening on "+HOST+":"+PORT2);
EDIT:
Should I be using .on(event,callback) vs .onlistener(event,callback)?
UPDATE:
That didn't work, I changed the stuff in tcp_server to all add_listener in the heartbeat as .on. Still didn't catch the errors and blew up and said I added too many listeners.
First, its a bit hard to say if your structure is right without some more understanding of the context of your code...
Try adding
socket.on('error', function() {
id = socket2id.get(socket);
console.log('socket:timeout by id ' + id);
removeSocketFromMap(id,socket);
socket.destroy();
}
to the anonymous function in net.CreateServer. ETIMEDOUT is a system call error, and node.js is just reporting it. It may not be caused by just a typical 'timeout'. You say its being caused by the Hearbeat write, but it looks like TCP.read is the origination. It may be a half-closed socket.
For the ETIMEDOUT exception issue, did you try listening for an uncaughtException on the process itself?
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});
See the documentation here : http://nodejs.org/docs/latest/api/process.html#event_uncaughtException_