Google App Script and public locks

泄露秘密 提交于 2021-01-28 08:06:08

问题


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?


回答1:


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..



来源:https://stackoverflow.com/questions/46753252/google-app-script-and-public-locks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!