【问题标题】:How can I increase the 6 minute execution limit in Google Apps script?如何增加 Google Apps 脚本中的 6 分钟执行限制?
【发布时间】:2017-06-17 17:30:18
【问题描述】:

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

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

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

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

  • “不,没有其他方法可以增加 Google 应用脚本的 6 分钟执行时间限制。”
  • “是的,还有其他方法可以增加 6 分钟执行限制,这些方法就是……”

【问题讨论】:

  • 非常感谢您的回答,特别是澄清没有办法增加限制。还感谢您提供有关处理长时间运行脚本的信息。我有从 Google Doc 读取并创建大约 1500 行和 50 列的 Google 电子表格的脚本。我认为它不仅仅是可以保存在 PropertiesService 中的。我考虑过用户指定起点和要处理的数量的脚本,然后再次运行该脚本。所以我喜欢你计时的想法,并使用触发器重新启动。再次感谢您的回答,Peter 和 Vytautas。
  • 此评论应发布在答案上,而不是问题上。顺便看看How do comment @reply work?

标签: google-apps-script limit execution-time


【解决方案1】:

您可以使用名为 GASRetry 的库来解决此问题。

how to add GASRetry library to your project

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

  1. var thingies = 行更改为您要处理的任何内容。理想情况下,它应该是一个数组。
  2. //do our work here 行之后添加您自己的代码
  3. 在名为outerLoop() 的函数上设置一个触发器,使其每x 小时/天运行一次。可以将其重命名为对您有意义的名称,例如 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;
  }
}

【讨论】:

  • 澄清答案:没有办法增加时间限制。这个想法是存储你在循环中的位置,我们可以继续使用脚本吗?我确实敦促您不要使用此代码,而是将其植入脚本中,而是编写您自己的变体,因为这不是适合所有人的格式。我知道我的完全不同,因为我不能说每次迭代平均需要多长时间,这就是为什么我计算平均执行时间并将其与 6 分钟标记之前剩余的时间进行比较
【解决方案2】:

据我了解,对于 G Suite 组织,可以通过登录 Early Access Program 来消除最长执行时间限制。来自Quotas for Google Services

灵活配额抢先体验

此功能是抢先体验功能集的一部分。因此,目前只有部分开发人员可以访问它。

通常,如果脚本执行超出上述配额或限制之一,则脚本执行将停止并出现相应的错误 返回消息。这可能会将脚本的数据留在 一个不确定的状态。

在灵活配额制度下,此类硬配额限制被取消。 脚本在达到配额限制时不会停止。相反,它们是 延迟到配额可用,此时脚本 执行恢复。配额开始使用后,将在 固定利率。为了合理使用,脚本延迟很少见。

如果您无法访问 EAP,此站点上提供了几种解决方法。一些变通方法让脚本运行直到发生错误,然后在下一个要处理的项目上重新启动它,其他脚本只是将工作拆分到足够小的范围内以避免错误。

相关问答

【讨论】:

  • 申请延长时间的链接在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-10
相关资源
最近更新 更多