Google app script timeout ~ 5 minutes?

后端 未结 9 2045
粉色の甜心
粉色の甜心 2020-11-22 08:31

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 minute

相关标签:
9条回答
  • 2020-11-22 08:54

    Figure out a way to split up your work so it takes less than 6 minutes, as that's the limit for any script. On the first pass, you can iterate and store the list of files and folders in a spreadsheet and add a time-driven trigger for part 2.

    In part 2, delete each entry in the list as you process it. When there are no items in the list, delete the trigger.

    This is how I'm processing a sheet of about 1500 rows that gets spread to about a dozen different spreadsheets. Because of the number of calls to spreadsheets, it times out, but continues when the trigger runs again.

    0 讨论(0)
  • 2020-11-22 08:55

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

    0 讨论(0)
  • 2020-11-22 09:01

    If you're a business customer, you can now sign up for Early Access to App Maker, which includes Flexible Quotas.

    Under the flexible quota system, such hard quota limits are removed. Scripts do not stop when they reach a quota limit. Rather, they are delayed until quota becomes available, at which point the script execution resumes. Once quotas begin being used, they are refilled at a regular rate. For reasonable usage, script delays are rare.

    0 讨论(0)
提交回复
热议问题