LockService: ScriptLock

118 views
Skip to first unread message

Duya

unread,
Mar 8, 2024, 12:07:35 AM3/8/24
to Google Apps Script Community
Hi, there.

I am in deep-chaos about getScriptLock() in Lock Service.

What I am trying to build ;
There is a list of fruits ( sheet fruits)
 User runs the script to get a fruit ( 2 persons cannot get a same fruit)

My experience ;

2 users ran the script and selected apple at the almost same time and they both won apple.
I suppose I include scriptLock in checkFruitAvailability() so two of them should not have won apple but one of them only..

I greatly appreciate if you could help me on this...

スクリーンショット 2024-03-08 13.45.14.png 

スクリーンショット 2024-03-08 14.03.19.png

Coding;

main function
スクリーンショット 2024-03-08 13.54.48.png

functions called by main

スクリーンショット 2024-03-08 13.55.42.png

スクリーンショット 2024-03-08 13.56.30.png

スクリーンショット 2024-03-08 13.56.48.png

sheet fruits

スクリーンショット 2024-03-08 13.57.56.png

sheet winner

スクリーンショット 2024-03-08 13.58.15.png

Ryuya Sakurai

unread,
Mar 8, 2024, 12:22:13 AM3/8/24
to google-apps-sc...@googlegroups.com
sorry, on the bottom of my previous email, pics for sheet fruit and sheet winner are opposite..

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d7118848-9d48-41a5-a356-f59a90d71fc0n%40googlegroups.com.


--

Best Regrads,

Ryuya Sakurai (櫻井 龍弥)

Phil Bainbridge

unread,
Mar 8, 2024, 5:21:28 AM3/8/24
to Google Apps Script Community

Hi

If it helps I have recently dabbled with ScriptLock on my blog for controlling multiple Form submissions. I have tried to break down the process which should help you implement it at the start/end of your Function.


Kind regards
Phil

Ryuya Sakurai

unread,
Mar 8, 2024, 5:33:09 AM3/8/24
to google-apps-sc...@googlegroups.com
Thank you, Phil.

With the attached, I actually tried if ( lock.tryLock(3000) ) instead of  if( lock.hasLock() ) that you scripted.

I would like to have more understanding in the differences between them.

Appreciate your support :)

2024年3月8日(金) 19:21 Phil Bainbridge <phil...@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
Screenshot_20240308-192717.png

Phil Bainbridge

unread,
Mar 8, 2024, 8:16:49 AM3/8/24
to Google Apps Script Community

Hi

If you look at my blog post I provide a breakdown under the heading "The Code".

Now we need to try and acquire the Lock so we know we have permission to run our section of code and ensure it is not in use already. We include a timeout here (10 seconds in this example) to prevent it from perpetually waiting:

lock.tryLock(10000);

We then test to see if it has been acquired (boolean) before running our main code snippet:

if (lock.hasLock()) { ...



You have to first try and get the lock before you can then run your code once you have it - that's how to prevent instances of the code from over running each other.

Kind regards
Phil

Duya

unread,
Mar 11, 2024, 7:40:18 PM3/11/24
to Google Apps Script Community
Hi

I have finally got this solved with your advice.

Thank you so much for your help.

Duya

Phil Bainbridge

unread,
Mar 12, 2024, 5:03:17 AM3/12/24
to Google Apps Script Community

Excellent!!

Phil
Reply all
Reply to author
Forward
0 new messages