Google app script timeout ~ 5 minutes?

后端 未结 9 2031
粉色の甜心
粉色の甜心 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:38

    I have used the ScriptDB to save my place while processing a large amount of information in a loop. The script can/does exceed the 5 minute limit. By updating the ScriptDb during each run, the script can read the state from the db and pick up where it left off until all processing is complete. Give this strategy a try and I think you'll be pleased with the results.

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

    Anton Soradoi's answer seems OK but consider using Cache Service instead of storing data into a temporary sheet.

     function getRssFeed() {
       var cache = CacheService.getPublicCache();
       var cached = cache.get("rss-feed-contents");
       if (cached != null) {
         return cached;
       }
       var result = UrlFetchApp.fetch("http://example.com/my-slow-rss-feed.xml"); // takes 20 seconds
       var contents = result.getContentText();
       cache.put("rss-feed-contents", contents, 1500); // cache for 25 minutes
       return contents;
     }
    

    Also note that as of April 2014 the limitation of script runtime is 6 minutes.


    G Suite Business / Enterprise / Education and Early Access users:

    As of August 2018, max script runtime is now set to 30 minutes for these users.

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

    Also, try to minimize the amount of calls to google services. For example, if you want to change a range of cells in the spreadsheets, don't read each one, mutate it and store it back. Instead read the whole range (using Range.getValues()) into memory, mutate it and store all of it at once (using Range.setValues()).

    This should save you a lot of execution time.

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

    Quotas

    The maximum execution time for a single script is 6 mins / execution
    - https://developers.google.com/apps-script/guides/services/quotas

    But there are other limitations to familiarize yourself with. For example, you're only allowed a total trigger runtime of 1 hour / day, so you can't just break up a long function into 12 different 5 minute blocks.

    Optimization

    That said, there are very few reasons why you'd really need to take six minutes to execute. JavaScript should have no problem sorting thousands of rows of data in a couple seconds. What's likely hurting your performance are service calls to Google Apps itself.

    You can write scripts to take maximum advantage of the built-in caching, by minimizing the number of reads and writes. 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.
    - https://developers.google.com/apps-script/best_practices

    Batching

    The best thing you can possibly do is reduce the number of service calls. Google enables this by allowing batch versions of most of their API calls.

    As a trivial example, Instead of this:

    for (var i = 1; i <= 100; i++) {
      SpreadsheetApp.getActiveSheet().deleteRow(i);
    }
    

    Do this:

    SpreadsheetApp.getActiveSheet().deleteRows(i, 100);
    

    In the first loop, not only did you need 100 calls to deleteRow on the sheet, but you also needed to get the active sheet 100 times as well. The second variation should perform several orders of magnitude better than the first.

    Interweaving Reads and Writes

    Additionally, you should also be very careful to not go back and forth frequently between reading and writing. Not only will you lose potential gains in batch operations, but Google won't be able to use its built-in caching.

    Every time you do a read, we must first empty (commit) the write cache to ensure that you're reading the latest data (you can force a write of the cache by calling SpreadsheetApp.flush()). Likewise, every time you do a write, we have to throw away the read cache because it's no longer valid. Therefore if you can avoid interleaving reads and writes, you'll get full benefit of the cache.
    - http://googleappsscript.blogspot.com/2010/06/optimizing-spreadsheet-operations.html

    For example, instead of this:

    sheet.getRange("A1").setValue(1);
    sheet.getRange("B1").setValue(2);
    sheet.getRange("C1").setValue(3);
    sheet.getRange("D1").setValue(4);
    

    Do this:

    sheet.getRange("A1:D1").setValues([[1,2,3,4]]);
    

    Chaining Function Calls

    As a last resort, if your function really can't finish in under six minutes, you can chain together calls or break up your function to work on a smaller segment of data.

    You can store data in the Cache Service (temporary) or Properties Service (permanent) buckets for retrieval across executions (since Google Apps Scripts has a stateless execution).

    If you want to kick off another event, you can create your own trigger with the Trigger Builder Class or setup a recurring trigger on a tight time table.

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

    If you are using G Suite Business or Enterprise edition. You can register early access for App Maker after App maker enabled your script run runtime will increase run time from 6 minutes to 30 minutes :)

    More details about app maker Click here

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

    If you are using G Suite as a Business, Enterprise or EDU customer the execution time for running scripts is set to:

    30 min / execution

    See: https://developers.google.com/apps-script/guides/services/quotas

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