How to understand LockService and implement it correctly?

Code Summary

I have a Google Apps Script project that is used by approximately 80 users in a specific domain, however the application is executed by me (i.e Publish. Deploy as web app>> Execute the app as:) Me.

One of the functions of the Script is to fill out a Google Sheet from a user form (using HTML Service), and then notify yourself and the sender (who is identified using a simple login system and cookies).

It has been working fine for about 6 months, but a notification email has been sent 1-2 times, but the record in Google Sheet has not appeared.

I think this may be due to the simultaneous use of Script (since two email notifications had the same timestamp) and recently learned about the Lock Service .

I use this post to make sure that I have the correct understanding Lockand how to implement it, to prevent the appearance of entries that do not appear in the Google Sheet due to the simultaneous use of Script.

Implementation

Pseudocode of my script:

Code.gs

var active_spreadsheet = SpreadsheetApp.openById("bbb");

// BEGIN - start lock here

var lock = LockService.getScriptLock();
try {
   lock.waitLock(30000); // wait 30 seconds for others' use of the code section and lock to stop and then proceed
 } catch (e) {
   Logger.log('Could not obtain lock after 30 seconds.');
 }

var active_sheet = active_spreadsheet.getSheetByName("ENTRIES");
var new_start_row = active_sheet.getLastRow() + 1;

//  Do lots of stuff - ie apply dynamic background colors based on previous entries colors, define the target range and set values, set data validations  

SpreadsheetApp.flush(); // applies all pending spreadsheet changes
lock.releaseLock();

// END - end lock here

return; 

Questions

01) whether implementation are correct LockService, getScriptLock(), waitLock()and releaseLock()?

02) Is it recommended to use SpreadsheetApp.flush(), and if this is true, then the correct implementation?

Terminology (for reference)

from: https://developers.google.com/apps-script/reference/lock

Lock:
.

LockService:
.

Lock 4 :

hasLock()
Boolean, true, .

releaseLock()
void, , , .

tryLock (timeoutInMillis)
Boolean, , - .

waitLock (timeoutInMillis)
void, , - .

LockService 3 :

getDocumentLock()
, , .

getScriptLock()
, , .

getUserLock()
Lock, , .

+4
1

, , script , . ? . :

var active_spreadsheet = SpreadsheetApp.openById("bbb");

// BEGIN - start lock here

var lock = LockService.getScriptLock();
try {
    lock.waitLock(30000); // wait 30 seconds for others' use of the code section and lock to stop and then proceed
} catch (e) {
    Logger.log('Could not obtain lock after 30 seconds.');
    return HtmlService.createHtmlOutput("<b> Server Busy please try after some time <p>")
    // In case this a server side code called asynchronously you return a error code and display the appropriate message on the client side
    return "Error: Server busy try again later... Sorry :("
}

// note:  if return is run in the catch block above the following will not run as the function will be exited

var active_sheet = active_spreadsheet.getSheetByName("ENTRIES");
var new_start_row = active_sheet.getLastRow() + 1;

//  Do lots of stuff - ie apply dynamic background colors based on previous entries colors, define the target range and set values, set data validations  

SpreadsheetApp.flush(); // applies all pending spreadsheet changes
lock.releaseLock();

// END - end lock here

return;

, !

+3

Source: https://habr.com/ru/post/1674056/


All Articles