title: How to understand LockService and implement it correctly?
body: <p><strong>Summary of Code</strong></p>
  
  <p>I have a Google Apps Script project that is used by around 80 users within a specific domain, however the app is executed by me (ie <code>Publish</code> > <code>Deploy as web app</code> > <code>Execute the app as</code>:  <code>Me</code>).  </p>
  
  <p>One of the functions of the script is to populate a Google Sheet from a custom form (using <code>HTML Service</code>) and then notify myself and the submitting user (who is identified through the use of a simple login system and cookies).  </p>
  
  <p>It has been working fine for about 6 months, however on 1-2 occasions the notification email has been sent but the Google Sheet entry has not appeared.  </p>
  
  <p>I am thinking this might be due to concurrent use of the script (as two notification emails had the same timestamp) and have recently learned of <a href="https://developers.google.com/apps-script/reference/lock/" rel="noreferrer">Lock Service</a>.  </p>
  
  <p>I am using this post to ensure I have the correct understanding of <code>Lock</code> and how to implement it in order to prevent entries not appearing in the Google Sheet due to concurrent script usage.  </p>
  
  <p><strong>Implementation</strong></p>
  
  <p>The pseudo code of my scenario is:</p>
  
  <p><em>Code.gs</em></p>
  
  <pre><code>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; 
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <p><strong>01)</strong>  Are the implementations of <code>LockService</code>, <code>getScriptLock()</code>, <code>waitLock()</code> and <code>releaseLock()</code> correct?</p>
  
  <p><strong>02)</strong>  Is it recommended to use <code>SpreadsheetApp.flush()</code>, and if so is the implementation above correct?</p>
  
  <p><strong>Terminology</strong> (for reference)  </p>
  
  <p>from:  <a href="https://developers.google.com/apps-script/reference/lock" rel="noreferrer">https://developers.google.com/apps-script/reference/lock</a> </p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock.html" rel="noreferrer">Lock</a>:<br>
  A representation of a mutual-exclusion lock.</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock-service.html" rel="noreferrer">LockService</a>:<br>
  Prevents concurrent access to sections of code.</p>
  
  <p>The Lock class has 4 methods:</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock.html#hasLock()" rel="noreferrer">hasLock()</a><br>
  Boolean, Returns true if the lock was acquired.</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock.html#releaseLock()" rel="noreferrer">releaseLock()</a><br>
  void, Releases the lock, allowing other processes waiting on the lock to continue.</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock.html#tryLock(Integer)" rel="noreferrer">tryLock(timeoutInMillis)</a><br>
  Boolean, Attempts to acquire the lock, timing out after the provided number of milliseconds.</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock.html#waitLock(Integer)" rel="noreferrer">waitLock(timeoutInMillis)</a><br>
  void, Attempts to acquire the lock, timing out with an exception after the provided number of milliseconds.</p>
  
  <p>The LockService class has 3 methods:</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock-service.html#getDocumentLock()" rel="noreferrer">getDocumentLock()</a><br>
  Lock, Gets a lock that prevents any user of the current document from concurrently running a section of code.</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock-service.html#getScriptLock()" rel="noreferrer">getScriptLock()</a><br>
  Lock, Gets a lock that prevents any user from concurrently running a section of code.</p>
  
  <p><a href="https://developers.google.com/apps-script/reference/lock/lock-service.html#getUserLock()" rel="noreferrer">getUserLock()</a><br>
  Lock, Gets a lock that prevents the current user from concurrently running a section of code.</p>