如何增加 Google Apps 脚本中的 6 分钟执行限制? [英] How can I increase the 6 minute execution limit in Google Apps script?

查看:17
本文介绍了如何增加 Google Apps 脚本中的 6 分钟执行限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以增加 Google Apps 脚本的 6 分钟执行时间限制?我认为答案可能是 G Business Suite 的抢先体验计划.如果我能加入 Early Access 计划,我可能愿意每月为 G Business Suite 支付 10 美元,以便将执行限制从 6 分钟增加到 30 分钟.但 G Suite 帮助论坛的一位顾问表示,抢先体验是一个有限的计划,这意味着我无法保证我能得到它.

Is there any way to increase the 6 minute execution time limit for Google Apps Scripts? I thought that the answer might be the Early Access program for G Business Suite. I might be willing to pay $10 a month for G Business Suite, if I could get into the Early Access program, in order to increase the execution limit from 6 minutes to 30 minutes. But an Advisor from the G Suite Help Forum said that Early Access is a limited program, which means that there is no guarantee that I could get that.

有没有其他方法可以增加 6 分钟的执行限制?

Is there any other way to increase the 6 minute execution limit?

请注意,在这个问题中,我不是在询问如何优化我的脚本以适应 6 分钟限制的想法.(我将来可能会问,如果当前问题的答案是不,没有其他办法.")

Please note that in this question I am not asking for ideas of how to optimize my scripts to fit within the 6 minute limit. (I may ask that in the future, if the answer to this current question is "No, there is no other way.")

因此,对当前问题的适当回答是:

Therefore, appropriate answers to this current question are:

  • 不,没有其他方法可以增加 Google App Scripts 的 6 分钟执行限制."
  • 是的,还有其他增加 6 分钟执行限制的方法,这些方法是..."

推荐答案

您可以使用名为 GASRetry 的库来解决.

You can workaround by using library called GASRetry.

请参阅如何将 GASRetry 库添加到您的项目.

See how to add GASRetry library to your project.

代码更改指南(您需要将其应用于您自己的特定场景):

A guide to code changes (you need to apply it to your own specific scenario):

  1. 将行 var thingies = 更改为您想要处理的任何内容.理想情况下,它应该是一个数组.
  2. //do our work here 行之后添加您自己的代码
  3. 在名为 outerLoop() 的函数上设置一个触发器,每 x 小时/天运行一次.可以将其重命名为对您有意义的名称,例如 doProcessWidgets().
  1. Change the line var thingies = to whatever you want to process. It should ideally be an array.
  2. Add your own code after the //do our work here line
  3. Set a Trigger on the function named outerLoop() to run every x hours/days. It's OK to rename it to something meaningful to you like doProcessWidgets().

代码:

//automatically invoked from outerLoop()'s creation of a new trigger if required to get work done
function outerLoopRepeating() {
  outerLoop();
}
// trigger this function
function outerLoop() {
  try {
    var processingMessage = 'Initialising', isOverMaxRuntime = false, startTime = new Date(), // calc elapsed time
        functionName = arguments.callee.name, repeatingFunctionName = functionName + 'Repeating'; //for logging, triggering
    
    // Deletes all occurrences of the Repeating trigger we don't end up with undeleted time based triggers all over the place
    //add library GASRetry MGJu3PS2ZYnANtJ9kyn2vnlLDhaBgl_dE
    GASRetry.call(function(){ScriptApp.getProjectTriggers().forEach(function(i) {
      if (i.getHandlerFunction() === repeatingFunctionName) {ScriptApp.deleteTrigger(i);}
    });});
    
    Logger.log('========== Starting the "%s" function ==========', functionName);
    
    // Handle max execution times in our outer loop
    // Get start index if we hit max execution time last run
    var start = parseInt(PropertiesService.getScriptProperties().getProperty(functionName + "-start")) || 0;
    
    var thingies = ['stuff to process', 'in an Array',,,,]; //
    for (var i = start ; i < thingies.length; i++) {
      if (Math.round((new Date() - startTime)/1000) > 300) { //360 seconds is Google Apps Script max run time
        //We've hit max runtime. 
        isOverMaxRuntime = true;
        break;
      }
      //do our work here
      Logger.log('Inside the for loop that does the xyz work. i is currently: %d', i);
      var processingMessage = Utilities.formatString('%d of %d thingies: %s <%s>',  i+1, thingies.length, thingyName, thingyId);
      
      //do our work above here
    }
    if (isOverMaxRuntime) {
      //save state in user/project prop if required
      PropertiesService.getScriptProperties().setProperty(functionName + '-start', i);
      //create another trigger
      GASRetry.call(function(){ScriptApp.newTrigger(repeatingFunctionName).timeBased().everyMinutes(10).create();});
      Logger.log('Hit max run time - last iteration completed was i=%s', i-1);
    } else {
      Logger.log('Done all the work and all iterations');
      PropertiesService.getScriptProperties().deleteProperty(functionName + '-start');
      Logger.log('Completed processing all %s things with the "%s" function', thingies.length, functionName);
    }
  } catch (e) {
    Logger.log('%s. While processing %s', JSON.stringify(e, null, 2), processingMessage);
    throw e;
  }
}

这篇关于如何增加 Google Apps 脚本中的 6 分钟执行限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆