Nigel Johnson
Nigel Johnson

Reputation: 522

Google App Script and public locks

I have a spreadsheet that I have a doGet() a doPost() as well as a timed trigger to do some updates. I want these to block each other. Yay the LockService... hopefully.

The Google documentation on the LockService does not document the getPublicLock() method and I can't seem to find anything specific to my needs. The documentation above does say that getDocumentLock() will return null in a web app which is not particularly useful, and general posts around the interweb suggest that it will stop bits of code being run concurrently... which isn't quite what I want. What I want is to stop requests for data when data is being updated.

I have tried to test it, but I can't seem to get a script to run for suitably long enough to test this to my satisfaction.

If I use:

var lock = LockService.getPublicLock();
lock.waitLock(30000);

In all 3 entry points, will the script successfully lock when any other invocation is called?

Upvotes: 1

Views: 809

Answers (1)

FatFingersJackson
FatFingersJackson

Reputation: 812

I have not run into LockService.getPublicLock() in Google-Apps-Script.

However the LockService.getScriptLock() should work. As the documentation states :

Gets a lock that prevents any user from concurrently running a section of code. A code section guarded by a script lock cannot be executed simultaneously regardless of the identity of the user. Note that the lock is not actually acquired until Lock.tryLock(timeoutInMillis) or Lock.waitLock(timeoutInMillis) is called.

I have used it particularly with Web App and as far as I have tested it it works as promised:

While anyone using the app has reserved a single Script Lock in the script, any other block locked by Script Lock will not execute, for any user.

I would have added this as a comment but I have no right to comment yet..

Upvotes: 2

Related Questions