Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exceeded maximum execution time in Google Apps Script [duplicate]

My google app script is iterating through the user's google drive files and copying and sometimes moving files to other folders. The script is always stopped after 5 minutes with no error message in the log.

I am sorting tens or sometimes thousands files in one run.

Are there any settings or workarounds?

like image 279
Martin V. Avatar asked Jan 22 '13 02:01

Martin V.


People also ask

How long can a Google script run for?

A single execution of Apps script can last no longer than 6 minutes and you're hitting this limit.

How do I make Google Apps Scripts faster?

Use batch operations Alternating read and write commands is slow. To speed up a script, read all data into an array with one command, perform any operations on the data in the array, and write the data out with one command.

How do I stop Google App script from running?

If you are in the code editor, and want to stop a script from running, you can click the "cancel" link in the toast display. Or you can click "View" -> "Executions" from the code editor and then terminate the script.

Is Google App script synchronous?

While Google Apps Script implements a subset of ECMAScript 5, there's nothing forcing it to be asynchronous. While it is true that JavaScript's major power is its asynchronous nature, the Google developers appear to have given that up in favor of a simpler, more straightforward API. UrlFetchApp methods are synchronous.


1 Answers

One thing you could do (this of course depends on what you are trying to accomplish) is:

  1. Store the necessary information (i.e. like a loop counter) in a spreadsheet or another permanent store(i.e. ScriptProperties).
  2. Have your script terminate every five minutes or so.
  3. Set up a time driven trigger to run the script every five minutes(or create a trigger programmatically using the Script service).
  4. On each run read the saved data from the permanent store you've used and continue to run the script from where it left off.

This is not a one-size-fit-all solution, if you post your code people would be able to better assist you.

Here is a simplified code excerpt from a script that I use every day:

function runMe() {   var startTime= (new Date()).getTime();      //do some work here      var scriptProperties = PropertiesService.getScriptProperties();   var startRow= scriptProperties.getProperty('start_row');   for(var ii = startRow; ii <= size; ii++) {     var currTime = (new Date()).getTime();     if(currTime - startTime >= MAX_RUNNING_TIME) {       scriptProperties.setProperty("start_row", ii);       ScriptApp.newTrigger("runMe")                .timeBased()                .at(new Date(currTime+REASONABLE_TIME_TO_WAIT))                .create();       break;     } else {       doSomeWork();     }   }      //do some more work here    } 

NOTE#1: The variable REASONABLE_TIME_TO_WAIT should be large enough for the new trigger to fire. (I set it to 5 minutes but I think it could be less than that).

NOTE#2: doSomeWork() must be a function that executes relatively quick( I would say less than 1 minute ).

NOTE#3 : Google has deprecated Script Properties, and introduced Properties Service in its stead. The function has been modified accordingly.

NOTE#4: 2nd time when the function is called, it takes the ith value of for loop as a string. so you have to convert it into an integer

like image 192
Anton Soradoi Avatar answered Oct 22 '22 04:10

Anton Soradoi