0

I have a Looker Studio custom connector written in Google Apps Script. In the connector, I am using the LockService's tryLock(), hasLock(), and releaseLock() functions to prevent concurrent execution of a section of code. Most of the time it works fine, but sometimes I get an error like this:

Exception: We're sorry, a server error occurred. Please wait a bit and try again.

The stack trace shows that the error always comes on the lines where the lock functions are called.

I am initializing the lock globally like this:

var lock = LockService.getScriptLock();

and then using the functions like this:

function execute(request) {
    try {
        lock.tryLock(100);
        if(lock.hasLock()) {
            // custom code
            lock.releaseLock(); 
        }
    }
    catch(e) {
        console.error(e)
    }
    finally {
        if(lock.hasLock()) {
            lock.releaseLock();
        }
    }
}
2
  • Please add the textual error message, including the stack trace.
    – Wicket
    Commented Jun 12 at 14:45
  • @Wicket I am just getting the following error: Exception: We're sorry, a server error occurred. Please wait a bit and try again. at Code:308 (responseToRows) at Code:435 (getData) Commented Jun 13 at 4:54

1 Answer 1

0

This error is pretty common to happen. Considering this you might want to implement an algorithm to retry when it happens, like exponential backoff.

From Peter Herrmann's BetterLog Library repository in GitHub

//copy version 10 lib GASRetry 'MGJu3PS2ZYnANtJ9kyn2vnlLDhaBgl_dE' (changed function name and log line)
function call_(func, optLoggerFunction) {
  for (var n=0; n<6; n++) {
    try {
      return func();
    } catch(e) {
      if (optLoggerFunction) {optLoggerFunction("call_ " + n + ": " + e)}
      if (n == 5) {
        throw e;
      } 
      Utilities.sleep((Math.pow(2,n)*1000) + (Math.round(Math.random() * 1000)));
    }    
  }
}

P.D. Personally, I don't particularly appreciate including calls to Google Apps Script services on the global scope. This practice causes problems like the errors mentioned in this question to be more frequent and makes the debugging and test logic more complex.

Related

Not the answer you're looking for? Browse other questions tagged or ask your own question.