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

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

问题描述

有没有办法增加Google Apps脚本的6分钟执行时间限制?我认为答案可能是G Business Suite的Early Access计划。为了将执行限制从6分钟提高到30分钟,我可能愿意为G Business Suite支付每月10美元的费用(如果我可以进入Early Access计划的话)。但是G Suite帮助论坛的一位顾问表示,Early Access是一个有限的程序,这意味着我不能保证我能得到那个。

有没有其他的如何增加6分钟执行限制?

请注意,在这个问题中,我没有要求如何优化脚本以适应6分钟的限制。 (我可能会问,在未来,如果对当前问题的回答是不,没有其他方式。)

因此,适当的回答这个电流问题是:


  • 不,没有其他方法可以提高Google App脚本的6分钟执行限制。

  • 是的,还有其他方法可以增加6分钟的执行限制,这些方式是......


解决方案

我有几十个工作需要6分钟才能完成。我使用了多年来我调整过的outerloop脚本(下图)。无论需要多长时间,它都可以完成每项工作。



安装程序(只是一个指南 - 您需要将它应用于您自己的特定场景):


  1. 将行 var thingies = 更改为您要处理的任何内容。它应该是一个数组。

  2. 之后添加自己的代码//在这里完成我们的工作 line

  3. 在名为 outerLoop()的函数上设置一个触发器,以每x个小时/天运行一次。可以将它重命名为对您有意义的内容,如 doProcessWidgets()

代码:

  //自动调用outerLoop()创建新触发器(如果需要完成工作)
函数outerLoopRepeating(){
outerLoop();

//触发此函数
函数outerLoop(){
try {
var processingMessage ='初始化',isOverMaxRuntime = false,startTime = new Date() ,//计算经过时间
functionName = arguments.callee.name,repeatingFunctionName = functionName +'Repeating'; //用于记录,触发

//删除所有出现的重复触发器我们不会以遍布该位置的未取消删除的基于时间的触发器为结束
//添加库GASRetry MGJu3PS2ZYnANtJ9kyn2vnlLDhaBgl_dE
GASRetry.call(function(){ScriptApp.getProjectTriggers()。forEach(function(i){
if(i.getHandlerFunction()=== repeatingFunctionName){ScriptApp.deleteTrigger(i);}
});});

Logger.log('==========启动%s函数==========',functionName);

//在外部循环中处理最大执行时间
//如果我们按最后执行时间的最大执行时间获取开始索引
var start = parseInt(PropertiesService.getScriptProperties()。 getProperty(functionName +-start))|| 0;

var thingies = ['stuff to process','in a Array',,,,]; ((新的日期() - 开始时间)/ 1000)>(300)/($ i = / 360秒是Google Apps脚本最大运行时间
//我们点击了最大运行时间。
isOverMaxRuntime = true;
休息;

//在这里完成我们的工作
Logger.log('在for循环中执行xyz工作,我当前是:%d',i);
var processingMessage = Utilities.formatString('%d%s%s:%s<%s>',i + 1,thingies.length,thingyName,thingyId);

//在这里完成我们的工作

if(isOverMaxRuntime){
//保存user / project prop中的状态(如果需要的话)
PropertiesService。 getScriptProperties()。setProperty(functionName +'-start',i);
//创建另一个触发器
GASRetry.call(function(){ScriptApp.newTrigger(repeatingFunctionName).timeBased()。everyMinutes(10).create();});
Logger.log('Hit max run time - last iteration completed is i =%s',i-1);
} else {
Logger.log('完成所有工作和所有迭代');
PropertiesService.getScriptProperties()。deleteProperty(functionName +'-start');
Logger.log('用'%s'函数',thingies.length,functionName'完成处理所有%s的事情';
}
} catch(e){
Logger.log('%s。While processing%s',JSON.stringify(e,null,2),processingMessage);
throw e;
}
}


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.

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

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:

  • "No, there are no other ways to increase the 6 minute execution limit for Google App Scripts."
  • "Yes, there are other ways to increase the 6 minute execution limit, and these are the ways..."

解决方案

I have dozens of jobs that take > 6 minutes to run. I use an "outerloop" script (below) that I've tuned over the years. It runs each job to completion no matter how long it takes.

Setup (just a guide - you need to apply it to your own specific scenario):

  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().

Code:

//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天全站免登陆