Do google apps scripts have a built in mechanism for preventing long loops that it suspects may be programming error? The reason I'm asking is that I have a google app script I'm creating for my gmail to keep it under control. To avoid the maximum execution time error, I'm only cleaning up 100 emails at a time and then starting the function over using a time based trigger, which will continue until there are no more emails to clean up. I have a lot of mail to clean up, and the script runs about 7 times or so, and then it just stops. I have the code set up to email me at every possible fail or end point but I don't get an email, it just stops. When I look at the triggers for the script, they go every minute or so as expected until about the 7th - 9th one and then they just stop. Here is my code, but I don't think it is a code issue.
function runcleanup() {
var unreadthreads = 'label:all label:unread -has:red-star older_than:7d',
archivethreads = 'label:inbox -has:red-star older_than:14d',
threads,
execute;
try {
threads = GmailApp.search(unreadthreads, 0, 100);
Logger.log(threads.length);
if (threads.length) {
execute = markasread(threads);
ScriptApp.newTrigger("runcleanup")
.timeBased()
.at(new Date((new Date()).getTime() + 1000 * 15))
.create();
} else {
GmailApp.sendEmail(Session.getActiveUser().getEmail(),
"troubleshooting script",
"threads = " + threads.length);
}
} catch (e) {
GmailApp.sendEmail(Session.getActiveUser().getEmail(),
"troubleshooting script ERROR",
"error = " + e);
}
}
function markasread(threads) {
if (!threads) {
return false;
}
for (var i = 0; i < threads.length; i++) {
threads[i].markRead();
}
return true;
}
function archive(threads, maxDate, delayread) {
if (!threads) {
return false;
}
for (var i = 0; i < threads.length; i++) {
threads[i].moveToArchive();
}
return true;
}