我可以在两个不相关的函数中调用Lock,而没有一个锁影响另一个吗?

时间:2017-04-25 22:18:08

标签: google-apps-script locking

如果我在Code.gs中有两个不相关的功能(每个功能都与不同的Google表格相互作用),我是否可以在两个功能中调用lock而不会有一个呼叫影响另一个功能?

对于更多上下文,该应用程序在同一个域中拥有大约80个用户,正在以Me身份运行,但有可能:

  • 用户A在
  • 的同时拨打myFunction_01()
  • 用户B致电myFunction_02()

在这种情况下,锁会独立运行吗?

lock的以下实施是否可以接受?

function myFunction_01() {

var my_spreadsheet_01 = SpreadsheetApp.openById("YYY");

var lock = LockService.getScriptLock();

try {
  lock.waitLock(30000); 
} catch (e) {
    Logger.log('Could not obtain lock after 30 seconds.');
    var returnObjectCatchError = {};
    returnObjectCatchError['LockErrorFlag'] = "An exception occurred when waiting for lock"; 
    return returnObjectCatchError; 
}

// do things with my_spreadsheet_01 here

var return_object = {};

// apply all pending spreadsheet changes
SpreadsheetApp.flush(); 

//release lock
lock.releaseLock();

return return_object;

}

function myFunction_02() {

var my_spreadsheet_02 = SpreadsheetApp.openById("ZZZ");

var lock = LockService.getScriptLock();

try {
  lock.waitLock(30000); 
} catch (e) {
    Logger.log('Could not obtain lock after 30 seconds.');
    var returnObjectCatchError = {};
    returnObjectCatchError['LockErrorFlag'] = "An exception occurred when waiting for lock"; 
    return returnObjectCatchError; 
}

// do things with my_spreadsheet_02 here

var return_object = {};

// apply all pending spreadsheet changes
SpreadsheetApp.flush(); 

//release lock
lock.releaseLock();

return return_object;

}

0 个答案:

没有答案