Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

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 ??

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

FYI - I've added an index - and I've found that the maximum number of API calls that I can make to SendGrid - is 10 in a 2 second window. In a 5 second window - I could likely get 25 . With the limitations on even a premium account - the highest number of emails I would be able to send - from the scripted server side scripting - is 300 per hour.

This seems rather limiting (to say the least). Advice?

Evgene Karachevtsev
Posts: 12
Joined: Mon Apr 28, 2014 1:12 pm

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

Hello Bruce,

For this purposes people usually buy dedicated server, so it will send e-mails only.
You can send portion by portion with current plan, or try to buy different one, or simply contact sales

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

Evgene,

thank you.

Is there someone in sales that you prefer to deal with ?

Bruce

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

Marina will get back to you shortly with more information on enterprise plan and higher limits.

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

Max, thank you. Look forward to hearing back. Initially, I believe my needs are low end on the database and requests side and most of what's described as a premium plan will suffice ourneeds. The outlier is the limit on execution time and the number of times I can call a scheduled script per hour. The processing needs are not high inside my script, but the script needs to do API calls to an external source ( SendGrid ), and those take time ( but not a lot of cpu or database ). So not sure if this is unique ... But it's high end on only one side of the equation ... Execution time ...

My client won't be able to afford the typical enterprise offering so I'm looking for something along the lines of a customized premium offering ....

Look forward to the chat though,
Bruce

Maryna Brodina
Posts: 0
Joined: Thu Apr 05, 2012 7:27 am

Error code SCSE018: Execution of the script has exceeded the allowed time and stopped

Hello Bruce!

Please send us a request on a href="mailto:support@appery.io" rel="nofollow"support@appery.io/a. We'll continue via email if you don't mind.

Return to “Issues”