我有一个触发器,每 5 分钟运行一次应用程序脚本函数。有时,该函数运行时间可能超过 5 分钟(但不到 6 分钟),这一情况可能十分之一。如果是这种情况,我不希望触发器再次运行该函数,因为它将重复当前正在执行的操作。我希望它再等一分钟然后运行。如何检查该函数是否正在运行并使用它来允许或禁止触发器?另请注意,我的脚本大多运行大约 3 或 4 分钟(非常短的条目是不同的函数),因此如果我可以提高效率并在提前完成的情况下更快地再次触发,那就太棒了。
最佳答案
将一个持续 10 分钟的值放入缓存中。 在代码末尾,删除 Cache 属性。 当代码运行时,尝试从缓存中获取该值,如果存在,则退出。 我会使用缓存服务而不是属性服务,因为如果代码失败并且属性服务中的值没有更改或删除,那么您的代码可能会完全停止运行。无论代码是否完成,缓存都会在设定的时间超时。
function myCode() {
var isItRunning;
isItRunning = CacheService.getDocumentCache().put("itzRunning", "true",600);//Keep this value in Cache for up to X minutes
//There are 3 types of Cache - if the Apps Script project is not
//bound to a document then use ScriptCache
if (isItRunning) {//If this is true then another instance of this
//function is running which means that you dont want this
//instance of this function to run - so quit
return;//Stop running this instance of this function
}
//Put the code you want to run here
CacheService.getDocumentCache().remove("itzRunning");
}
关于google-apps-script - 如果函数已经在运行,如何防止谷歌应用程序脚本触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67066779/