1

I have a function (function doGet()) in Google App Script that is being called from a chrome extension. This function is being called everytime a user clicks a button in chrome extension popup.html. This button in popup.html is simultaneously being clicked by 30+ users. Can I place a check that if the function is already being executed by one user, next user cannot execute it till then ?

Rubén
  • 34,714
  • 9
  • 70
  • 166
aks
  • 458
  • 1
  • 5
  • 16
  • You would have to save a value to a cell, removing it as the last thing that happens. Then the first thing you do for each attempt to run is check on that value. It won't change the buttons, etc, but will put the other person's script on hold. If needed maybe you create a queue for processing order as well? The cell(s) could be in a hidden sheet/tab. – Karl_S Aug 22 '17 at 12:16
  • 1
    @Karl_S Wouldn't [Lock Service](https://developers.google.com/apps-script/reference/lock/) be a more natural solution? –  Aug 22 '17 at 13:34
  • @Michelle Yes, it would! I did not find that when I was searching for the same thing about a year ago. Thank you! – Karl_S Aug 22 '17 at 13:38

1 Answers1

3

You can use Lock Service of App script.

The Lock Service allows scripts to prevents concurrent access to sections of code. This can be useful when you have multiple users or processes modifying a shared resource and want to prevent collisions.

function doGet(){
  var lock = LockService.getScriptLock();
  lock.waitLock(10000); // in milliseconds

// Your code

 lock.releaseLock();
}
Ritesh Nair
  • 3,327
  • 1
  • 17
  • 24