I've looked at the previous post here on this topic.
Here's what I'm attempting to do - I have a group of emails that need to be sent. I need to use a server side script to do so (this is scheduled email traffic that I'm sending and the whole point of the app - is that the scheduled email goes out - even if you're in Jamica, your phone fell in the toilet, you turn it off at night , etc. ). In my example data - I have 18 emails to be sent with the SendGrid api .... yes this is an external API call , and yes, they do take a few moments to execute.
So when I run this script (it's not even close to fully developed yet).... it times out. The previous post here basically states I'm out of luck. Am I really out of luck? My app depends on being able to do this - and do it with potentially even larger numbers of emails (say every minute of so... here's the code):
( I can and will remove a lot of the logging - but somehow I still believe I have a challenge that won't go away easily ):
var responseBody = {},
requestParams = {},
paramKeys = request.keys();
for (var key = 0; key < paramKeys.length; key++) {
requestParams[paramKeys[key]] = request.get(paramKeys[key]);
}
responseBody.requestBody = request.body();
responseBody.requestParams = requestParams;
responseBody.method = request.method; // responseBody.method now contains string "GET" or "POST".
var susername = request.get('susername');
// code to do the work. Login to the database with the master key,
// and then - delete the requested rows...
var dbId = "14bad3d2-e20f-4698-b088-dc4a07e37595"; // the api key from the control panel
var smasterkey = '07facce5-2b15-4469-b3cf-7669df9c00d0' // the master key from the control panel
// set up the email here....
var emailbody = {
"subject": "if you get this email - we are lucky",
"text": "This is the text of a very lucky email",
"from": "a href="mailto:bruce.stuart@the-software-studio.com" rel="nofollow"bruce.stuart@the-software-studio.com/a",
"api_user": "bstuart3714",
"api_key": "code3714",
"to": "a href="mailto:support@the-software-studio.com" rel="nofollow"support@the-software-studio.com/a"
};
// login
try {
result = {};
var token = DatabaseUser.login(dbId, susername, null, smasterkey).sessionToken; //Login the user and save its sessionToken to the variable
result.token = token; //Save it token to the result too
} catch (e) {
console.log('there was an error: ' + e.message + ' Error ncode was:' + e.code);
}
console.log('made it past login');
// test code to send an email.....
// get a cursor with all the emails that need to be sent in the next 5 minutes...
var collectionName = "eMessages"; // the name of the messages collection
// set the query that you will use...
var sQuery = '';
var params = {};
// build the where clause here...
var dnow = new Date();
// find the time 5 minutes from now...
var din5 = fdatemath(dnow, 5, 0, 0, "+", 0, 0);
var stimein5 = dbDate(din5);
// stimein5 is now a queryable database time that can be used in a where clause...
sQuery = "{'dutcdatetimetosend': {$lte : '" + stimein5 + "'} }"
console.log(sQuery);
params.criteria = sQuery;
var ncounter = 0;
var nrows = 0;
var bhtml = true;
var semailtext = '';
var semailhtml = '';
// execute the query inside a try & catch loop
try {
queryResult = Collection.query(dbId, collectionName, params);
responseBody.numberofemailstosend = queryResult.length;
console.log('records with emails that need to be sent:' + queryResult.length);
// now iterate through the results - sending an email for each email that needs to be sent. For now - we're just sending to the 'to:' address - this will be changed as the script matures...
nrows = queryResult.length;
for (ncounter = 0; ncounter < nrows; ncounter++) {
// console.log('executing delete object:' + queryResult._id );
// the query results are queryResult[ncounter].yyyy - where yyy is the field name....
// set up the email body here....
bhtml = queryResult[ncounter].bhtml
// if (bhtml) {
// semailtext = '';
semailtext = queryResult[ncounter].smessage;
// } else {
// semailhtml = '';
// semailtext = queryResult[ncounter].smessage;
// }
emailbody = {
"subject": queryResult[ncounter].ssubject,
"text": semailtext,
"from": "a href="mailto:bruce.stuart@the-software-studio.com" rel="nofollow"bruce.stuart@the-software-studio.com/a",
"api_user": "bstuart3714",
"api_key": "code3714",
"replyto": "a href="mailto:bruce.stuart@the-software-studio.com" rel="nofollow"bruce.stuart@the-software-studio.com/a",
"to": queryResult[ncounter].stoaddresses
};
console.log('email body is: ' + JSON.stringify(emailbody) );
try {
var XHRResponse = XHR2.send("POST", "https://api.sendgrid.com/api/mail.sen...", {
"parameters": emailbody
});
//var resultObject = JSON.parse(XHRResponse.body); //Conevrting the //XHRResponse.body (which is string) in to the JSON object
console.log(XHRResponse.body);
console.log('email sent to :' + queryResult[ncounter].stoaddresses);
// console.log( resultObject );
} catch (e) {
console.log('could not send request to sendGrid');
console.log('there was an error: ' + e.message + ' Error ncode was:' + e.code);
}
};
} catch (e) {
console.log('there was an error in the query: ' + e.message + ' Error ncode was:' + e.code);
}
//console.log('ready to return results')
console.log(responseBody);
response.success(responseBody, "application/json");
//DatabaseUser.logout(dbId, token);
//console.log('logged out of db & script complete');
What's your suggestion ?? Change plans? Add an index ?? or ??