I am writing a code on Google Apps Script. I need to give an unique number for each user.
At first, I wrote some code with tryLock and ScriptProperties class. It gave the same number for several persons when 6 users called the function at almost the same time. So, now I am using waitLock and ScriptProperties.
Is there any difference between tryLock and waitLock in terms of locking ability? Also, I am wondering the update timing of ScriptProperties. Is it updated immediately for all users?
If you give advises on this issue, I really appreciate it.
//My code with tryLock: This gave the same number for 3 users in a test by 6 users.
var glock = LockService.getPublicLock();
if( glock.tryLock(10000) )
{
var val = ScriptProperties.getProperty(proName) * 1 + 1;
ScriptProperties.setProperty(proName, val);
glock.releaseLock();
return val;
} else { return null; }
//Another code with waitLock: This gave an unique number for each in a test by 8 users.
var val = null;
try{
var glock = LockService.getPublicLock();
glock.waitLock(10000);
val = ScriptProperties.getProperty(proName) * 1 + 1;
ScriptProperties.setProperty(proName, val);
glock.releaseLock();
} catch (e) { }
return val;