V
V
Viktor Familyevich2018-01-23 14:21:36
Google Apps Script
Viktor Familyevich, 2018-01-23 14:21:36

What is the correct way to use LockService in Google Apps Script?

Good afternoon. There is a code that accepts post and get requests and inserts the transferred data into the table. So that the data is not overwritten, I added a queue:

var lock = LockService.getPublicLock();
lock.waitLock(30000);
//код
lock.releaseLock();

But after that, the lock timeout error constantly appears in the logs: another process holds the lock for too long. at doPost(Post:4). How can I correctly register the queue so that errors do not fall out?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ivanov, 2018-01-25
@Wintego

The waitLock method throws an exception, handle it.

var lock = LockService.getPublicLock();
try {
  lock.waitLock(30000);
  return success();
} catch (err) {
  Logger.log('Could not obtain lock after 30 seconds.');
  return failure();
}

For function continuity, use tryLock. This method wraps the lock result in a boolean value:
var lock = LockService.getPublicLock();
var success = lock.tryLock(30000);
if (!success) {
  Logger.log('Could not obtain lock after 30 seconds.');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question