Does the Lock Service in Google Apps Script have a time limit?

35 views
Skip to first unread message

Ryuji Takami

unread,
Oct 3, 2020, 6:05:44 AM10/3/20
to Google Apps Script Community
I would like to ask if there is a time limit for Lock Service in Google Apps Script.

For example, when I subscribe to GSuite, Google Apps Script runs for up to 30 minutes, but the Lock Service seems to lose its effectiveness after 5-6 minutes.

Reference.

Please let me know if you know of any of these.
Thank you very much.

Best Regard,
Ryuji Takami

Translated with www.DeepL.com/Translator (free version)

cbmserv...@gmail.com

unread,
Oct 3, 2020, 4:23:09 PM10/3/20
to google-apps-sc...@googlegroups.com

I am not sure what the upper limit for the lock service is. However, it is not a good practice in general to lock out for such a long duration. Locks should be used to protect multiple writes to critical data to avoid race conditions. Locking out for much longer than a few seconds is not recommended.

--
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/a12c938d-714c-4461-880c-d6d23b259909n%40googlegroups.com.

Alan Wells

unread,
Oct 3, 2020, 8:03:07 PM10/3/20
to Google Apps Script Community
I don't see anything in the documentation that states a time limit for lock service.
Imagine if one instance of your code needed to run for 25 to 35 seconds in order to complete,
and you set the lock for 15 seconds.
What would happen?
The lock would release in 15 seconds, but the first instance of your code would still be running,
then, I'm assuming, that the next instance of the code would start running.
So, now you'd have two instances of the code running?
And that's what you're trying to avoid.
But the next instance of the code would also set a lock.
So, even though the first lock timed-out before the code completed,
the next instance of the code would set another lock.
But, I don't know of any documentation that explains what really happens in a situation like this.
Personally, I don't really trust Lock Service 100% because in the past I've tested it, and it didn't really work.
Whether it's better now or not, I don't know.
In the past I've set up a test for Lock Service where I called the server code lots of times in rapid succession
from client side code, and Lock Service did not work well at all.
But, let's go back to the timeout setting.
If you set the timeout setting for much longer than is needed,
but when you code completes it releases the lock,
then I'd think that the extra long timeout setting would be totally material.
For example, imagine that it takes your code 10 seconds to run,
and you set the time out for 1000 seconds.
But when your code completes after 10 seconds your code releases the lock.
The fact that you set the lock timeout for 1000 seconds doesn't matter,
as long as your code reliably releases the lock every time.
There can be a serious problem though.
If there is a fatal error, and your lock doesn't get released,
and the lock is set for 1000 seconds, then the lock is waiting for nothing.
So, make sure that you have error handling, and that your error handling releases the lock.
You need to have an idea about what the average duration time of your code is.
And if your code could have widely varying duration times,
then you need to assume the worst and plan for the longest run time.
Otherwise your lock could time out before the code is done running.
I don't want even 1% failure rate of the lock.
I wouldn't set the lock timeout for a shorter time that you think is needed and take a risk
that maybe you'll be okay.
At some point, you're not going to be okay.
Making sure that the lock gets released when your code is done is probably one of the most important things you can do.
And making sure that you have a line of code that releases the lock for every possible way that your code can run,
could be a problem if you've got a lot of code, and lots of possible ending points.

cbmserv...@gmail.com

unread,
Oct 3, 2020, 8:12:10 PM10/3/20
to google-apps-sc...@googlegroups.com

I believe there may be some misconceptions on what a lock does in Google AppsScript.

 

In most computer operating systems, a lock would prevent other instances from accessing that section of code as Alan mentioned.

 

However what would occur is the second instance is actually suspended until the first instance completes and releases the lock. So the second instance will still run but only after the first one completed. There will only ever be once instance in that section of code for the timeout you specify.

 

Hope that helps.

Ryuji Takami

unread,
Oct 4, 2020, 3:38:36 AM10/4/20
to google-apps-sc...@googlegroups.com
Thank you both for your answers.
It was very helpful.

I'm glad to hear about both of you.
I think I misunderstood the use and original purpose of LockService.

---

I did not share the premise.
I accidentally created multiple pages for a site (about 500,000 pages) and I wanted to delete them all at once, but the service only allowed the API to delete one page at a time.

I tried running it several times with a setting of deleting 200 pages per instance, and found that it took at least 10 minutes and up to 20 minutes to execute, depending on the time and server load.
In other words, the processing time varied depending on the timing of the execution.
Under these conditions, we looked for a way to utilize triggers and delete continuously for 24 hours to finish the process in the shortest possible time, and came up with the idea of setting a 15-minute time trigger and keeping it running for 24 hours.
This is because we wanted to release Lock if it finishes executing within 15 minutes, and if it takes longer than 15 minutes to execute, we wanted to make sure that the instance is locked and cannot be executed.

Here's an image of the description code
The following is an image of the code.

ーーーー
//Every 15 minutes
function doDelete() {
  var lock = LockService. getScriptLock();

  if(lock.tryLock(5000)) {
    var values = array ; //here is an array of 200 deleted data requests
    for(var i in values) {
      Deletion process
    }
  lock. releaseLock()
  }
}

ーーー

I was concerned about duplicate executions when I continued to run for 24 hours, in some cases it would time out due to a server error in GoogleAppsScript.
I would like to get over this by devising the unit of processing and execution frequency.

Thank you.

2020年10月4日(日) 9:12 <cbmserv...@gmail.com>:
You received this message because you are subscribed to a topic in the Google Groups "Google Apps Script Community" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-apps-script-community/Nt0aLVN8FD8/unsubscribe.
To unsubscribe from this group and all its topics, 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/002b01d699e2%24fdffb2a0%24f9ff17e0%24%40gmail.com.

Alan Wells

unread,
Oct 4, 2020, 11:02:01 AM10/4/20
to Google Apps Script Community
You could try something like what is explained in the following site.


Basically, the code is monitoring the run time, and then creating a time-based trigger for some time in the future
to run in order to automatically keep the process continuing.

So, one option is to use a time based trigger to run the code in order to avoid the need to run the code in person.
Reply all
Reply to author
Forward
0 new messages