如何修复 "There are too many LockService operations against the same script" 错误?
How to fix "There are too many LockService operations against the same script" error?
我有一个 public 插件,它利用 Google Apps 脚本锁定服务来防止在写入之前读取电子表格。
var lock = LockService.getDocumentLock();
var success = lock.tryLock(240000); // 4 minutes
if (!success) {
console.warn('Could not obtain lock after 4 minutes.');
return;
}
//perform some function then release lock
lock.releaseLock();
我继续收到错误消息:
There are too many LockService operations against the same script.
最初我认为这个错误是 'user based' 并且每个用户 的锁定服务操作数量可能存在配额 ,但是我最近开始看到它出现得更多这几天经常针对30+用户
- LockService 操作的数量是否有限制每个用户
或 每个脚本?
- 如果可以,限制是多少?
中没有看到任何对配额的引用
中看到任何对配额的引用
当有多个触发器试图同时访问同一个 Google Sheet 时,可能会发生这种情况。 Google 配额页面没有在任何地方记录确切的限制,但尝试减少与 sheet 关联的触发器数量,它应该可以工作。
我不知道问题的答案(所以我不会将此答案标记为已接受),但这是我的解决方法。
不是最优雅的解决方案,因为它不能防止所有情况(快速连续触发 运行 并且有时 运行 乱序触发),所以来自更有经验的人的任何指导都是赞赏。
try{
for(var i=1; i<11; i++){ //attempt 10 times, then run and hope for the best
var documentLock = PropertiesService.getDocumentProperties().getProperty('documentLock');
if(documentLock){
Utilities.sleep(20000); //if locked, sleep for 20 seconds
console.log('Wait ' + i + ' for documentLock to be released.');
}else{break;}
}
PropertiesService.getDocumentProperties().setProperty('documentLock', true);
runSomeFunction()
}
catch(err){
console.error(err);
console.error(err["stack"]);
}
finally{
PropertiesService.getDocumentProperties().deleteProperty('documentLock');
}
有报道:https://issuetracker.google.com/issues/112384851
这似乎偶尔会发生。一名 Google 员工回复如下:
Just to give an update to this: The Apps Script team recommends:
1) Use a try/catch block around the LockService call, and assume that an error means "lock was not acquired" (whatever that means for the logic the script is trying to do).
2) If "lock was not acquired" means "script should try again", then put a sleep in there of a few seconds (maybe even exponential backoff) before retrying the LockService operation.
We do not have any other solution for this at the moment and might have an update once certain changes are made to Apps Script.
我有一个 public 插件,它利用 Google Apps 脚本锁定服务来防止在写入之前读取电子表格。
var lock = LockService.getDocumentLock();
var success = lock.tryLock(240000); // 4 minutes
if (!success) {
console.warn('Could not obtain lock after 4 minutes.');
return;
}
//perform some function then release lock
lock.releaseLock();
我继续收到错误消息:
There are too many LockService operations against the same script.
最初我认为这个错误是 'user based' 并且每个用户 的锁定服务操作数量可能存在配额 ,但是我最近开始看到它出现得更多这几天经常针对30+用户
- LockService 操作的数量是否有限制每个用户 或 每个脚本?
- 如果可以,限制是多少?
当有多个触发器试图同时访问同一个 Google Sheet 时,可能会发生这种情况。 Google 配额页面没有在任何地方记录确切的限制,但尝试减少与 sheet 关联的触发器数量,它应该可以工作。
我不知道问题的答案(所以我不会将此答案标记为已接受),但这是我的解决方法。
不是最优雅的解决方案,因为它不能防止所有情况(快速连续触发 运行 并且有时 运行 乱序触发),所以来自更有经验的人的任何指导都是赞赏。
try{
for(var i=1; i<11; i++){ //attempt 10 times, then run and hope for the best
var documentLock = PropertiesService.getDocumentProperties().getProperty('documentLock');
if(documentLock){
Utilities.sleep(20000); //if locked, sleep for 20 seconds
console.log('Wait ' + i + ' for documentLock to be released.');
}else{break;}
}
PropertiesService.getDocumentProperties().setProperty('documentLock', true);
runSomeFunction()
}
catch(err){
console.error(err);
console.error(err["stack"]);
}
finally{
PropertiesService.getDocumentProperties().deleteProperty('documentLock');
}
有报道:https://issuetracker.google.com/issues/112384851
这似乎偶尔会发生。一名 Google 员工回复如下:
Just to give an update to this: The Apps Script team recommends:
1) Use a try/catch block around the LockService call, and assume that an error means "lock was not acquired" (whatever that means for the logic the script is trying to do).
2) If "lock was not acquired" means "script should try again", then put a sleep in there of a few seconds (maybe even exponential backoff) before retrying the LockService operation.
We do not have any other solution for this at the moment and might have an update once certain changes are made to Apps Script.