有什么办法可以增加 Google Apps 脚本的 6 分钟执行时间限制?我认为答案可能是 G Business Suite 的抢先体验计划。如果我能加入 Early Access 计划,我可能愿意每月为 G Business Suite 支付 10 美元,以便将执行限制从 6 分钟增加到 30 分钟。但是 G Suite 帮助论坛的一位顾问说抢先体验是一个有限的计划,这意味着我不能保证我能得到它。
有没有其他方法可以增加 6 分钟的执行限制?
请注意,在这个问题中,我不是在询问如何优化我的脚本以适应 6 分钟限制的想法。 (我将来可能会问,如果当前问题的答案是“不,没有其他办法。”)
因此,对当前问题的适当答案是:
最佳答案
我有几十个作业需要 > 6 分钟才能运行。我使用了多年来一直在调整的“外环”脚本(如下)。无论需要多长时间,它都会运行每个作业直到完成。
设置(只是一个指南 - 您需要将其应用于您自己的特定场景):
var thingies =
到你想要处理的任何东西。理想情况下,它应该是一个数组。 //do our work here
后面添加您自己的代码线路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;
}
}
关于google-apps-script - 如何增加 Google Apps 脚本中的 6 分钟执行限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971806/